annotate vmalloc.c @ 86:ef4cb0ded940

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