Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
diff vmalloc.c @ 50:8f7141a9272e
Added VMS__malloc and probes, and major re-factoring to separate mallocs
| author | Me |
|---|---|
| date | Sat, 30 Oct 2010 20:54:36 -0700 |
| parents | |
| children | 42dd44df1bb0 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/vmalloc.c Sat Oct 30 20:54:36 2010 -0700 1.3 @@ -0,0 +1,256 @@ 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 + 1.15 +#include "VMS.h" 1.16 + 1.17 +/*Helper function 1.18 + *Insert a newly generated free chunk into the first spot on the free list. 1.19 + * The chunk is cast as a MallocProlog, so the various pointers in it are 1.20 + * accessed with C's help -- and the size of the prolog is easily added to 1.21 + * the pointer when a chunk is returned to the app -- so C handles changes 1.22 + * in pointer sizes among machines. 1.23 + * 1.24 + *The list head is a normal MallocProlog struct -- identified by its 1.25 + * prevChunkInFreeList being NULL -- the only one. 1.26 + * 1.27 + *The end of the list is identified by next chunk being NULL, as usual. 1.28 + */ 1.29 +void inline 1.30 +add_chunk_to_free_list( MallocProlog *chunk, MallocProlog *listHead ) 1.31 + { 1.32 + chunk->nextChunkInFreeList = listHead->nextChunkInFreeList; 1.33 + if( chunk->nextChunkInFreeList != NULL ) //if not last in free list 1.34 + chunk->nextChunkInFreeList->prevChunkInFreeList = chunk; 1.35 + chunk->prevChunkInFreeList = listHead; 1.36 + listHead->nextChunkInFreeList = chunk; 1.37 + } 1.38 + 1.39 + 1.40 +/*This is sequential code, meant to only be called from the Master, not from 1.41 + * any slave VPs. 1.42 + *Search down list, checking size by the nextHigherInMem pointer, to find 1.43 + * first chunk bigger than size needed. 1.44 + *Shave off the extra and make it into a new free-list element, hook it in 1.45 + * then return the address of the found element plus size of prolog. 1.46 + * 1.47 + *Will find a 1.48 + */ 1.49 +void * 1.50 +VMS__malloc( int32 sizeRequested ) 1.51 + { MallocProlog *foundElem = NULL, *currElem, *newElem; 1.52 + int32 amountExtra, foundElemIsTopOfHeap, sizeConsumed,sizeOfFound; 1.53 + 1.54 + //step up the size to be aligned at 16-byte boundary, prob better ways 1.55 + sizeRequested = ((sizeRequested + 16) >> 4) << 4; 1.56 + currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList; 1.57 + 1.58 + while( currElem != NULL ) 1.59 + { //check if size of currElem is big enough 1.60 + sizeOfFound=(int32)((char*)currElem->nextHigherInMem -(char*)currElem); 1.61 + amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog); 1.62 + if( amountExtra > 0 ) 1.63 + { //found it, get out of loop 1.64 + foundElem = currElem; 1.65 + currElem = NULL; 1.66 + } 1.67 + else 1.68 + currElem = currElem->nextChunkInFreeList; 1.69 + } 1.70 + 1.71 + if( foundElem == NULL ) 1.72 + { PRINT_ERROR("\nmalloc failed\n") 1.73 + return NULL; //indicates malloc failed 1.74 + } 1.75 + //Using a kludge to identify the element that is the top chunk in the 1.76 + // heap -- saving top-of-heap addr in head's nextHigherInMem -- and 1.77 + // save addr of start of heap in head's nextLowerInMem 1.78 + //Will handle top of Heap specially 1.79 + foundElemIsTopOfHeap = foundElem->nextHigherInMem == 1.80 + _VMSMasterEnv->freeListHead->nextHigherInMem; 1.81 + 1.82 + //before shave off and try to insert new elem, remove found elem 1.83 + //note, foundElem will never be the head, so always has valid prevChunk 1.84 + foundElem->prevChunkInFreeList->nextChunkInFreeList = 1.85 + foundElem->nextChunkInFreeList; 1.86 + if( foundElem->nextChunkInFreeList != NULL ) 1.87 + { foundElem->nextChunkInFreeList->prevChunkInFreeList = 1.88 + foundElem->prevChunkInFreeList; 1.89 + } 1.90 + foundElem->prevChunkInFreeList = NULL;//indicates elem currently allocated 1.91 + 1.92 + //if enough, turn extra into new elem & insert it 1.93 + if( amountExtra > 64 ) 1.94 + { //make new elem by adding to addr of curr elem then casting 1.95 + sizeConsumed = sizeof(MallocProlog) + sizeRequested; 1.96 + newElem = (MallocProlog *)( (char *)foundElem + sizeConsumed ); 1.97 + newElem->nextHigherInMem = foundElem->nextHigherInMem; 1.98 + newElem->nextLowerInMem = foundElem; 1.99 + foundElem->nextHigherInMem = newElem; 1.100 + 1.101 + if( ! foundElemIsTopOfHeap ) 1.102 + { //there is no next higher for top of heap, so can't write to it 1.103 + newElem->nextHigherInMem->nextLowerInMem = newElem; 1.104 + } 1.105 + add_chunk_to_free_list( newElem, _VMSMasterEnv->freeListHead ); 1.106 + } 1.107 + else 1.108 + { 1.109 + sizeConsumed = sizeOfFound; 1.110 + } 1.111 + _VMSMasterEnv->amtOfOutstandingMem += sizeConsumed; 1.112 + 1.113 + //skip over the prolog by adding its size to the pointer return 1.114 + return (void *)((char *)foundElem + sizeof(MallocProlog)); 1.115 + } 1.116 + 1.117 + 1.118 +/*This is sequential code -- only to be called from the Master 1.119 + * When free, subtract the size of prolog from pointer, then cast it to a 1.120 + * MallocProlog. Then check the nextLower and nextHigher chunks to see if 1.121 + * one or both are also free, and coalesce if so, and if neither free, then 1.122 + * add this one to free-list. 1.123 + */ 1.124 +void 1.125 +VMS__free( void *ptrToFree ) 1.126 + { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem; 1.127 + int32 lowerExistsAndIsFree, higherExistsAndIsFree, sizeOfElem; 1.128 + 1.129 + if( ptrToFree < _VMSMasterEnv->freeListHead->nextLowerInMem || 1.130 + ptrToFree > _VMSMasterEnv->freeListHead->nextHigherInMem ) 1.131 + { //outside the range of data owned by VMS's malloc, so do nothing 1.132 + return; 1.133 + } 1.134 + //subtract size of prolog to get pointer to prolog, then cast 1.135 + elemToFree = (MallocProlog *)((char *)ptrToFree - sizeof(MallocProlog)); 1.136 + sizeOfElem =(int32)((char*)elemToFree->nextHigherInMem-(char*)elemToFree); 1.137 + _VMSMasterEnv->amtOfOutstandingMem -= sizeOfElem; 1.138 + 1.139 + nextLowerElem = elemToFree->nextLowerInMem; 1.140 + nextHigherElem = elemToFree->nextHigherInMem; 1.141 + 1.142 + if( nextHigherElem == NULL ) 1.143 + higherExistsAndIsFree = FALSE; 1.144 + else //okay exists, now check if in the free-list by checking back ptr 1.145 + higherExistsAndIsFree = (nextHigherElem->prevChunkInFreeList != NULL); 1.146 + 1.147 + if( nextLowerElem == NULL ) 1.148 + lowerExistsAndIsFree = FALSE; 1.149 + else //okay, it exists, now check if it's free 1.150 + lowerExistsAndIsFree = (nextLowerElem->prevChunkInFreeList != NULL); 1.151 + 1.152 + 1.153 + //now, know what exists and what's free 1.154 + if( lowerExistsAndIsFree ) 1.155 + { if( higherExistsAndIsFree ) 1.156 + { //both exist and are free, so coalesce all three 1.157 + //First, remove higher from free-list 1.158 + nextHigherElem->prevChunkInFreeList->nextChunkInFreeList = 1.159 + nextHigherElem->nextChunkInFreeList; 1.160 + if( nextHigherElem->nextChunkInFreeList != NULL ) //end-of-list? 1.161 + nextHigherElem->nextChunkInFreeList->prevChunkInFreeList = 1.162 + nextHigherElem->prevChunkInFreeList; 1.163 + //Now, fix-up sequence-in-mem list -- by side-effect, this also 1.164 + // changes size of the lower elem, which is still in free-list 1.165 + nextLowerElem->nextHigherInMem = nextHigherElem->nextHigherInMem; 1.166 + if( nextHigherElem->nextHigherInMem != 1.167 + _VMSMasterEnv->freeListHead->nextHigherInMem ) 1.168 + nextHigherElem->nextHigherInMem->nextLowerInMem = nextLowerElem; 1.169 + //notice didn't do anything to elemToFree -- it simply is no 1.170 + // longer reachable from any of the lists. Wonder if could be a 1.171 + // security leak because left valid addresses in it, 1.172 + // but don't care for now. 1.173 + } 1.174 + else 1.175 + { //lower is the only of the two that exists and is free, 1.176 + //In this case, no adjustment to free-list, just change mem-list. 1.177 + // By side-effect, changes size of the lower elem 1.178 + nextLowerElem->nextHigherInMem = elemToFree->nextHigherInMem; 1.179 + if( elemToFree->nextHigherInMem != 1.180 + _VMSMasterEnv->freeListHead->nextHigherInMem ) 1.181 + elemToFree->nextHigherInMem->nextLowerInMem = nextLowerElem; 1.182 + } 1.183 + } 1.184 + else 1.185 + { //lower either doesn't exist or isn't free, so check higher 1.186 + if( higherExistsAndIsFree ) 1.187 + { //higher exists and is the only of the two free 1.188 + //First, in free-list, replace higher elem with the one to free 1.189 + elemToFree->nextChunkInFreeList=nextHigherElem->nextChunkInFreeList; 1.190 + elemToFree->prevChunkInFreeList=nextHigherElem->prevChunkInFreeList; 1.191 + elemToFree->prevChunkInFreeList->nextChunkInFreeList = elemToFree; 1.192 + if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list? 1.193 + elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree; 1.194 + //Now chg mem-list. By side-effect, changes size of elemToFree 1.195 + elemToFree->nextHigherInMem = nextHigherElem->nextHigherInMem; 1.196 + if( elemToFree->nextHigherInMem != 1.197 + _VMSMasterEnv->freeListHead->nextHigherInMem ) 1.198 + elemToFree->nextHigherInMem->nextLowerInMem = elemToFree; 1.199 + } 1.200 + else 1.201 + { //neither lower nor higher is availabe to coalesce so add to list 1.202 + // this makes prev chunk ptr non-null, which indicates it's free 1.203 + elemToFree->nextChunkInFreeList = 1.204 + _VMSMasterEnv->freeListHead->nextChunkInFreeList; 1.205 + _VMSMasterEnv->freeListHead->nextChunkInFreeList = elemToFree; 1.206 + if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list? 1.207 + elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree; 1.208 + elemToFree->prevChunkInFreeList = _VMSMasterEnv->freeListHead; 1.209 + } 1.210 + } 1.211 + 1.212 + } 1.213 + 1.214 + 1.215 +/*Designed to be called from the main thread outside of VMS, during init 1.216 + */ 1.217 +MallocProlog * 1.218 +VMS__create_free_list() 1.219 + { MallocProlog *freeListHead, *firstChunk; 1.220 + 1.221 + //Note, this is running in the main thread -- all increases in malloc 1.222 + // mem and all frees of it must be done in this thread, with the 1.223 + // thread's original stack available 1.224 + freeListHead = malloc( sizeof(MallocProlog) ); 1.225 + firstChunk = malloc( MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE ); 1.226 + if( firstChunk == NULL ) {printf("malloc error\n"); exit(1);} 1.227 + 1.228 + freeListHead->prevChunkInFreeList = NULL; 1.229 + //Use this addr to free the heap when cleanup 1.230 + freeListHead->nextLowerInMem = firstChunk; 1.231 + //to identify top-of-heap elem, compare this addr to elem's next higher 1.232 + freeListHead->nextHigherInMem = (char *)firstChunk + 1.233 + MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE; 1.234 + freeListHead->nextChunkInFreeList = firstChunk; 1.235 + 1.236 + firstChunk->nextChunkInFreeList = NULL; 1.237 + firstChunk->prevChunkInFreeList = freeListHead; 1.238 + //next Higher has to be set to top of chunk, so can calc size in malloc 1.239 + firstChunk->nextHigherInMem = (char *)firstChunk + 1.240 + MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE; 1.241 + firstChunk->nextLowerInMem = NULL; //identifies as bott of heap 1.242 + 1.243 + return freeListHead; 1.244 + } 1.245 + 1.246 + 1.247 +/*Designed to be called from the main thread outside of VMS, during cleanup 1.248 + */ 1.249 +void 1.250 +VMS_ext__free_free_list( MallocProlog *freeListHead ) 1.251 + { 1.252 + //stashed a ptr to the one and only bug chunk malloc'd from OS in the 1.253 + // free list head's next lower in mem pointer 1.254 + free( freeListHead->nextLowerInMem ); 1.255 + 1.256 + //don't free the head -- it'll be in an array eventually -- free whole 1.257 + // array when all the free lists linked from it have already been freed 1.258 + } 1.259 +
