view vmalloc.c @ 81:a214c84dff4e

malloc_touch
author Merten Sach <msach@mailbox.tu-berlin.de>
date Mon, 11 Jul 2011 20:35:54 +0200
parents 97e26095c01f
children 21c95d402fe6
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>
15 #include "VMS.h"
16 #include "Histogram/Histogram.h"
18 /*Helper function
19 *Insert a newly generated free chunk into the first spot on the free list.
20 * The chunk is cast as a MallocProlog, so the various pointers in it are
21 * accessed with C's help -- and the size of the prolog is easily added to
22 * the pointer when a chunk is returned to the app -- so C handles changes
23 * in pointer sizes among machines.
24 *
25 *The list head is a normal MallocProlog struct -- identified by its
26 * prevChunkInFreeList being NULL -- the only one.
27 *
28 *The end of the list is identified by next chunk being NULL, as usual.
29 */
30 void inline
31 add_chunk_to_free_list( MallocProlog *chunk, MallocProlog *listHead )
32 {
33 chunk->nextChunkInFreeList = listHead->nextChunkInFreeList;
34 if( chunk->nextChunkInFreeList != NULL ) //if not last in free list
35 chunk->nextChunkInFreeList->prevChunkInFreeList = chunk;
36 chunk->prevChunkInFreeList = listHead;
37 listHead->nextChunkInFreeList = chunk;
38 }
41 /*This is sequential code, meant to only be called from the Master, not from
42 * any slave VPs.
43 *Search down list, checking size by the nextHigherInMem pointer, to find
44 * first chunk bigger than size needed.
45 *Shave off the extra and make it into a new free-list element, hook it in
46 * then return the address of the found element plus size of prolog.
47 *
48 *Will find a
49 */
50 void *VMS__malloc( size_t sizeRequested )
51 { MallocProlog *foundElem = NULL, *currElem, *newElem;
52 ssize_t amountExtra, sizeConsumed,sizeOfFound;
53 uint32 foundElemIsTopOfHeap;
55 //============================= MEASUREMENT STUFF ========================
56 #ifdef MEAS__TIME_MALLOC
57 int32 startStamp, endStamp;
58 saveLowTimeStampCountInto( startStamp );
59 #endif
60 //========================================================================
62 //step up the size to be aligned at 16-byte boundary, prob better ways
63 sizeRequested = (sizeRequested + 16) & ~15;
64 currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList;
66 while( currElem != NULL )
67 { //check if size of currElem is big enough
68 sizeOfFound=(size_t)((uintptr_t)currElem->nextHigherInMem -(uintptr_t)currElem);
69 amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
70 if( amountExtra > 0 )
71 { //found it, get out of loop
72 foundElem = currElem;
73 currElem = NULL;
74 }
75 else
76 currElem = currElem->nextChunkInFreeList;
77 }
79 if( foundElem == NULL )
80 { ERROR("\nmalloc failed\n")
81 return (void *)NULL; //indicates malloc failed
82 }
83 //Using a kludge to identify the element that is the top chunk in the
84 // heap -- saving top-of-heap addr in head's nextHigherInMem -- and
85 // save addr of start of heap in head's nextLowerInMem
86 //Will handle top of Heap specially
87 foundElemIsTopOfHeap = foundElem->nextHigherInMem ==
88 _VMSMasterEnv->freeListHead->nextHigherInMem;
90 //before shave off and try to insert new elem, remove found elem
91 //note, foundElem will never be the head, so always has valid prevChunk
92 foundElem->prevChunkInFreeList->nextChunkInFreeList =
93 foundElem->nextChunkInFreeList;
94 if( foundElem->nextChunkInFreeList != NULL )
95 { foundElem->nextChunkInFreeList->prevChunkInFreeList =
96 foundElem->prevChunkInFreeList;
97 }
98 foundElem->prevChunkInFreeList = NULL;//indicates elem currently allocated
100 //if enough, turn extra into new elem & insert it
101 if( amountExtra > 64 )
102 { //make new elem by adding to addr of curr elem then casting
103 sizeConsumed = sizeof(MallocProlog) + sizeRequested;
104 newElem = (MallocProlog *)( (uintptr_t)foundElem + sizeConsumed );
105 newElem->nextLowerInMem = foundElem; //This is evil (but why?)
106 newElem->nextHigherInMem = foundElem->nextHigherInMem; //This is evil (but why?)
107 foundElem->nextHigherInMem = newElem;
108 if( ! foundElemIsTopOfHeap )
109 { //there is no next higher for top of heap, so can't write to it
110 newElem->nextHigherInMem->nextLowerInMem = newElem;
111 }
112 add_chunk_to_free_list( newElem, _VMSMasterEnv->freeListHead );
113 }
114 else
115 {
116 sizeConsumed = sizeOfFound;
117 }
118 _VMSMasterEnv->amtOfOutstandingMem += sizeConsumed;
120 //============================= MEASUREMENT STUFF ========================
121 #ifdef MEAS__TIME_MALLOC
122 saveLowTimeStampCountInto( endStamp );
123 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
124 #endif
125 //========================================================================
127 //skip over the prolog by adding its size to the pointer return
128 return (void*)((uintptr_t)foundElem + sizeof(MallocProlog));
129 }
131 /*This is sequential code, meant to only be called from the Master, not from
132 * any slave VPs.
133 *Search down list, checking size by the nextHigherInMem pointer, to find
134 * first chunk bigger than size needed.
135 *Shave off the extra and make it into a new free-list element, hook it in
136 * then return the address of the found element plus size of prolog.
137 *
138 * The difference to the regular malloc is, that all the allocated chunks are
139 * aligned and padded to the size of a CACHE_LINE. Thus creating a new chunk
140 * before the aligned chunk.
141 */
142 void *VMS__malloc_aligned( size_t sizeRequested )
143 { MallocProlog *foundElem = NULL, *currElem, *newElem;
144 ssize_t amountExtra, sizeConsumed,sizeOfFound,prevAmount;
145 uint32 foundElemIsTopOfHeap;
147 //============================= MEASUREMENT STUFF ========================
148 #ifdef MEAS__TIME_MALLOC
149 uint32 startStamp, endStamp;
150 saveLowTimeStampCountInto( startStamp );
151 #endif
152 //========================================================================
154 //step up the size to be multiple of the cache line size
155 sizeRequested = (sizeRequested + CACHE_LINE) & ~(CACHE_LINE-1);
156 currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList;
158 while( currElem != NULL )
159 { //check if size of currElem is big enough
160 sizeOfFound=(size_t)((uintptr_t)currElem->nextHigherInMem -(uintptr_t)currElem);
161 amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
162 if( amountExtra > 0 )
163 {
164 //look if the found element is already aligned
165 if((((uintptr_t)currElem+sizeof(MallocProlog)) & (uintptr_t)(CACHE_LINE-1)) == 0){
166 //found it, get out of loop
167 foundElem = currElem;
168 break;
169 }else{
170 //find first aligned address and check if it's still big enough
171 //check also if the space before the aligned address is big enough
172 //for a new element
173 void *firstAlignedAddr = (void*)(((uintptr_t)currElem + 2*CACHE_LINE) & ~((uintptr_t)(CACHE_LINE-1)));
174 prevAmount = (uintptr_t)firstAlignedAddr - (uintptr_t)currElem;
175 sizeOfFound=(uintptr_t)currElem->nextHigherInMem -(uintptr_t)firstAlignedAddr + sizeof(MallocProlog);
176 amountExtra= sizeOfFound - sizeRequested - sizeof(MallocProlog);
177 if(prevAmount > 2*sizeof(MallocProlog) && amountExtra > 0 ){
178 //found suitable element
179 //create new previous element and exit loop
180 MallocProlog *newAlignedElem = (MallocProlog*)firstAlignedAddr - 1;
182 //insert new element into free list
183 if(currElem->nextChunkInFreeList != NULL)
184 currElem->nextChunkInFreeList->prevChunkInFreeList = newAlignedElem;
185 newAlignedElem->prevChunkInFreeList = currElem;
186 newAlignedElem->nextChunkInFreeList = currElem->nextChunkInFreeList;
187 currElem->nextChunkInFreeList = newAlignedElem;
189 //set higherInMem and lowerInMem
190 newAlignedElem->nextHigherInMem = currElem->nextHigherInMem;
191 foundElemIsTopOfHeap = currElem->nextHigherInMem ==
192 _VMSMasterEnv->freeListHead->nextHigherInMem;
193 if(!foundElemIsTopOfHeap)
194 currElem->nextHigherInMem->nextLowerInMem = newAlignedElem;
195 currElem->nextHigherInMem = newAlignedElem;
196 newAlignedElem->nextLowerInMem = currElem;
198 //Found new element leaving loop
199 foundElem = newAlignedElem;
200 break;
201 }
202 }
204 }
205 currElem = currElem->nextChunkInFreeList;
206 }
208 if( foundElem == NULL )
209 { ERROR("\nmalloc failed\n")
210 return (void *)NULL; //indicates malloc failed
211 }
212 //Using a kludge to identify the element that is the top chunk in the
213 // heap -- saving top-of-heap addr in head's nextHigherInMem -- and
214 // save addr of start of heap in head's nextLowerInMem
215 //Will handle top of Heap specially
216 foundElemIsTopOfHeap = foundElem->nextHigherInMem ==
217 _VMSMasterEnv->freeListHead->nextHigherInMem;
219 //before shave off and try to insert new elem, remove found elem
220 //note, foundElem will never be the head, so always has valid prevChunk
221 foundElem->prevChunkInFreeList->nextChunkInFreeList =
222 foundElem->nextChunkInFreeList;
223 if( foundElem->nextChunkInFreeList != NULL )
224 { foundElem->nextChunkInFreeList->prevChunkInFreeList =
225 foundElem->prevChunkInFreeList;
226 }
227 foundElem->prevChunkInFreeList = NULL;//indicates elem currently allocated
229 //if enough, turn extra into new elem & insert it
230 if( amountExtra > 64 )
231 { //make new elem by adding to addr of curr elem then casting
232 sizeConsumed = sizeof(MallocProlog) + sizeRequested;
233 newElem = (MallocProlog *)( (uintptr_t)foundElem + sizeConsumed );
234 newElem->nextHigherInMem = foundElem->nextHigherInMem;
235 newElem->nextLowerInMem = foundElem;
236 foundElem->nextHigherInMem = newElem;
238 if( ! foundElemIsTopOfHeap )
239 { //there is no next higher for top of heap, so can't write to it
240 newElem->nextHigherInMem->nextLowerInMem = newElem;
241 }
242 add_chunk_to_free_list( newElem, _VMSMasterEnv->freeListHead );
243 }
244 else
245 {
246 sizeConsumed = sizeOfFound;
247 }
248 _VMSMasterEnv->amtOfOutstandingMem += sizeConsumed;
250 //============================= MEASUREMENT STUFF ========================
251 #ifdef MEAS__TIME_MALLOC
252 saveLowTimeStampCountInto( endStamp );
253 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
254 #endif
255 //========================================================================
257 //skip over the prolog by adding its size to the pointer return
258 return (void*)((uintptr_t)foundElem + sizeof(MallocProlog));
259 }
262 /*This is sequential code -- only to be called from the Master
263 * When free, subtract the size of prolog from pointer, then cast it to a
264 * MallocProlog. Then check the nextLower and nextHigher chunks to see if
265 * one or both are also free, and coalesce if so, and if neither free, then
266 * add this one to free-list.
267 */
268 void
269 VMS__free( void *ptrToFree )
270 { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem;
271 size_t sizeOfElem;
272 uint32 lowerExistsAndIsFree, higherExistsAndIsFree;
274 //============================= MEASUREMENT STUFF ========================
275 #ifdef MEAS__TIME_MALLOC
276 int32 startStamp, endStamp;
277 saveLowTimeStampCountInto( startStamp );
278 #endif
279 //========================================================================
281 if( ptrToFree < (void*)_VMSMasterEnv->freeListHead->nextLowerInMem ||
282 ptrToFree > (void*)_VMSMasterEnv->freeListHead->nextHigherInMem )
283 { //outside the range of data owned by VMS's malloc, so do nothing
284 return;
285 }
286 //subtract size of prolog to get pointer to prolog, then cast
287 elemToFree = (MallocProlog *)((uintptr_t)ptrToFree - sizeof(MallocProlog));
288 sizeOfElem =(size_t)((uintptr_t)elemToFree->nextHigherInMem-(uintptr_t)elemToFree);
290 if( elemToFree->prevChunkInFreeList != NULL )
291 { printf( "error: freeing same element twice!" ); exit(1);
292 }
294 _VMSMasterEnv->amtOfOutstandingMem -= sizeOfElem;
296 nextLowerElem = elemToFree->nextLowerInMem;
297 nextHigherElem = elemToFree->nextHigherInMem;
299 if( nextHigherElem == NULL )
300 higherExistsAndIsFree = FALSE;
301 else //okay exists, now check if in the free-list by checking back ptr
302 higherExistsAndIsFree = (nextHigherElem->prevChunkInFreeList != NULL);
304 if( nextLowerElem == NULL )
305 lowerExistsAndIsFree = FALSE;
306 else //okay, it exists, now check if it's free
307 lowerExistsAndIsFree = (nextLowerElem->prevChunkInFreeList != NULL);
310 //now, know what exists and what's free
311 if( lowerExistsAndIsFree )
312 { if( higherExistsAndIsFree )
313 { //both exist and are free, so coalesce all three
314 //First, remove higher from free-list
315 nextHigherElem->prevChunkInFreeList->nextChunkInFreeList =
316 nextHigherElem->nextChunkInFreeList;
317 if( nextHigherElem->nextChunkInFreeList != NULL ) //end-of-list?
318 nextHigherElem->nextChunkInFreeList->prevChunkInFreeList =
319 nextHigherElem->prevChunkInFreeList;
320 //Now, fix-up sequence-in-mem list -- by side-effect, this also
321 // changes size of the lower elem, which is still in free-list
322 nextLowerElem->nextHigherInMem = nextHigherElem->nextHigherInMem;
323 if( nextHigherElem->nextHigherInMem !=
324 _VMSMasterEnv->freeListHead->nextHigherInMem )
325 nextHigherElem->nextHigherInMem->nextLowerInMem = nextLowerElem;
326 //notice didn't do anything to elemToFree -- it simply is no
327 // longer reachable from any of the lists. Wonder if could be a
328 // security leak because left valid addresses in it,
329 // but don't care for now.
330 }
331 else
332 { //lower is the only of the two that exists and is free,
333 //In this case, no adjustment to free-list, just change mem-list.
334 // By side-effect, changes size of the lower elem
335 nextLowerElem->nextHigherInMem = elemToFree->nextHigherInMem;
336 if( elemToFree->nextHigherInMem !=
337 _VMSMasterEnv->freeListHead->nextHigherInMem )
338 elemToFree->nextHigherInMem->nextLowerInMem = nextLowerElem;
339 }
340 }
341 else
342 { //lower either doesn't exist or isn't free, so check higher
343 if( higherExistsAndIsFree )
344 { //higher exists and is the only of the two free
345 //First, in free-list, replace higher elem with the one to free
346 elemToFree->nextChunkInFreeList=nextHigherElem->nextChunkInFreeList;
347 elemToFree->prevChunkInFreeList=nextHigherElem->prevChunkInFreeList;
348 elemToFree->prevChunkInFreeList->nextChunkInFreeList = elemToFree;
349 if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list?
350 elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree;
351 //Now chg mem-list. By side-effect, changes size of elemToFree
352 elemToFree->nextHigherInMem = nextHigherElem->nextHigherInMem;
353 if( elemToFree->nextHigherInMem !=
354 _VMSMasterEnv->freeListHead->nextHigherInMem )
355 elemToFree->nextHigherInMem->nextLowerInMem = elemToFree;
356 }
357 else
358 { //neither lower nor higher is availabe to coalesce so add to list
359 // this makes prev chunk ptr non-null, which indicates it's free
360 elemToFree->nextChunkInFreeList =
361 _VMSMasterEnv->freeListHead->nextChunkInFreeList;
362 _VMSMasterEnv->freeListHead->nextChunkInFreeList = elemToFree;
363 if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list?
364 elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree;
365 elemToFree->prevChunkInFreeList = _VMSMasterEnv->freeListHead;
366 }
367 }
368 //============================= MEASUREMENT STUFF ========================
369 #ifdef MEAS__TIME_MALLOC
370 saveLowTimeStampCountInto( endStamp );
371 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->freeTimeHist );
372 #endif
373 //========================================================================
375 }
378 /*Allocates memory from the external system -- higher overhead
379 *
380 *Because of Linux's malloc throwing bizarre random faults when malloc is
381 * used inside a VMS virtual processor, have to pass this as a request and
382 * have the core loop do it when it gets around to it -- will look for these
383 * chores leftover from the previous animation of masterVP the next time it
384 * goes to animate the masterVP -- so it takes two separate masterVP
385 * animations, separated by work, to complete an external malloc or
386 * external free request.
387 *
388 *Thinking core loop accepts signals -- just looks if signal-location is
389 * empty or not --
390 */
391 void *
392 VMS__malloc_in_ext( size_t sizeRequested )
393 {
394 /*
395 //This is running in the master, so no chance for multiple cores to be
396 // competing for the core's flag.
397 if( *(_VMSMasterEnv->coreLoopSignalAddr[ 0 ]) != 0 )
398 { //something has already signalled to core loop, so save the signal
399 // and look, next time master animated, to see if can send it.
400 //Note, the addr to put a signal is in the coreloop's frame, so just
401 // checks it each time through -- make it volatile to avoid GCC
402 // optimizations -- it's a coreloop local var that only changes
403 // after jumping away. The signal includes the addr to send the
404 //return to -- even if just empty return completion-signal
405 //
406 //save the signal in some queue that the master looks at each time
407 // it starts up -- one loc says if empty for fast common case --
408 //something like that -- want to hide this inside this call -- but
409 // think this has to come as a request -- req handler gives procr
410 // back to master loop, which gives it back to req handler at point
411 // it sees that core loop has sent return signal. Something like
412 // that.
413 saveTheSignal
415 }
416 coreSigData->type = malloc;
417 coreSigData->sizeToMalloc = sizeRequested;
418 coreSigData->locToSignalCompletion = &figureOut;
419 _VMSMasterEnv->coreLoopSignals[ 0 ] = coreSigData;
420 */
421 //just risk system-stack faults until get this figured out
422 return malloc( sizeRequested );
423 }
426 /*Frees memory that was allocated in the external system -- higher overhead
427 *
428 *As noted in external malloc comment, this is clunky 'cause the free has
429 * to be called in the core loop.
430 */
431 void
432 VMS__free_in_ext( void *ptrToFree )
433 {
434 //just risk system-stack faults until get this figured out
435 free( ptrToFree );
437 //TODO: fix this -- so
438 }
441 /*Designed to be called from the main thread outside of VMS, during init
442 */
443 MallocProlog *
444 VMS_ext__create_free_list()
445 { MallocProlog *freeListHead, *firstChunk;
447 //Note, this is running in the main thread -- all increases in malloc
448 // mem and all frees of it must be done in this thread, with the
449 // thread's original stack available
450 freeListHead = malloc( sizeof(MallocProlog) );
451 firstChunk = malloc( MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE );
452 if( firstChunk == NULL ) {printf("malloc error\n"); exit(1);}
454 Touch memory to avoid page faults
455 void *ptr,*endPtr;
456 endPtr = (void*)firstChunk+MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE;
457 for(ptr = firstChunk; ptr < endPtr; ptr+=PAGE_SIZE)
458 {
459 *(char*)ptr = 0;
460 }
462 freeListHead->prevChunkInFreeList = NULL;
463 //Use this addr to free the heap when cleanup
464 freeListHead->nextLowerInMem = firstChunk;
465 //to identify top-of-heap elem, compare this addr to elem's next higher
466 freeListHead->nextHigherInMem = (void*)( (uintptr_t)firstChunk +
467 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
468 freeListHead->nextChunkInFreeList = firstChunk;
470 firstChunk->nextChunkInFreeList = NULL;
471 firstChunk->prevChunkInFreeList = freeListHead;
472 //next Higher has to be set to top of chunk, so can calc size in malloc
473 firstChunk->nextHigherInMem = (void*)( (uintptr_t)firstChunk +
474 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
475 firstChunk->nextLowerInMem = NULL; //identifies as bott of heap
477 _VMSMasterEnv->amtOfOutstandingMem = 0; //none allocated yet
479 return freeListHead;
480 }
483 /*Designed to be called from the main thread outside of VMS, during cleanup
484 */
485 void
486 VMS_ext__free_free_list( MallocProlog *freeListHead )
487 {
488 //stashed a ptr to the one and only bug chunk malloc'd from OS in the
489 // free list head's next lower in mem pointer
490 free( freeListHead->nextLowerInMem );
492 //don't free the head -- it'll be in an array eventually -- free whole
493 // array when all the free lists linked from it have already been freed
494 }