diff vmalloc.c @ 208:eaf7e4c58c9e

Create common_ancestor brch -- all branches will be closed, then new ones created with this as the common ancestor of all branches -- it is incomplete! only code that is common to all HW and Feat and FeatDev branches is in here
author Some Random Person <seanhalle@yahoo.com>
date Wed, 22 Feb 2012 11:39:12 -0800
parents
children 0c83ea8adefc
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vmalloc.c	Wed Feb 22 11:39:12 2012 -0800
     1.3 @@ -0,0 +1,494 @@
     1.4 +/*
     1.5 + *  Copyright 2009 OpenSourceCodeStewardshipFoundation.org
     1.6 + *  Licensed under GNU General Public License version 2
     1.7 + *
     1.8 + * Author: seanhalle@yahoo.com
     1.9 + *
    1.10 + * Created on November 14, 2009, 9:07 PM
    1.11 + */
    1.12 +
    1.13 +#include <malloc.h>
    1.14 +#include <inttypes.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <stdio.h>
    1.17 +
    1.18 +#include "VMS.h"
    1.19 +#include "C_Libraries/Histogram/Histogram.h"
    1.20 +
    1.21 +/*Helper function
    1.22 + *Insert a newly generated free chunk into the first spot on the free list.
    1.23 + * The chunk is cast as a MallocProlog, so the various pointers in it are
    1.24 + * accessed with C's help -- and the size of the prolog is easily added to
    1.25 + * the pointer when a chunk is returned to the app -- so C handles changes
    1.26 + * in pointer sizes among machines.
    1.27 + *
    1.28 + *The list head is a normal MallocProlog struct -- identified by its
    1.29 + * prevChunkInFreeList being NULL -- the only one.
    1.30 + *
    1.31 + *The end of the list is identified by next chunk being NULL, as usual.
    1.32 + */
    1.33 +void inline
    1.34 +add_chunk_to_free_list( MallocProlog *chunk, MallocProlog *listHead )
    1.35 + { 
    1.36 +   chunk->nextChunkInFreeList     = listHead->nextChunkInFreeList;
    1.37 +   if( chunk->nextChunkInFreeList != NULL ) //if not last in free list
    1.38 +      chunk->nextChunkInFreeList->prevChunkInFreeList = chunk;
    1.39 +   chunk->prevChunkInFreeList     = listHead;
    1.40 +   listHead->nextChunkInFreeList  = chunk;
    1.41 + }
    1.42 +
    1.43 +
    1.44 +/*This is sequential code, meant to only be called from the Master, not from
    1.45 + * any slave VPs.
    1.46 + *Search down list, checking size by the nextHigherInMem pointer, to find
    1.47 + * first chunk bigger than size needed.
    1.48 + *Shave off the extra and make it into a new free-list element, hook it in
    1.49 + * then return the address of the found element plus size of prolog.
    1.50 + *
    1.51 + */
    1.52 +void *VMS_int__malloc( size_t sizeRequested )
    1.53 + { MallocProlog *foundElem = NULL, *currElem, *newElem;
    1.54 +   ssize_t        amountExtra, sizeConsumed,sizeOfFound;
    1.55 +   uint32        foundElemIsTopOfHeap;
    1.56 +
    1.57 +   //============================= MEASUREMENT STUFF ========================
    1.58 +   #ifdef MEAS__TIME_MALLOC
    1.59 +   int32 startStamp, endStamp;
    1.60 +   saveLowTimeStampCountInto( startStamp );
    1.61 +   #endif
    1.62 +   //========================================================================
    1.63 +   
    1.64 +      //step up the size to be aligned at 16-byte boundary, prob better ways
    1.65 +   sizeRequested = (sizeRequested + 16) & ~15;
    1.66 +   currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList;
    1.67 +
    1.68 +   while( currElem != NULL )
    1.69 +    {    //check if size of currElem is big enough
    1.70 +      sizeOfFound=(size_t)((uintptr_t)currElem->nextHigherInMem -(uintptr_t)currElem);
    1.71 +      amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
    1.72 +      if( amountExtra > 0 )
    1.73 +       {    //found it, get out of loop
    1.74 +         foundElem = currElem;
    1.75 +         currElem = NULL;
    1.76 +       }
    1.77 +      else
    1.78 +         currElem = currElem->nextChunkInFreeList;
    1.79 +    }
    1.80 +   
    1.81 +   if( foundElem == NULL )
    1.82 +    { ERROR("\nmalloc failed\n")
    1.83 +      return (void *)NULL;  //indicates malloc failed
    1.84 +    }
    1.85 +      //Using a kludge to identify the element that is the top chunk in the
    1.86 +      // heap -- saving top-of-heap addr in head's nextHigherInMem -- and
    1.87 +      // save addr of start of heap in head's nextLowerInMem
    1.88 +      //Will handle top of Heap specially
    1.89 +   foundElemIsTopOfHeap = foundElem->nextHigherInMem ==
    1.90 +                          _VMSMasterEnv->freeListHead->nextHigherInMem;
    1.91 +   
    1.92 +      //before shave off and try to insert new elem, remove found elem
    1.93 +      //note, foundElem will never be the head, so always has valid prevChunk
    1.94 +   foundElem->prevChunkInFreeList->nextChunkInFreeList =
    1.95 +                                              foundElem->nextChunkInFreeList;
    1.96 +   if( foundElem->nextChunkInFreeList != NULL )
    1.97 +    { foundElem->nextChunkInFreeList->prevChunkInFreeList =
    1.98 +                                              foundElem->prevChunkInFreeList;
    1.99 +    }
   1.100 +   foundElem->prevChunkInFreeList = NULL;//indicates elem currently allocated
   1.101 +   
   1.102 +      //if enough, turn extra into new elem & insert it
   1.103 +   if( amountExtra > 64 )
   1.104 +    {   //make new elem by adding to addr of curr elem then casting
   1.105 +        sizeConsumed = sizeof(MallocProlog) + sizeRequested; 
   1.106 +        newElem = (MallocProlog *)( (uintptr_t)foundElem + sizeConsumed );
   1.107 +        newElem->nextLowerInMem    = foundElem; //This is evil (but why?) 
   1.108 +        newElem->nextHigherInMem   = foundElem->nextHigherInMem; //This is evil (but why?)
   1.109 +        foundElem->nextHigherInMem = newElem;
   1.110 +        if( ! foundElemIsTopOfHeap )
   1.111 +        {  //there is no next higher for top of heap, so can't write to it
   1.112 +           newElem->nextHigherInMem->nextLowerInMem = newElem;
   1.113 +        }
   1.114 +        add_chunk_to_free_list( newElem, _VMSMasterEnv->freeListHead );
   1.115 +    }
   1.116 +   else
   1.117 +    {
   1.118 +      sizeConsumed = sizeOfFound;
   1.119 +    }
   1.120 +  _VMSMasterEnv->amtOfOutstandingMem += sizeConsumed;
   1.121 +
   1.122 +   //============================= MEASUREMENT STUFF ========================
   1.123 +   #ifdef MEAS__TIME_MALLOC
   1.124 +   saveLowTimeStampCountInto( endStamp );
   1.125 +   addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
   1.126 +   #endif
   1.127 +   //========================================================================
   1.128 +
   1.129 +      //skip over the prolog by adding its size to the pointer return
   1.130 +   return (void*)((uintptr_t)foundElem + sizeof(MallocProlog));
   1.131 + }
   1.132 +
   1.133 +/*This is sequential code, meant to only be called from the Master, not from
   1.134 + * any slave VPs.
   1.135 + *Search down list, checking size by the nextHigherInMem pointer, to find
   1.136 + * first chunk bigger than size needed.
   1.137 + *Shave off the extra and make it into a new free-list element, hook it in
   1.138 + * then return the address of the found element plus size of prolog.
   1.139 + *
   1.140 + * The difference to the regular malloc is, that all the allocated chunks are
   1.141 + * aligned and padded to the size of a CACHE_LINE_SZ. Thus creating a new chunk
   1.142 + * before the aligned chunk.
   1.143 + */
   1.144 +void *VMS_int__malloc_aligned( size_t sizeRequested )
   1.145 + { MallocProlog *foundElem = NULL, *currElem, *newElem;
   1.146 +   ssize_t        amountExtra, sizeConsumed,sizeOfFound,prevAmount;
   1.147 +   uint32        foundElemIsTopOfHeap;
   1.148 +
   1.149 +   //============================= MEASUREMENT STUFF ========================
   1.150 +   #ifdef MEAS__TIME_MALLOC
   1.151 +   uint32 startStamp, endStamp;
   1.152 +   saveLowTimeStampCountInto( startStamp );
   1.153 +   #endif
   1.154 +   //========================================================================
   1.155 +   
   1.156 +      //step up the size to be multiple of the cache line size
   1.157 +   sizeRequested = (sizeRequested + CACHE_LINE_SZ) & ~(CACHE_LINE_SZ-1);
   1.158 +   currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList;
   1.159 +
   1.160 +   while( currElem != NULL )
   1.161 +    {    //check if size of currElem is big enough
   1.162 +      sizeOfFound=(size_t)((uintptr_t)currElem->nextHigherInMem -(uintptr_t)currElem);
   1.163 +      amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
   1.164 +      if( amountExtra > 0 )
   1.165 +       {    
   1.166 +         //look if the found element is already aligned
   1.167 +         if((((uintptr_t)currElem+sizeof(MallocProlog)) & (uintptr_t)(CACHE_LINE_SZ-1)) == 0){
   1.168 +             //found it, get out of loop
   1.169 +             foundElem = currElem;
   1.170 +             break;
   1.171 +         }else{
   1.172 +             //find first aligned address and check if it's still big enough
   1.173 +             //check also if the space before the aligned address is big enough
   1.174 +             //for a new element
   1.175 +             void *firstAlignedAddr = (void*)(((uintptr_t)currElem + 2*CACHE_LINE_SZ) & ~((uintptr_t)(CACHE_LINE_SZ-1)));
   1.176 +             prevAmount = (uintptr_t)firstAlignedAddr - (uintptr_t)currElem;
   1.177 +             sizeOfFound=(uintptr_t)currElem->nextHigherInMem -(uintptr_t)firstAlignedAddr + sizeof(MallocProlog);
   1.178 +             amountExtra= sizeOfFound - sizeRequested - sizeof(MallocProlog);
   1.179 +             if(prevAmount > 2*sizeof(MallocProlog) && amountExtra > 0 ){
   1.180 +                 //found suitable element
   1.181 +                 //create new previous element and exit loop
   1.182 +                 MallocProlog *newAlignedElem = (MallocProlog*)firstAlignedAddr - 1;
   1.183 +                 
   1.184 +                 //insert new element into free list
   1.185 +                 if(currElem->nextChunkInFreeList != NULL)
   1.186 +                     currElem->nextChunkInFreeList->prevChunkInFreeList = newAlignedElem;                     
   1.187 +                 newAlignedElem->prevChunkInFreeList = currElem;
   1.188 +                 newAlignedElem->nextChunkInFreeList = currElem->nextChunkInFreeList;
   1.189 +                 currElem->nextChunkInFreeList = newAlignedElem;
   1.190 +                 
   1.191 +                 //set higherInMem and lowerInMem
   1.192 +                 newAlignedElem->nextHigherInMem = currElem->nextHigherInMem;
   1.193 +                 foundElemIsTopOfHeap = currElem->nextHigherInMem ==
   1.194 +                          _VMSMasterEnv->freeListHead->nextHigherInMem;
   1.195 +                 if(!foundElemIsTopOfHeap)
   1.196 +                     currElem->nextHigherInMem->nextLowerInMem = newAlignedElem;
   1.197 +                 currElem->nextHigherInMem = newAlignedElem;
   1.198 +                 newAlignedElem->nextLowerInMem = currElem;
   1.199 +                 
   1.200 +                 //Found new element leaving loop
   1.201 +                 foundElem = newAlignedElem;
   1.202 +                 break;
   1.203 +             }
   1.204 +         }
   1.205 +         
   1.206 +       }
   1.207 +       currElem = currElem->nextChunkInFreeList;
   1.208 +    }
   1.209 +
   1.210 +   if( foundElem == NULL )
   1.211 +    { ERROR("\nmalloc failed\n")
   1.212 +      return (void *)NULL;  //indicates malloc failed
   1.213 +    }
   1.214 +      //Using a kludge to identify the element that is the top chunk in the
   1.215 +      // heap -- saving top-of-heap addr in head's nextHigherInMem -- and
   1.216 +      // save addr of start of heap in head's nextLowerInMem
   1.217 +      //Will handle top of Heap specially
   1.218 +   foundElemIsTopOfHeap = foundElem->nextHigherInMem ==
   1.219 +                          _VMSMasterEnv->freeListHead->nextHigherInMem;
   1.220 +
   1.221 +      //before shave off and try to insert new elem, remove found elem
   1.222 +      //note, foundElem will never be the head, so always has valid prevChunk
   1.223 +   foundElem->prevChunkInFreeList->nextChunkInFreeList =
   1.224 +                                              foundElem->nextChunkInFreeList;
   1.225 +   if( foundElem->nextChunkInFreeList != NULL )
   1.226 +    { foundElem->nextChunkInFreeList->prevChunkInFreeList =
   1.227 +                                              foundElem->prevChunkInFreeList;
   1.228 +    }
   1.229 +   foundElem->prevChunkInFreeList = NULL;//indicates elem currently allocated
   1.230 +   
   1.231 +      //if enough, turn extra into new elem & insert it
   1.232 +   if( amountExtra > 64 )
   1.233 +    {    //make new elem by adding to addr of curr elem then casting
   1.234 +      sizeConsumed = sizeof(MallocProlog) + sizeRequested;
   1.235 +      newElem = (MallocProlog *)( (uintptr_t)foundElem + sizeConsumed );
   1.236 +      newElem->nextHigherInMem   = foundElem->nextHigherInMem;
   1.237 +      newElem->nextLowerInMem    = foundElem;
   1.238 +      foundElem->nextHigherInMem = newElem;
   1.239 +      
   1.240 +      if( ! foundElemIsTopOfHeap )
   1.241 +       {    //there is no next higher for top of heap, so can't write to it
   1.242 +         newElem->nextHigherInMem->nextLowerInMem = newElem;
   1.243 +       }
   1.244 +      add_chunk_to_free_list( newElem, _VMSMasterEnv->freeListHead );
   1.245 +    }
   1.246 +   else
   1.247 +    {
   1.248 +      sizeConsumed = sizeOfFound;
   1.249 +    }
   1.250 +  _VMSMasterEnv->amtOfOutstandingMem += sizeConsumed;
   1.251 +
   1.252 +   //============================= MEASUREMENT STUFF ========================
   1.253 +   #ifdef MEAS__TIME_MALLOC
   1.254 +   saveLowTimeStampCountInto( endStamp );
   1.255 +   addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
   1.256 +   #endif
   1.257 +   //========================================================================
   1.258 +
   1.259 +      //skip over the prolog by adding its size to the pointer return
   1.260 +   return (void*)((uintptr_t)foundElem + sizeof(MallocProlog));
   1.261 + }
   1.262 +
   1.263 +
   1.264 +/*This is sequential code -- only to be called from the Master
   1.265 + * When free, subtract the size of prolog from pointer, then cast it to a
   1.266 + * MallocProlog.  Then check the nextLower and nextHigher chunks to see if
   1.267 + * one or both are also free, and coalesce if so, and if neither free, then
   1.268 + * add this one to free-list.
   1.269 + */
   1.270 +void
   1.271 +VMS_int__free( void *ptrToFree )
   1.272 + { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem;
   1.273 +   size_t         sizeOfElem;
   1.274 +   uint32         lowerExistsAndIsFree, higherExistsAndIsFree;
   1.275 +
   1.276 +   //============================= MEASUREMENT STUFF ========================
   1.277 +   #ifdef MEAS__TIME_MALLOC
   1.278 +   int32 startStamp, endStamp;
   1.279 +   saveLowTimeStampCountInto( startStamp );
   1.280 +   #endif
   1.281 +   //========================================================================
   1.282 +
   1.283 +   if( ptrToFree < (void*)_VMSMasterEnv->freeListHead->nextLowerInMem ||
   1.284 +       ptrToFree > (void*)_VMSMasterEnv->freeListHead->nextHigherInMem )
   1.285 +    {    //outside the range of data owned by VMS's malloc, so do nothing
   1.286 +      return;
   1.287 +    }
   1.288 +      //subtract size of prolog to get pointer to prolog, then cast
   1.289 +   elemToFree = (MallocProlog *)((uintptr_t)ptrToFree - sizeof(MallocProlog));
   1.290 +   sizeOfElem =(size_t)((uintptr_t)elemToFree->nextHigherInMem-(uintptr_t)elemToFree);
   1.291 +
   1.292 +   if( elemToFree->prevChunkInFreeList != NULL )
   1.293 +    { printf( "error: freeing same element twice!" ); exit(1);
   1.294 +    }
   1.295 +
   1.296 +   _VMSMasterEnv->amtOfOutstandingMem -= sizeOfElem;
   1.297 +
   1.298 +   nextLowerElem  = elemToFree->nextLowerInMem;
   1.299 +   nextHigherElem = elemToFree->nextHigherInMem;
   1.300 +
   1.301 +   if( nextHigherElem == NULL )
   1.302 +      higherExistsAndIsFree = FALSE;
   1.303 +   else //okay exists, now check if in the free-list by checking back ptr
   1.304 +      higherExistsAndIsFree = (nextHigherElem->prevChunkInFreeList != NULL);
   1.305 +    
   1.306 +   if( nextLowerElem == NULL )
   1.307 +      lowerExistsAndIsFree = FALSE;
   1.308 +   else //okay, it exists, now check if it's free
   1.309 +      lowerExistsAndIsFree = (nextLowerElem->prevChunkInFreeList != NULL);
   1.310 +    
   1.311 +
   1.312 +      //now, know what exists and what's free
   1.313 +   if( lowerExistsAndIsFree )
   1.314 +    { if( higherExistsAndIsFree )
   1.315 +       {    //both exist and are free, so coalesce all three
   1.316 +            //First, remove higher from free-list
   1.317 +         nextHigherElem->prevChunkInFreeList->nextChunkInFreeList =
   1.318 +                                         nextHigherElem->nextChunkInFreeList;
   1.319 +         if( nextHigherElem->nextChunkInFreeList != NULL ) //end-of-list?
   1.320 +            nextHigherElem->nextChunkInFreeList->prevChunkInFreeList =
   1.321 +                                         nextHigherElem->prevChunkInFreeList;
   1.322 +            //Now, fix-up sequence-in-mem list -- by side-effect, this also
   1.323 +            // changes size of the lower elem, which is still in free-list
   1.324 +         nextLowerElem->nextHigherInMem = nextHigherElem->nextHigherInMem;
   1.325 +         if( nextHigherElem->nextHigherInMem !=
   1.326 +             _VMSMasterEnv->freeListHead->nextHigherInMem )
   1.327 +            nextHigherElem->nextHigherInMem->nextLowerInMem = nextLowerElem;
   1.328 +            //notice didn't do anything to elemToFree -- it simply is no
   1.329 +            // longer reachable from any of the lists.  Wonder if could be a
   1.330 +            // security leak because left valid addresses in it,
   1.331 +            // but don't care for now.
   1.332 +       }
   1.333 +      else
   1.334 +       {    //lower is the only of the two that exists and is free,
   1.335 +            //In this case, no adjustment to free-list, just change mem-list.
   1.336 +            // By side-effect, changes size of the lower elem
   1.337 +         nextLowerElem->nextHigherInMem = elemToFree->nextHigherInMem;
   1.338 +         if( elemToFree->nextHigherInMem !=
   1.339 +             _VMSMasterEnv->freeListHead->nextHigherInMem )
   1.340 +            elemToFree->nextHigherInMem->nextLowerInMem = nextLowerElem;
   1.341 +       }
   1.342 +    }
   1.343 +   else
   1.344 +    {    //lower either doesn't exist or isn't free, so check higher
   1.345 +      if( higherExistsAndIsFree )
   1.346 +       {    //higher exists and is the only of the two free
   1.347 +            //First, in free-list, replace higher elem with the one to free
   1.348 +         elemToFree->nextChunkInFreeList=nextHigherElem->nextChunkInFreeList;
   1.349 +         elemToFree->prevChunkInFreeList=nextHigherElem->prevChunkInFreeList;
   1.350 +         elemToFree->prevChunkInFreeList->nextChunkInFreeList = elemToFree;
   1.351 +         if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list?
   1.352 +            elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree;
   1.353 +            //Now chg mem-list. By side-effect, changes size of elemToFree
   1.354 +         elemToFree->nextHigherInMem = nextHigherElem->nextHigherInMem;
   1.355 +         if( elemToFree->nextHigherInMem !=
   1.356 +             _VMSMasterEnv->freeListHead->nextHigherInMem )
   1.357 +            elemToFree->nextHigherInMem->nextLowerInMem = elemToFree;
   1.358 +       }
   1.359 +      else
   1.360 +       {    //neither lower nor higher is availabe to coalesce so add to list
   1.361 +            // this makes prev chunk ptr non-null, which indicates it's free
   1.362 +         elemToFree->nextChunkInFreeList =
   1.363 +                            _VMSMasterEnv->freeListHead->nextChunkInFreeList;
   1.364 +         _VMSMasterEnv->freeListHead->nextChunkInFreeList = elemToFree;
   1.365 +         if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list?
   1.366 +            elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree;
   1.367 +         elemToFree->prevChunkInFreeList = _VMSMasterEnv->freeListHead;
   1.368 +       }
   1.369 +    }
   1.370 +   //============================= MEASUREMENT STUFF ========================
   1.371 +   #ifdef MEAS__TIME_MALLOC
   1.372 +   saveLowTimeStampCountInto( endStamp );
   1.373 +   addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->freeTimeHist );
   1.374 +   #endif
   1.375 +   //========================================================================
   1.376 +
   1.377 + }
   1.378 +
   1.379 +
   1.380 +/*Allocates memory from the external system -- higher overhead
   1.381 + *
   1.382 + *Because of Linux's malloc throwing bizarre random faults when malloc is
   1.383 + * used inside a VMS virtual processor, have to pass this as a request and
   1.384 + * have the core loop do it when it gets around to it -- will look for these
   1.385 + * chores leftover from the previous animation of masterVP the next time it
   1.386 + * goes to animate the masterVP -- so it takes two separate masterVP
   1.387 + * animations, separated by work, to complete an external malloc or
   1.388 + * external free request.
   1.389 + *
   1.390 + *Thinking core loop accepts signals -- just looks if signal-location is
   1.391 + * empty or not --
   1.392 + */
   1.393 +void *
   1.394 +VMS__malloc_in_ext( size_t sizeRequested )
   1.395 + {
   1.396 + /*
   1.397 +      //This is running in the master, so no chance for multiple cores to be
   1.398 +      // competing for the core's flag.
   1.399 +   if(  *(_VMSMasterEnv->coreLoopSignalAddr[ 0 ]) != 0 )
   1.400 +    {    //something has already signalled to core loop, so save the signal
   1.401 +         // and look, next time master animated, to see if can send it.
   1.402 +         //Note, the addr to put a signal is in the coreloop's frame, so just
   1.403 +         // checks it each time through -- make it volatile to avoid GCC
   1.404 +         // optimizations -- it's a coreloop local var that only changes
   1.405 +         // after jumping away.  The signal includes the addr to send the
   1.406 +         //return to -- even if just empty return completion-signal
   1.407 +         //
   1.408 +         //save the signal in some queue that the master looks at each time
   1.409 +         // it starts up -- one loc says if empty for fast common case --
   1.410 +         //something like that -- want to hide this inside this call -- but
   1.411 +         // think this has to come as a request -- req handler gives procr
   1.412 +         // back to master loop, which gives it back to req handler at point
   1.413 +         // it sees that core loop has sent return signal.  Something like
   1.414 +         // that.
   1.415 +      saveTheSignal
   1.416 +
   1.417 +    }
   1.418 +  coreSigData->type = malloc;
   1.419 +  coreSigData->sizeToMalloc = sizeRequested;
   1.420 +  coreSigData->locToSignalCompletion = &figureOut;
   1.421 +   _VMSMasterEnv->coreLoopSignals[ 0 ] = coreSigData;
   1.422 +  */
   1.423 +      //just risk system-stack faults until get this figured out
   1.424 +   return malloc( sizeRequested );
   1.425 + }
   1.426 +
   1.427 +
   1.428 +/*Frees memory that was allocated in the external system -- higher overhead
   1.429 + *
   1.430 + *As noted in external malloc comment, this is clunky 'cause the free has
   1.431 + * to be called in the core loop.
   1.432 + */
   1.433 +void
   1.434 +VMS__free_in_ext( void *ptrToFree )
   1.435 + {
   1.436 +      //just risk system-stack faults until get this figured out
   1.437 +   free( ptrToFree );
   1.438 +
   1.439 +      //TODO: fix this -- so 
   1.440 + }
   1.441 +
   1.442 +
   1.443 +/*Designed to be called from the main thread outside of VMS, during init
   1.444 + */
   1.445 +MallocProlog *
   1.446 +VMS_ext__create_free_list()
   1.447 + { MallocProlog *freeListHead, *firstChunk;
   1.448 +
   1.449 +      //Note, this is running in the main thread -- all increases in malloc
   1.450 +      // mem and all frees of it must be done in this thread, with the
   1.451 +      // thread's original stack available
   1.452 +   freeListHead = malloc( sizeof(MallocProlog) );
   1.453 +   firstChunk   = malloc( MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE );
   1.454 +   if( firstChunk == NULL ) {printf("malloc error\n"); exit(1);}
   1.455 +   
   1.456 +   //Touch memory to avoid page faults
   1.457 +   void *ptr,*endPtr; 
   1.458 +   endPtr = (void*)firstChunk+MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE;
   1.459 +   for(ptr = firstChunk; ptr < endPtr; ptr+=PAGE_SIZE)
   1.460 +   {
   1.461 +       *(char*)ptr = 0;
   1.462 +   }
   1.463 +
   1.464 +   freeListHead->prevChunkInFreeList = NULL;
   1.465 +      //Use this addr to free the heap when cleanup
   1.466 +   freeListHead->nextLowerInMem      = firstChunk;
   1.467 +      //to identify top-of-heap elem, compare this addr to elem's next higher
   1.468 +   freeListHead->nextHigherInMem     = (void*)( (uintptr_t)firstChunk +
   1.469 +                                         MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
   1.470 +   freeListHead->nextChunkInFreeList = firstChunk;
   1.471 +
   1.472 +   firstChunk->nextChunkInFreeList   = NULL;
   1.473 +   firstChunk->prevChunkInFreeList   = freeListHead;
   1.474 +      //next Higher has to be set to top of chunk, so can calc size in malloc
   1.475 +   firstChunk->nextHigherInMem       = (void*)( (uintptr_t)firstChunk +
   1.476 +                                         MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
   1.477 +   firstChunk->nextLowerInMem        = NULL; //identifies as bott of heap
   1.478 +   
   1.479 +   _VMSMasterEnv->amtOfOutstandingMem = 0; //none allocated yet
   1.480 +
   1.481 +   return freeListHead;
   1.482 + }
   1.483 +
   1.484 +
   1.485 +/*Designed to be called from the main thread outside of VMS, during cleanup
   1.486 + */
   1.487 +void
   1.488 +VMS_ext__free_free_list( MallocProlog *freeListHead )
   1.489 + {    
   1.490 +      //stashed a ptr to the one and only bug chunk malloc'd from OS in the
   1.491 +      // free list head's next lower in mem pointer
   1.492 +   free( freeListHead->nextLowerInMem );
   1.493 +
   1.494 +   //don't free the head -- it'll be in an array eventually -- free whole
   1.495 +   // array when all the free lists linked from it have already been freed
   1.496 + }
   1.497 +