LEFT | RIGHT |
(no file at all) | |
| 1 /* |
| 2 * ***** BEGIN GPL LICENSE BLOCK ***** |
| 3 * |
| 4 * This program is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU General Public License |
| 6 * as published by the Free Software Foundation; either version 2 |
| 7 * of the License, or (at your option) any later version. |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software Foundation, |
| 16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 * |
| 18 * Contributor(s): Tao Ju |
| 19 * |
| 20 * ***** END GPL LICENSE BLOCK ***** |
| 21 */ |
| 22 |
| 23 #ifndef MEMORYALLOCATOR_H |
| 24 #define MEMORYALLOCATOR_H |
| 25 |
| 26 #include <stdio.h> |
| 27 #include <stdlib.h> |
| 28 |
| 29 #define HEAP_BASE 16 |
| 30 #define UCHAR unsigned char |
| 31 |
| 32 /** |
| 33 * Customized memory allocators that allocates/deallocates memory in chunks |
| 34 * |
| 35 * @author Tao Ju |
| 36 */ |
| 37 |
| 38 |
| 39 |
| 40 /** |
| 41 * Base class of memory allocators |
| 42 */ |
| 43 class VirtualMemoryAllocator |
| 44 { |
| 45 public: |
| 46 virtual UCHAR * allocate( ) = 0 ; |
| 47 virtual void deallocate( UCHAR * obj ) = 0 ; |
| 48 virtual void destroy( ) = 0 ; |
| 49 virtual void printInfo( ) = 0 ; |
| 50 |
| 51 virtual int getAllocated( ) = 0 ; |
| 52 virtual int getAll( ) = 0 ; |
| 53 virtual int getBytes( ) = 0 ; |
| 54 }; |
| 55 |
| 56 /** |
| 57 * Dynamic memory allocator - allows allocation/deallocation |
| 58 *· |
| 59 * Note: there are 4 bytes overhead for each allocated yet unused object. |
| 60 */ |
| 61 template < int N > |
| 62 class MemoryAllocator : public VirtualMemoryAllocator |
| 63 { |
| 64 private: |
| 65 |
| 66 /// Constants |
| 67 int HEAP_UNIT, HEAP_MASK ; |
| 68 |
| 69 /// Data array |
| 70 UCHAR ** data ; |
| 71 |
| 72 /// Allocation stack |
| 73 UCHAR *** stack ; |
| 74 |
| 75 /// Number of data blocks |
| 76 int datablocknum ; |
| 77 |
| 78 /// Number of stack blocks |
| 79 int stackblocknum ; |
| 80 |
| 81 /// Size of stack |
| 82 int stacksize ; |
| 83 |
| 84 /// Number of available objects on stack |
| 85 int available ; |
| 86 |
| 87 /** |
| 88 * Allocate a memory block |
| 89 */ |
| 90 void allocateDataBlock ( ) |
| 91 { |
| 92 // Allocate a data block |
| 93 datablocknum += 1 ; |
| 94 data = ( UCHAR ** )realloc( data, sizeof ( UCHAR * ) * datablock
num ) ; |
| 95 data[ datablocknum - 1 ] = ( UCHAR * )malloc( HEAP_UNIT * N ) ; |
| 96 |
| 97 // Update allocation stack |
| 98 for ( int i = 0 ; i < HEAP_UNIT ; i ++ ) |
| 99 { |
| 100 stack[ 0 ][ i ] = ( data[ datablocknum - 1 ] + i * N ) ; |
| 101 } |
| 102 available = HEAP_UNIT ; |
| 103 } |
| 104 |
| 105 /** |
| 106 * Allocate a stack block, to store more deallocated objects |
| 107 */ |
| 108 void allocateStackBlock( ) |
| 109 { |
| 110 // Allocate a stack block |
| 111 stackblocknum += 1 ; |
| 112 stacksize += HEAP_UNIT ; |
| 113 stack = ( UCHAR *** )realloc( stack, sizeof ( UCHAR ** ) * stack
blocknum ) ; |
| 114 stack[ stackblocknum - 1 ] = ( UCHAR ** )malloc( HEAP_UNIT * siz
eof ( UCHAR * ) ) ; |
| 115 } |
| 116 |
| 117 |
| 118 public: |
| 119 /** |
| 120 * Constructor |
| 121 */ |
| 122 MemoryAllocator( ) |
| 123 { |
| 124 HEAP_UNIT = 1 << HEAP_BASE ; |
| 125 HEAP_MASK = ( 1 << HEAP_BASE ) - 1 ; |
| 126 |
| 127 data = ( UCHAR ** )malloc( sizeof( UCHAR * ) ) ; |
| 128 data[ 0 ] = ( UCHAR * )malloc( HEAP_UNIT * N ) ; |
| 129 datablocknum = 1 ; |
| 130 |
| 131 stack = ( UCHAR *** )malloc( sizeof ( UCHAR ** ) ) ; |
| 132 stack[ 0 ] = ( UCHAR ** )malloc( HEAP_UNIT * sizeof ( UCHAR * )
) ; |
| 133 stackblocknum = 1 ; |
| 134 stacksize = HEAP_UNIT ; |
| 135 available = HEAP_UNIT ; |
| 136 |
| 137 for ( int i = 0 ; i < HEAP_UNIT ; i ++ ) |
| 138 { |
| 139 stack[ 0 ][ i ] = ( data[ 0 ] + i * N ) ; |
| 140 } |
| 141 } |
| 142 |
| 143 /** |
| 144 * Destructor |
| 145 */ |
| 146 void destroy( ) |
| 147 { |
| 148 int i ; |
| 149 for ( i = 0 ; i < datablocknum ; i ++ ) |
| 150 { |
| 151 free( data[ i ] ) ; |
| 152 } |
| 153 for ( i = 0 ; i < stackblocknum ; i ++ ) |
| 154 { |
| 155 free( stack[ i ] ) ; |
| 156 } |
| 157 free( data ) ; |
| 158 free( stack ) ; |
| 159 } |
| 160 |
| 161 /** |
| 162 * Allocation method |
| 163 */ |
| 164 UCHAR * allocate ( ) |
| 165 { |
| 166 if ( available == 0 ) |
| 167 { |
| 168 allocateDataBlock ( ) ; |
| 169 } |
| 170 |
| 171 // printf("Allocating %d\n", header[ allocated ]) ; |
| 172 available -- ; |
| 173 return stack[ available >> HEAP_BASE ][ available & HEAP_MASK ]
; |
| 174 } |
| 175 |
| 176 /** |
| 177 * De-allocation method |
| 178 */ |
| 179 void deallocate ( UCHAR * obj ) |
| 180 { |
| 181 if ( available == stacksize ) |
| 182 { |
| 183 allocateStackBlock ( ) ; |
| 184 } |
| 185 |
| 186 // printf("De-allocating %d\n", ( obj - data ) / N ) ; |
| 187 stack[ available >> HEAP_BASE ][ available & HEAP_MASK ] = obj ; |
| 188 available ++ ; |
| 189 // printf("%d %d\n", allocated, header[ allocated ]) ; |
| 190 } |
| 191 |
| 192 /** |
| 193 * Print information |
| 194 */ |
| 195 void printInfo ( ) |
| 196 { |
| 197 printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n", getByte
s(), getAllocated(), getAll(), stacksize ) ; |
| 198 } |
| 199 |
| 200 /** |
| 201 * Query methods |
| 202 */ |
| 203 int getAllocated( ) |
| 204 { |
| 205 return HEAP_UNIT * datablocknum - available ;··· |
| 206 }; |
| 207 |
| 208 int getAll( ) |
| 209 { |
| 210 return HEAP_UNIT * datablocknum ; |
| 211 }; |
| 212 |
| 213 int getBytes( ) |
| 214 { |
| 215 return N ;······ |
| 216 }; |
| 217 }; |
| 218 |
| 219 #endif |
LEFT | RIGHT |