view vmalloc.c @ 142:1fe7ba1ab81f

bugfixes and typos
author Merten Sach <msach@mailbox.tu-berlin.de>
date Wed, 21 Sep 2011 12:35:11 +0200
parents d0aa5a796fc5
children c3f458403cd6
line source
1 /*
2 * Copyright 2009 OpenSourceCodeStewardshipFoundation.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 * Created on November 14, 2009, 9:07 PM
8 */
10 #include <malloc.h>
11 #include <inttypes.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <math.h>
17 #include "VMS.h"
18 #include "Histogram/Histogram.h"
20 #define MAX_UINT64 0xFFFFFFFFFFFFFFFF
22 //A MallocProlog is a head element if the HigherInMem variable is NULL
23 //A Chunk is free if the prevChunkInFreeList variable is NULL
25 /*
26 * This calculates the container which fits the given size.
27 */
28 inline
29 uint32 getContainer(size_t size)
30 {
31 return (log2(size)-LOG128)/LOG54;
32 }
34 /*
35 * Removes the first chunk of a freeList
36 * The chunk is removed but not set as free. There is no check if
37 * the free list is empty, so make sure this is not the case.
38 */
39 inline
40 MallocProlog *removeChunk(MallocArrays* freeLists, uint32 containerIdx)
41 {
42 MallocProlog** container = &freeLists->bigChunks[containerIdx];
43 MallocProlog* removedChunk = *container;
44 *container = removedChunk->nextChunkInFreeList;
46 if(removedChunk->nextChunkInFreeList)
47 removedChunk->nextChunkInFreeList->prevChunkInFreeList =
48 (MallocProlog*)container;
50 if(*container == NULL)
51 {
52 if(containerIdx < 64)
53 freeLists->bigChunksSearchVector[0] &= ~((uint64)1 << containerIdx);
54 else
55 freeLists->bigChunksSearchVector[1] &= ~((uint64)1 << (containerIdx-64));
56 }
58 return removedChunk;
59 }
61 /*
62 * Removes the first chunk of a freeList
63 * The chunk is removed but not set as free. There is no check if
64 * the free list is empty, so make sure this is not the case.
65 */
66 inline
67 MallocProlog *removeSmallChunk(MallocArrays* freeLists, uint32 containerIdx)
68 {
69 MallocProlog** container = &freeLists->smallChunks[containerIdx];
70 MallocProlog* removedChunk = *container;
71 *container = removedChunk->nextChunkInFreeList;
73 if(removedChunk->nextChunkInFreeList)
74 removedChunk->nextChunkInFreeList->prevChunkInFreeList =
75 (MallocProlog*)container;
77 return removedChunk;
78 }
80 inline
81 size_t getChunkSize(MallocProlog* chunk)
82 {
83 return (uintptr_t)chunk->nextHigherInMem -
84 (uintptr_t)chunk - sizeof(MallocProlog);
85 }
87 /*
88 * Removes a chunk from a free list.
89 */
90 inline
91 void extractChunk(MallocProlog* chunk, MallocArrays *freeLists)
92 {
93 chunk->prevChunkInFreeList->nextChunkInFreeList = chunk->nextChunkInFreeList;
94 if(chunk->nextChunkInFreeList)
95 chunk->nextChunkInFreeList->prevChunkInFreeList = chunk->prevChunkInFreeList;
97 //The last element in the list points to the container. If the container points
98 //to NULL the container is empty
99 if(*((void**)(chunk->prevChunkInFreeList)) == NULL && getChunkSize(chunk) >= BIG_LOWER_BOUND)
100 {
101 //Find the approppiate container because we do not know it
102 uint64 containerIdx = ((uintptr_t)chunk->prevChunkInFreeList - (uintptr_t)freeLists->bigChunks) >> 3;
103 if(containerIdx < (uint32)64)
104 freeLists->bigChunksSearchVector[0] &= ~((uint64)1 << containerIdx);
105 if(containerIdx < 128 && containerIdx >=64)
106 freeLists->bigChunksSearchVector[1] &= ~((uint64)1 << (containerIdx-64));
108 }
109 }
111 /*
112 * Merges two chunks.
113 * Chunk A has to be before chunk B in memory. Both have to be removed from
114 * a free list
115 */
116 inline
117 MallocProlog *mergeChunks(MallocProlog* chunkA, MallocProlog* chunkB)
118 {
119 chunkA->nextHigherInMem = chunkB->nextHigherInMem;
120 chunkB->nextHigherInMem->nextLowerInMem = chunkA;
121 return chunkA;
122 }
123 /*
124 * Inserts a chunk into a free list.
125 */
126 inline
127 void insertChunk(MallocProlog* chunk, MallocProlog** container)
128 {
129 chunk->nextChunkInFreeList = *container;
130 chunk->prevChunkInFreeList = (MallocProlog*)container;
131 if(*container)
132 (*container)->prevChunkInFreeList = chunk;
133 *container = chunk;
134 }
136 /*
137 * Divides the chunk that a new chunk of newSize is created.
138 * There is no size check, so make sure the size value is valid.
139 */
140 inline
141 MallocProlog *divideChunk(MallocProlog* chunk, size_t newSize)
142 {
143 MallocProlog* newChunk = (MallocProlog*)((uintptr_t)chunk->nextHigherInMem -
144 newSize - sizeof(MallocProlog));
146 newChunk->nextLowerInMem = chunk;
147 newChunk->nextHigherInMem = chunk->nextHigherInMem;
149 chunk->nextHigherInMem->nextLowerInMem = newChunk;
150 chunk->nextHigherInMem = newChunk;
152 return newChunk;
153 }
155 /*
156 * Search for chunk in the list of big chunks. Split the block if it's too big
157 */
158 inline
159 MallocProlog *searchChunk(MallocArrays *freeLists, size_t sizeRequested, uint32 containerIdx)
160 {
161 MallocProlog* foundChunk;
163 uint64 searchVector = freeLists->bigChunksSearchVector[0];
164 //set small chunk bits to zero
165 searchVector &= MAX_UINT64 << containerIdx;
166 containerIdx = __builtin_ffsl(searchVector);
168 if(containerIdx == 0)
169 {
170 searchVector = freeLists->bigChunksSearchVector[1];
171 containerIdx = __builtin_ffsl(searchVector);
172 if(containerIdx == 0)
173 {
174 printf("VMS malloc failed: low memory");
175 exit(1);
176 }
177 containerIdx += 64;
178 }
179 containerIdx--;
182 foundChunk = removeChunk(freeLists, containerIdx);
183 size_t chunkSize = getChunkSize(foundChunk);
185 //If the new chunk is larger than the requested size: split
186 if(chunkSize > sizeRequested + 2 * sizeof(MallocProlog) + BIG_LOWER_BOUND)
187 {
188 MallocProlog *newChunk = divideChunk(foundChunk,sizeRequested);
189 containerIdx = getContainer(getChunkSize(foundChunk)) - 1;
190 insertChunk(foundChunk,&freeLists->bigChunks[containerIdx]);
191 if(containerIdx < 64)
192 freeLists->bigChunksSearchVector[0] |= ((uint64)1 << containerIdx);
193 else
194 freeLists->bigChunksSearchVector[1] |= ((uint64)1 << (containerIdx-64));
195 foundChunk = newChunk;
196 }
198 return foundChunk;
199 }
202 /*
203 * This is sequential code, meant to only be called from the Master, not from
204 * any slave VPs.
205 */
206 void *VMS__malloc( size_t sizeRequested )
207 {
208 //============================= MEASUREMENT STUFF ========================
209 #ifdef MEAS__TIME_MALLOC
210 int32 startStamp, endStamp;
211 saveLowTimeStampCountInto( startStamp );
212 #endif
213 //========================================================================
215 MallocArrays* freeLists = _VMSMasterEnv->freeLists;
216 MallocProlog* foundChunk;
218 //Return a small chunk if the requested size is smaller than 128B
219 if(sizeRequested <= LOWER_BOUND)
220 {
221 uint32 freeListIdx = (sizeRequested-1)/SMALL_CHUNK_SIZE;
222 if(freeLists->smallChunks[freeListIdx] == NULL)
223 foundChunk = searchChunk(freeLists, SMALL_CHUNK_SIZE*(freeListIdx+1), 0);
224 else
225 foundChunk = removeSmallChunk(freeLists, freeListIdx);
227 //Mark as allocated
228 foundChunk->prevChunkInFreeList = NULL;
229 return foundChunk + 1;
230 }
232 //Calculate the expected container. Start one higher to have a Chunk that's
233 //always big enough.
234 uint32 containerIdx = getContainer(sizeRequested);
236 if(freeLists->bigChunks[containerIdx] == NULL)
237 foundChunk = searchChunk(freeLists, sizeRequested, containerIdx);
238 else
239 foundChunk = removeChunk(freeLists, containerIdx);
241 //Mark as allocated
242 foundChunk->prevChunkInFreeList = NULL;
244 //============================= MEASUREMENT STUFF ========================
245 #ifdef MEAS__TIME_MALLOC
246 saveLowTimeStampCountInto( endStamp );
247 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
248 #endif
249 //========================================================================
251 //skip over the prolog by adding its size to the pointer return
252 return foundChunk + 1;
253 }
255 /*
256 * This is sequential code, meant to only be called from the Master, not from
257 * any slave VPs.
258 */
259 void
260 VMS__free( void *ptrToFree )
261 {
263 //============================= MEASUREMENT STUFF ========================
264 #ifdef MEAS__TIME_MALLOC
265 int32 startStamp, endStamp;
266 saveLowTimeStampCountInto( startStamp );
267 #endif
268 //========================================================================
270 MallocArrays* freeLists = _VMSMasterEnv->freeLists;
271 MallocProlog *chunkToFree = (MallocProlog*)ptrToFree - 1;
272 uint32 containerIdx;
274 //Check for free neighbors
275 if(chunkToFree->nextLowerInMem)
276 {
277 if(chunkToFree->nextLowerInMem->prevChunkInFreeList != NULL)
278 {//Chunk is not allocated
279 extractChunk(chunkToFree->nextLowerInMem, freeLists);
280 chunkToFree = mergeChunks(chunkToFree->nextLowerInMem, chunkToFree);
281 }
282 }
283 if(chunkToFree->nextHigherInMem)
284 {
285 if(chunkToFree->nextHigherInMem->prevChunkInFreeList != NULL)
286 {//Chunk is not allocated
287 extractChunk(chunkToFree->nextHigherInMem, freeLists);
288 chunkToFree = mergeChunks(chunkToFree, chunkToFree->nextHigherInMem);
289 }
290 }
292 size_t chunkSize = getChunkSize(chunkToFree);
293 if(chunkSize < BIG_LOWER_BOUND)
294 {
295 containerIdx = (chunkSize/SMALL_CHUNK_SIZE)-1;
296 if(containerIdx > SMALL_CHUNK_COUNT-1)
297 containerIdx = SMALL_CHUNK_COUNT-1;
298 insertChunk(chunkToFree, &freeLists->smallChunks[containerIdx]);
299 }
300 else
301 {
302 containerIdx = getContainer(getChunkSize(chunkToFree)) - 1;
303 insertChunk(chunkToFree, &freeLists->bigChunks[containerIdx]);
304 if(containerIdx < 64)
305 freeLists->bigChunksSearchVector[0] |= (uint64)1 << containerIdx;
306 else
307 freeLists->bigChunksSearchVector[1] |= (uint64)1 << (containerIdx-64);
308 }
310 //============================= MEASUREMENT STUFF ========================
311 #ifdef MEAS__TIME_MALLOC
312 saveLowTimeStampCountInto( endStamp );
313 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->freeTimeHist );
314 #endif
315 //========================================================================
317 }
319 /*
320 * Designed to be called from the main thread outside of VMS, during init
321 */
322 MallocArrays *
323 VMS_ext__create_free_list()
324 {
325 //Initialize containers for small chunks and fill with zeros
326 _VMSMasterEnv->freeLists = (MallocArrays*)malloc( sizeof(MallocArrays) );
327 MallocArrays *freeLists = _VMSMasterEnv->freeLists;
329 freeLists->smallChunks =
330 (MallocProlog**)malloc(SMALL_CHUNK_COUNT*sizeof(MallocProlog*));
331 memset((void*)freeLists->smallChunks,
332 0,SMALL_CHUNK_COUNT*sizeof(MallocProlog*));
334 //Calculate number of containers for big chunks
335 uint32 container = getContainer(MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE)+1;
336 freeLists->bigChunks = (MallocProlog**)malloc(container*sizeof(MallocProlog*));
337 memset((void*)freeLists->bigChunks,0,container*sizeof(MallocProlog*));
338 freeLists->containerCount = container;
340 //Create first element in lastContainer
341 MallocProlog *firstChunk = malloc( MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE );
342 if( firstChunk == NULL ) {printf("Can't allocate initial memory\n"); exit(1);}
343 freeLists->memSpace = firstChunk;
345 //Touch memory to avoid page faults
346 void *ptr,*endPtr;
347 endPtr = (void*)firstChunk+MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE;
348 for(ptr = firstChunk; ptr < endPtr; ptr+=PAGE_SIZE)
349 {
350 *(char*)ptr = 0;
351 }
353 firstChunk->nextLowerInMem = NULL;
354 firstChunk->nextHigherInMem = (MallocProlog*)((uintptr_t)firstChunk +
355 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE - sizeof(MallocProlog));
356 firstChunk->nextChunkInFreeList = NULL;
357 //previous element in the queue is the container
358 firstChunk->prevChunkInFreeList = &freeLists->bigChunks[container-2];
360 freeLists->bigChunks[container-2] = firstChunk;
361 //Insert into bit search list
362 if(container <= 65)
363 {
364 freeLists->bigChunksSearchVector[0] = ((uint64)1 << (container-2));
365 freeLists->bigChunksSearchVector[1] = 0;
366 }
367 else
368 {
369 freeLists->bigChunksSearchVector[0] = 0;
370 freeLists->bigChunksSearchVector[1] = ((uint64)1 << (container-66));
371 }
373 //Create dummy chunk to mark the top of stack this is of course
374 //never freed
375 MallocProlog *dummyChunk = firstChunk->nextHigherInMem;
376 dummyChunk->nextHigherInMem = dummyChunk+1;
377 dummyChunk->nextLowerInMem = NULL;
378 dummyChunk->nextChunkInFreeList = NULL;
379 dummyChunk->prevChunkInFreeList = NULL;
381 return freeLists;
382 }
385 /*Designed to be called from the main thread outside of VMS, during cleanup
386 */
387 void
388 VMS_ext__free_free_list( MallocArrays *freeLists )
389 {
390 free(freeLists->memSpace);
391 free(freeLists->bigChunks);
392 free(freeLists->smallChunks);
394 }