2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Tao Ju
20 * ***** END GPL LICENSE BLOCK *****
23 #ifndef __MEMORYALLOCATOR_H__
24 #define __MEMORYALLOCATOR_H__
30 #define UCHAR unsigned char
33 * Customized memory allocators that allocates/deallocates memory in chunks
41 * Base class of memory allocators
43 class VirtualMemoryAllocator
46 virtual ~VirtualMemoryAllocator() {}
48 virtual void *allocate( ) = 0;
49 virtual void deallocate(void *obj) = 0;
50 virtual void destroy( ) = 0;
51 virtual void printInfo( ) = 0;
53 virtual int getAllocated( ) = 0;
54 virtual int getAll( ) = 0;
55 virtual int getBytes( ) = 0;
57 #ifdef WITH_CXX_GUARDEDALLOC
58 MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:VirtualMemoryAllocator")
64 * Dynamic memory allocator - allows allocation/deallocation
66 * Note: there are 4 bytes overhead for each allocated yet unused object.
69 class MemoryAllocator : public VirtualMemoryAllocator
74 int HEAP_UNIT, HEAP_MASK;
82 /// Number of data blocks
85 /// Number of stack blocks
91 /// Number of available objects on stack
95 * Allocate a memory block
97 void allocateDataBlock( )
99 // Allocate a data block
101 data = ( UCHAR ** )realloc(data, sizeof (UCHAR *) * datablocknum);
102 data[datablocknum - 1] = ( UCHAR * )malloc(HEAP_UNIT * N);
104 // Update allocation stack
105 for (int i = 0; i < HEAP_UNIT; i++)
107 stack[0][i] = (data[datablocknum - 1] + i * N);
109 available = HEAP_UNIT;
113 * Allocate a stack block, to store more deallocated objects
115 void allocateStackBlock( )
117 // Allocate a stack block
119 stacksize += HEAP_UNIT;
120 stack = ( UCHAR *** )realloc(stack, sizeof (UCHAR * *) * stackblocknum);
121 stack[stackblocknum - 1] = ( UCHAR ** )malloc(HEAP_UNIT * sizeof (UCHAR *) );
131 HEAP_UNIT = 1 << HEAP_BASE;
132 HEAP_MASK = (1 << HEAP_BASE) - 1;
134 data = ( UCHAR ** )malloc(sizeof(UCHAR *) );
135 data[0] = ( UCHAR * )malloc(HEAP_UNIT * N);
138 stack = ( UCHAR *** )malloc(sizeof (UCHAR * *) );
139 stack[0] = ( UCHAR ** )malloc(HEAP_UNIT * sizeof (UCHAR *) );
141 stacksize = HEAP_UNIT;
142 available = HEAP_UNIT;
144 for (int i = 0; i < HEAP_UNIT; i++)
146 stack[0][i] = (data[0] + i * N);
156 for (i = 0; i < datablocknum; i++)
160 for (i = 0; i < stackblocknum; i++)
175 allocateDataBlock( );
178 // printf("Allocating %d\n", header[ allocated ]) ;
180 return (void *)stack[available >> HEAP_BASE][available & HEAP_MASK];
184 * De-allocation method
186 void deallocate(void *obj)
188 if (available == stacksize)
190 allocateStackBlock( );
193 // printf("De-allocating %d\n", ( obj - data ) / N ) ;
194 stack[available >> HEAP_BASE][available & HEAP_MASK] = (UCHAR *)obj;
196 // printf("%d %d\n", allocated, header[ allocated ]) ;
204 printf("Bytes: %d Used: %d Allocated: %d Maxfree: %d\n", getBytes(), getAllocated(), getAll(), stacksize);
212 return HEAP_UNIT * datablocknum - available;
217 return HEAP_UNIT * datablocknum;
225 #ifdef WITH_CXX_GUARDEDALLOC
226 MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:MemoryAllocator")
231 #endif /* __MEMORYALLOCATOR_H__ */