view vmalloc.c @ 174:c3f458403cd6

Always allocate more than 256 byte, measurements removed in master loop, aligned slots
author Merten Sach <msach@mailbox.tu-berlin.de>
date Tue, 20 Dec 2011 16:50:21 +0100
parents 1fe7ba1ab81f
children de5e7c522f1f
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 sizeRequested += CACHELINE_SIZE; //Allocate more than cacheline_size to avoid false sharing
217 MallocArrays* freeLists = _VMSMasterEnv->freeLists;
218 MallocProlog* foundChunk;
220 //Return a small chunk if the requested size is smaller than 128B
221 if(sizeRequested <= LOWER_BOUND)
222 {
223 uint32 freeListIdx = (sizeRequested-1)/SMALL_CHUNK_SIZE;
224 if(freeLists->smallChunks[freeListIdx] == NULL)
225 foundChunk = searchChunk(freeLists, SMALL_CHUNK_SIZE*(freeListIdx+1), 0);
226 else
227 foundChunk = removeSmallChunk(freeLists, freeListIdx);
229 //Mark as allocated
230 foundChunk->prevChunkInFreeList = NULL;
231 return foundChunk + 1;
232 }
234 //Calculate the expected container. Start one higher to have a Chunk that's
235 //always big enough.
236 uint32 containerIdx = getContainer(sizeRequested);
238 if(freeLists->bigChunks[containerIdx] == NULL)
239 foundChunk = searchChunk(freeLists, sizeRequested, containerIdx);
240 else
241 foundChunk = removeChunk(freeLists, containerIdx);
243 //Mark as allocated
244 foundChunk->prevChunkInFreeList = NULL;
246 //============================= MEASUREMENT STUFF ========================
247 #ifdef MEAS__TIME_MALLOC
248 saveLowTimeStampCountInto( endStamp );
249 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
250 #endif
251 //========================================================================
253 //skip over the prolog by adding its size to the pointer return
254 return foundChunk + 1;
255 }
257 /*
258 * This is sequential code, meant to only be called from the Master, not from
259 * any slave VPs.
260 */
261 void
262 VMS__free( void *ptrToFree )
263 {
265 //============================= MEASUREMENT STUFF ========================
266 #ifdef MEAS__TIME_MALLOC
267 int32 startStamp, endStamp;
268 saveLowTimeStampCountInto( startStamp );
269 #endif
270 //========================================================================
272 MallocArrays* freeLists = _VMSMasterEnv->freeLists;
273 MallocProlog *chunkToFree = (MallocProlog*)ptrToFree - 1;
274 uint32 containerIdx;
276 //Check for free neighbors
277 if(chunkToFree->nextLowerInMem)
278 {
279 if(chunkToFree->nextLowerInMem->prevChunkInFreeList != NULL)
280 {//Chunk is not allocated
281 extractChunk(chunkToFree->nextLowerInMem, freeLists);
282 chunkToFree = mergeChunks(chunkToFree->nextLowerInMem, chunkToFree);
283 }
284 }
285 if(chunkToFree->nextHigherInMem)
286 {
287 if(chunkToFree->nextHigherInMem->prevChunkInFreeList != NULL)
288 {//Chunk is not allocated
289 extractChunk(chunkToFree->nextHigherInMem, freeLists);
290 chunkToFree = mergeChunks(chunkToFree, chunkToFree->nextHigherInMem);
291 }
292 }
294 size_t chunkSize = getChunkSize(chunkToFree);
295 if(chunkSize < BIG_LOWER_BOUND)
296 {
297 containerIdx = (chunkSize/SMALL_CHUNK_SIZE)-1;
298 if(containerIdx > SMALL_CHUNK_COUNT-1)
299 containerIdx = SMALL_CHUNK_COUNT-1;
300 insertChunk(chunkToFree, &freeLists->smallChunks[containerIdx]);
301 }
302 else
303 {
304 containerIdx = getContainer(getChunkSize(chunkToFree)) - 1;
305 insertChunk(chunkToFree, &freeLists->bigChunks[containerIdx]);
306 if(containerIdx < 64)
307 freeLists->bigChunksSearchVector[0] |= (uint64)1 << containerIdx;
308 else
309 freeLists->bigChunksSearchVector[1] |= (uint64)1 << (containerIdx-64);
310 }
312 //============================= MEASUREMENT STUFF ========================
313 #ifdef MEAS__TIME_MALLOC
314 saveLowTimeStampCountInto( endStamp );
315 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->freeTimeHist );
316 #endif
317 //========================================================================
319 }
321 /*
322 * Designed to be called from the main thread outside of VMS, during init
323 */
324 MallocArrays *
325 VMS_ext__create_free_list()
326 {
327 //Initialize containers for small chunks and fill with zeros
328 _VMSMasterEnv->freeLists = (MallocArrays*)malloc( sizeof(MallocArrays) );
329 MallocArrays *freeLists = _VMSMasterEnv->freeLists;
331 freeLists->smallChunks =
332 (MallocProlog**)malloc(SMALL_CHUNK_COUNT*sizeof(MallocProlog*));
333 memset((void*)freeLists->smallChunks,
334 0,SMALL_CHUNK_COUNT*sizeof(MallocProlog*));
336 //Calculate number of containers for big chunks
337 uint32 container = getContainer(MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE)+1;
338 freeLists->bigChunks = (MallocProlog**)malloc(container*sizeof(MallocProlog*));
339 memset((void*)freeLists->bigChunks,0,container*sizeof(MallocProlog*));
340 freeLists->containerCount = container;
342 //Create first element in lastContainer
343 MallocProlog *firstChunk = malloc( MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE );
344 if( firstChunk == NULL ) {printf("Can't allocate initial memory\n"); exit(1);}
345 freeLists->memSpace = firstChunk;
347 //Touch memory to avoid page faults
348 void *ptr,*endPtr;
349 endPtr = (void*)firstChunk+MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE;
350 for(ptr = firstChunk; ptr < endPtr; ptr+=PAGE_SIZE)
351 {
352 *(char*)ptr = 0;
353 }
355 firstChunk->nextLowerInMem = NULL;
356 firstChunk->nextHigherInMem = (MallocProlog*)((uintptr_t)firstChunk +
357 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE - sizeof(MallocProlog));
358 firstChunk->nextChunkInFreeList = NULL;
359 //previous element in the queue is the container
360 firstChunk->prevChunkInFreeList = &freeLists->bigChunks[container-2];
362 freeLists->bigChunks[container-2] = firstChunk;
363 //Insert into bit search list
364 if(container <= 65)
365 {
366 freeLists->bigChunksSearchVector[0] = ((uint64)1 << (container-2));
367 freeLists->bigChunksSearchVector[1] = 0;
368 }
369 else
370 {
371 freeLists->bigChunksSearchVector[0] = 0;
372 freeLists->bigChunksSearchVector[1] = ((uint64)1 << (container-66));
373 }
375 //Create dummy chunk to mark the top of stack this is of course
376 //never freed
377 MallocProlog *dummyChunk = firstChunk->nextHigherInMem;
378 dummyChunk->nextHigherInMem = dummyChunk+1;
379 dummyChunk->nextLowerInMem = NULL;
380 dummyChunk->nextChunkInFreeList = NULL;
381 dummyChunk->prevChunkInFreeList = NULL;
383 return freeLists;
384 }
387 /*Designed to be called from the main thread outside of VMS, during cleanup
388 */
389 void
390 VMS_ext__free_free_list( MallocArrays *freeLists )
391 {
392 free(freeLists->memSpace);
393 free(freeLists->bigChunks);
394 free(freeLists->smallChunks);
396 }