1 /******************************************************************************* 2 3 Base class for a bucket element allocator using the D runtime memory 4 manager. Even though this memory manager is called "GC-managed" this class 5 in fact doesn't rely on garbage collection but explicitly deletes unused 6 bucket elements. 7 8 Copyright: 9 Copyright (c) 2009-2016 dunnhumby Germany GmbH. 10 All rights reserved. 11 12 License: 13 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 14 Alternatively, this file may be distributed under the terms of the Tango 15 3-Clause BSD License (see LICENSE_BSD.txt for details). 16 17 *******************************************************************************/ 18 19 module ocean.util.container.map.model.IBucketElementGCAllocator; 20 21 22 import ocean.util.container.map.model.IAllocator; 23 24 /******************************************************************************/ 25 26 class IBucketElementGCAllocator: IAllocator 27 { 28 /*************************************************************************** 29 30 Constructor. 31 32 Params: 33 bucket_element_sizeof = the amount of memory used by each allocated 34 element. 35 36 ***************************************************************************/ 37 38 public this ( size_t bucket_element_sizeof ) 39 { 40 super(bucket_element_sizeof); 41 } 42 43 /*************************************************************************** 44 45 Deletes a bucket element that is no longer used. 46 47 Params: 48 element = old bucket element 49 50 ***************************************************************************/ 51 52 protected override void deallocate ( void* element ) 53 { 54 import core.memory; 55 GC.free(element); 56 } 57 58 /*************************************************************************** 59 60 Helper class to temprarily park a certain number of bucket elements. 61 62 ***************************************************************************/ 63 64 static class ParkingStack: IParkingStack 65 { 66 /*********************************************************************** 67 68 List of parked object. 69 70 ***********************************************************************/ 71 72 private void*[] elements; 73 74 /*********************************************************************** 75 76 Constructor. 77 78 Params: 79 n = number of objects that will be parked 80 81 ***********************************************************************/ 82 83 public this ( size_t n ) 84 { 85 super(n); 86 this.elements = new void*[n]; 87 } 88 89 /********************************************************************** 90 91 Pushes an object on the stack. 92 93 Params: 94 element = object to push 95 n = number of parked objects before object is pushed 96 (guaranteed to be less than max_length) 97 98 **********************************************************************/ 99 100 protected override void push_ ( void* element, size_t n ) 101 { 102 this.elements[n] = element; 103 } 104 105 /********************************************************************** 106 107 Pops an object from the stack. This method is never called if the 108 stack is empty. 109 110 Params: 111 n = number of parked objects after object is popped (guaranteed 112 to be less than max_length) 113 114 Returns: 115 object popped from the stack or null if the stack is empty. 116 117 **********************************************************************/ 118 119 protected override void* pop_ ( size_t n ) 120 { 121 return this.elements[n]; 122 } 123 } 124 125 /*************************************************************************** 126 127 Calls dg with an IParkingStack instance that is set up to keep n 128 elements. 129 130 Params: 131 n = number of elements to park 132 dg = delegate to call with the IParkingStack instance 133 134 ***************************************************************************/ 135 136 public override void parkElements (size_t n, 137 scope void delegate ( IParkingStack park ) dg) 138 { 139 scope park = new ParkingStack(n); 140 dg(park); 141 } 142 }