comparison vmalloc.c @ 76:9ddbb071142d

make hardware independent and port to 64bit
author Merten Sach <msach@mailbox.tu-berlin.de>
date Thu, 16 Jun 2011 14:41:15 +0200
parents 9c3107044f86
children 521c75d64cef
comparison
equal deleted inserted replaced
7:ec7c30cec7bc 8:2f2ee79fea94
6 * 6 *
7 * Created on November 14, 2009, 9:07 PM 7 * Created on November 14, 2009, 9:07 PM
8 */ 8 */
9 9
10 #include <malloc.h> 10 #include <malloc.h>
11 #include <inttypes.h>
11 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <stdio.h>
12 14
13 #include "VMS.h" 15 #include "VMS.h"
14 #include "Histogram/Histogram.h" 16 #include "Histogram/Histogram.h"
15 17
16 /*Helper function 18 /*Helper function
43 *Shave off the extra and make it into a new free-list element, hook it in 45 *Shave off the extra and make it into a new free-list element, hook it in
44 * then return the address of the found element plus size of prolog. 46 * then return the address of the found element plus size of prolog.
45 * 47 *
46 *Will find a 48 *Will find a
47 */ 49 */
48 void * 50 void *VMS__malloc( size_t sizeRequested )
49 VMS__malloc( int32 sizeRequested )
50 { MallocProlog *foundElem = NULL, *currElem, *newElem; 51 { MallocProlog *foundElem = NULL, *currElem, *newElem;
51 int32 amountExtra, foundElemIsTopOfHeap, sizeConsumed,sizeOfFound; 52 ssize_t amountExtra, sizeConsumed,sizeOfFound;
53 uint32 foundElemIsTopOfHeap;
52 54
53 //============================= MEASUREMENT STUFF ======================== 55 //============================= MEASUREMENT STUFF ========================
54 #ifdef MEAS__TIME_MALLOC 56 #ifdef MEAS__TIME_MALLOC
55 int32 startStamp, endStamp; 57 int32 startStamp, endStamp;
56 saveLowTimeStampCountInto( startStamp ); 58 saveLowTimeStampCountInto( startStamp );
61 sizeRequested = ((sizeRequested + 16) >> 4) << 4; 63 sizeRequested = ((sizeRequested + 16) >> 4) << 4;
62 currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList; 64 currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList;
63 65
64 while( currElem != NULL ) 66 while( currElem != NULL )
65 { //check if size of currElem is big enough 67 { //check if size of currElem is big enough
66 sizeOfFound=(int32)((char*)currElem->nextHigherInMem -(char*)currElem); 68 sizeOfFound=(size_t)((uintptr_t)currElem->nextHigherInMem -(uintptr_t)currElem);
67 amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog); 69 amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
68 if( amountExtra > 0 ) 70 if( amountExtra > 0 )
69 { //found it, get out of loop 71 { //found it, get out of loop
70 foundElem = currElem; 72 foundElem = currElem;
71 currElem = NULL; 73 currElem = NULL;
97 99
98 //if enough, turn extra into new elem & insert it 100 //if enough, turn extra into new elem & insert it
99 if( amountExtra > 64 ) 101 if( amountExtra > 64 )
100 { //make new elem by adding to addr of curr elem then casting 102 { //make new elem by adding to addr of curr elem then casting
101 sizeConsumed = sizeof(MallocProlog) + sizeRequested; 103 sizeConsumed = sizeof(MallocProlog) + sizeRequested;
102 newElem = (MallocProlog *)( (char *)foundElem + sizeConsumed ); 104 newElem = (MallocProlog *)( (uintptr_t)foundElem + sizeConsumed );
103 newElem->nextHigherInMem = foundElem->nextHigherInMem; 105 newElem->nextHigherInMem = foundElem->nextHigherInMem;
104 newElem->nextLowerInMem = foundElem; 106 newElem->nextLowerInMem = foundElem;
105 foundElem->nextHigherInMem = newElem; 107 foundElem->nextHigherInMem = newElem;
106 108
107 if( ! foundElemIsTopOfHeap ) 109 if( ! foundElemIsTopOfHeap )
122 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist ); 124 addIntervalToHist( startStamp, endStamp, _VMSMasterEnv->mallocTimeHist );
123 #endif 125 #endif
124 //======================================================================== 126 //========================================================================
125 127
126 //skip over the prolog by adding its size to the pointer return 128 //skip over the prolog by adding its size to the pointer return
127 return (void *)((char *)foundElem + sizeof(MallocProlog)); 129 return (void*)((uintptr_t)foundElem + sizeof(MallocProlog));
128 } 130 }
129 131
130 132
131 /*This is sequential code -- only to be called from the Master 133 /*This is sequential code -- only to be called from the Master
132 * When free, subtract the size of prolog from pointer, then cast it to a 134 * When free, subtract the size of prolog from pointer, then cast it to a
135 * add this one to free-list. 137 * add this one to free-list.
136 */ 138 */
137 void 139 void
138 VMS__free( void *ptrToFree ) 140 VMS__free( void *ptrToFree )
139 { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem; 141 { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem;
140 int32 lowerExistsAndIsFree, higherExistsAndIsFree, sizeOfElem; 142 size_t sizeOfElem;
143 uint32 lowerExistsAndIsFree, higherExistsAndIsFree;
141 144
142 //============================= MEASUREMENT STUFF ======================== 145 //============================= MEASUREMENT STUFF ========================
143 #ifdef MEAS__TIME_MALLOC 146 #ifdef MEAS__TIME_MALLOC
144 int32 startStamp, endStamp; 147 int32 startStamp, endStamp;
145 saveLowTimeStampCountInto( startStamp ); 148 saveLowTimeStampCountInto( startStamp );
150 ptrToFree > (void*)_VMSMasterEnv->freeListHead->nextHigherInMem ) 153 ptrToFree > (void*)_VMSMasterEnv->freeListHead->nextHigherInMem )
151 { //outside the range of data owned by VMS's malloc, so do nothing 154 { //outside the range of data owned by VMS's malloc, so do nothing
152 return; 155 return;
153 } 156 }
154 //subtract size of prolog to get pointer to prolog, then cast 157 //subtract size of prolog to get pointer to prolog, then cast
155 elemToFree = (MallocProlog *)((char *)ptrToFree - sizeof(MallocProlog)); 158 elemToFree = (MallocProlog *)((uintptr_t)ptrToFree - sizeof(MallocProlog));
156 sizeOfElem =(int32)((char*)elemToFree->nextHigherInMem-(char*)elemToFree); 159 sizeOfElem =(size_t)((uintptr_t)elemToFree->nextHigherInMem-(uintptr_t)elemToFree);
157 160
158 if( elemToFree->prevChunkInFreeList != NULL ) 161 if( elemToFree->prevChunkInFreeList != NULL )
159 { printf( "error: freeing same element twice!" ); exit(1); 162 { printf( "error: freeing same element twice!" ); exit(1);
160 } 163 }
161 164
255 * 258 *
256 *Thinking core loop accepts signals -- just looks if signal-location is 259 *Thinking core loop accepts signals -- just looks if signal-location is
257 * empty or not -- 260 * empty or not --
258 */ 261 */
259 void * 262 void *
260 VMS__malloc_in_ext( int32 sizeRequested ) 263 VMS__malloc_in_ext( size_t sizeRequested )
261 { 264 {
262 /* 265 /*
263 //This is running in the master, so no chance for multiple cores to be 266 //This is running in the master, so no chance for multiple cores to be
264 // competing for the core's flag. 267 // competing for the core's flag.
265 if( *(_VMSMasterEnv->coreLoopSignalAddr[ 0 ]) != 0 ) 268 if( *(_VMSMasterEnv->coreLoopSignalAddr[ 0 ]) != 0 )
321 324
322 freeListHead->prevChunkInFreeList = NULL; 325 freeListHead->prevChunkInFreeList = NULL;
323 //Use this addr to free the heap when cleanup 326 //Use this addr to free the heap when cleanup
324 freeListHead->nextLowerInMem = firstChunk; 327 freeListHead->nextLowerInMem = firstChunk;
325 //to identify top-of-heap elem, compare this addr to elem's next higher 328 //to identify top-of-heap elem, compare this addr to elem's next higher
326 freeListHead->nextHigherInMem = (void*)( (char*)firstChunk + 329 freeListHead->nextHigherInMem = (void*)( (uintptr_t)firstChunk +
327 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE); 330 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
328 freeListHead->nextChunkInFreeList = firstChunk; 331 freeListHead->nextChunkInFreeList = firstChunk;
329 332
330 firstChunk->nextChunkInFreeList = NULL; 333 firstChunk->nextChunkInFreeList = NULL;
331 firstChunk->prevChunkInFreeList = freeListHead; 334 firstChunk->prevChunkInFreeList = freeListHead;
332 //next Higher has to be set to top of chunk, so can calc size in malloc 335 //next Higher has to be set to top of chunk, so can calc size in malloc
333 firstChunk->nextHigherInMem = (void*)( (char*)firstChunk + 336 firstChunk->nextHigherInMem = (void*)( (uintptr_t)firstChunk +
334 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE); 337 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
335 firstChunk->nextLowerInMem = NULL; //identifies as bott of heap 338 firstChunk->nextLowerInMem = NULL; //identifies as bott of heap
336 339
337 _VMSMasterEnv->amtOfOutstandingMem = 0; //none allocated yet 340 _VMSMasterEnv->amtOfOutstandingMem = 0; //none allocated yet
338 341