Mercurial > cgi-bin > hgwebdir.cgi > VMS > VMS_Implementations > VMS_impls > VMS__MC_shared_impl
view vmalloc.c @ 62:dd3e60aeae26
Middle of fixing for -O3.. works -O0 still -- hard-code field offsets in assbly
| author | Me |
|---|---|
| date | Fri, 12 Nov 2010 07:36:01 -0800 |
| parents | 3bac84e4e56e |
| children | a6c442d52590 13b22ffb8a2f |
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 <stdlib.h>
13 #include "VMS.h"
15 /*Helper function
16 *Insert a newly generated free chunk into the first spot on the free list.
17 * The chunk is cast as a MallocProlog, so the various pointers in it are
18 * accessed with C's help -- and the size of the prolog is easily added to
19 * the pointer when a chunk is returned to the app -- so C handles changes
20 * in pointer sizes among machines.
21 *
22 *The list head is a normal MallocProlog struct -- identified by its
23 * prevChunkInFreeList being NULL -- the only one.
24 *
25 *The end of the list is identified by next chunk being NULL, as usual.
26 */
27 void inline
28 add_chunk_to_free_list( MallocProlog *chunk, MallocProlog *listHead )
29 {
30 chunk->nextChunkInFreeList = listHead->nextChunkInFreeList;
31 if( chunk->nextChunkInFreeList != NULL ) //if not last in free list
32 chunk->nextChunkInFreeList->prevChunkInFreeList = chunk;
33 chunk->prevChunkInFreeList = listHead;
34 listHead->nextChunkInFreeList = chunk;
35 }
38 /*This is sequential code, meant to only be called from the Master, not from
39 * any slave VPs.
40 *Search down list, checking size by the nextHigherInMem pointer, to find
41 * first chunk bigger than size needed.
42 *Shave off the extra and make it into a new free-list element, hook it in
43 * then return the address of the found element plus size of prolog.
44 *
45 *Will find a
46 */
47 void *
48 VMS__malloc( int32 sizeRequested )
49 { MallocProlog *foundElem = NULL, *currElem, *newElem;
50 int32 amountExtra, foundElemIsTopOfHeap, sizeConsumed,sizeOfFound;
52 //step up the size to be aligned at 16-byte boundary, prob better ways
53 sizeRequested = ((sizeRequested + 16) >> 4) << 4;
54 currElem = (_VMSMasterEnv->freeListHead)->nextChunkInFreeList;
56 while( currElem != NULL )
57 { //check if size of currElem is big enough
58 sizeOfFound=(int32)((char*)currElem->nextHigherInMem -(char*)currElem);
59 amountExtra = sizeOfFound - sizeRequested - sizeof(MallocProlog);
60 if( amountExtra > 0 )
61 { //found it, get out of loop
62 foundElem = currElem;
63 currElem = NULL;
64 }
65 else
66 currElem = currElem->nextChunkInFreeList;
67 }
69 if( foundElem == NULL )
70 { ERROR("\nmalloc failed\n")
71 return (void *)NULL; //indicates malloc failed
72 }
73 //Using a kludge to identify the element that is the top chunk in the
74 // heap -- saving top-of-heap addr in head's nextHigherInMem -- and
75 // save addr of start of heap in head's nextLowerInMem
76 //Will handle top of Heap specially
77 foundElemIsTopOfHeap = foundElem->nextHigherInMem ==
78 _VMSMasterEnv->freeListHead->nextHigherInMem;
80 //before shave off and try to insert new elem, remove found elem
81 //note, foundElem will never be the head, so always has valid prevChunk
82 foundElem->prevChunkInFreeList->nextChunkInFreeList =
83 foundElem->nextChunkInFreeList;
84 if( foundElem->nextChunkInFreeList != NULL )
85 { foundElem->nextChunkInFreeList->prevChunkInFreeList =
86 foundElem->prevChunkInFreeList;
87 }
88 foundElem->prevChunkInFreeList = NULL;//indicates elem currently allocated
90 //if enough, turn extra into new elem & insert it
91 if( amountExtra > 64 )
92 { //make new elem by adding to addr of curr elem then casting
93 sizeConsumed = sizeof(MallocProlog) + sizeRequested;
94 newElem = (MallocProlog *)( (char *)foundElem + sizeConsumed );
95 newElem->nextHigherInMem = foundElem->nextHigherInMem;
96 newElem->nextLowerInMem = foundElem;
97 foundElem->nextHigherInMem = newElem;
99 if( ! foundElemIsTopOfHeap )
100 { //there is no next higher for top of heap, so can't write to it
101 newElem->nextHigherInMem->nextLowerInMem = newElem;
102 }
103 add_chunk_to_free_list( newElem, _VMSMasterEnv->freeListHead );
104 }
105 else
106 {
107 sizeConsumed = sizeOfFound;
108 }
109 _VMSMasterEnv->amtOfOutstandingMem += sizeConsumed;
111 //skip over the prolog by adding its size to the pointer return
112 return (void *)((char *)foundElem + sizeof(MallocProlog));
113 }
116 /*This is sequential code -- only to be called from the Master
117 * When free, subtract the size of prolog from pointer, then cast it to a
118 * MallocProlog. Then check the nextLower and nextHigher chunks to see if
119 * one or both are also free, and coalesce if so, and if neither free, then
120 * add this one to free-list.
121 */
122 void
123 VMS__free( void *ptrToFree )
124 { MallocProlog *elemToFree, *nextLowerElem, *nextHigherElem;
125 int32 lowerExistsAndIsFree, higherExistsAndIsFree, sizeOfElem;
127 if( ptrToFree < (void*)_VMSMasterEnv->freeListHead->nextLowerInMem ||
128 ptrToFree > (void*)_VMSMasterEnv->freeListHead->nextHigherInMem )
129 { //outside the range of data owned by VMS's malloc, so do nothing
130 return;
131 }
132 //subtract size of prolog to get pointer to prolog, then cast
133 elemToFree = (MallocProlog *)((char *)ptrToFree - sizeof(MallocProlog));
134 sizeOfElem =(int32)((char*)elemToFree->nextHigherInMem-(char*)elemToFree);
136 if( elemToFree->prevChunkInFreeList != NULL )
137 { printf( "error: freeing same element twice!" ); exit(1);
138 }
140 _VMSMasterEnv->amtOfOutstandingMem -= sizeOfElem;
142 nextLowerElem = elemToFree->nextLowerInMem;
143 nextHigherElem = elemToFree->nextHigherInMem;
145 if( nextHigherElem == NULL )
146 higherExistsAndIsFree = FALSE;
147 else //okay exists, now check if in the free-list by checking back ptr
148 higherExistsAndIsFree = (nextHigherElem->prevChunkInFreeList != NULL);
150 if( nextLowerElem == NULL )
151 lowerExistsAndIsFree = FALSE;
152 else //okay, it exists, now check if it's free
153 lowerExistsAndIsFree = (nextLowerElem->prevChunkInFreeList != NULL);
156 //now, know what exists and what's free
157 if( lowerExistsAndIsFree )
158 { if( higherExistsAndIsFree )
159 { //both exist and are free, so coalesce all three
160 //First, remove higher from free-list
161 nextHigherElem->prevChunkInFreeList->nextChunkInFreeList =
162 nextHigherElem->nextChunkInFreeList;
163 if( nextHigherElem->nextChunkInFreeList != NULL ) //end-of-list?
164 nextHigherElem->nextChunkInFreeList->prevChunkInFreeList =
165 nextHigherElem->prevChunkInFreeList;
166 //Now, fix-up sequence-in-mem list -- by side-effect, this also
167 // changes size of the lower elem, which is still in free-list
168 nextLowerElem->nextHigherInMem = nextHigherElem->nextHigherInMem;
169 if( nextHigherElem->nextHigherInMem !=
170 _VMSMasterEnv->freeListHead->nextHigherInMem )
171 nextHigherElem->nextHigherInMem->nextLowerInMem = nextLowerElem;
172 //notice didn't do anything to elemToFree -- it simply is no
173 // longer reachable from any of the lists. Wonder if could be a
174 // security leak because left valid addresses in it,
175 // but don't care for now.
176 }
177 else
178 { //lower is the only of the two that exists and is free,
179 //In this case, no adjustment to free-list, just change mem-list.
180 // By side-effect, changes size of the lower elem
181 nextLowerElem->nextHigherInMem = elemToFree->nextHigherInMem;
182 if( elemToFree->nextHigherInMem !=
183 _VMSMasterEnv->freeListHead->nextHigherInMem )
184 elemToFree->nextHigherInMem->nextLowerInMem = nextLowerElem;
185 }
186 }
187 else
188 { //lower either doesn't exist or isn't free, so check higher
189 if( higherExistsAndIsFree )
190 { //higher exists and is the only of the two free
191 //First, in free-list, replace higher elem with the one to free
192 elemToFree->nextChunkInFreeList=nextHigherElem->nextChunkInFreeList;
193 elemToFree->prevChunkInFreeList=nextHigherElem->prevChunkInFreeList;
194 elemToFree->prevChunkInFreeList->nextChunkInFreeList = elemToFree;
195 if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list?
196 elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree;
197 //Now chg mem-list. By side-effect, changes size of elemToFree
198 elemToFree->nextHigherInMem = nextHigherElem->nextHigherInMem;
199 if( elemToFree->nextHigherInMem !=
200 _VMSMasterEnv->freeListHead->nextHigherInMem )
201 elemToFree->nextHigherInMem->nextLowerInMem = elemToFree;
202 }
203 else
204 { //neither lower nor higher is availabe to coalesce so add to list
205 // this makes prev chunk ptr non-null, which indicates it's free
206 elemToFree->nextChunkInFreeList =
207 _VMSMasterEnv->freeListHead->nextChunkInFreeList;
208 _VMSMasterEnv->freeListHead->nextChunkInFreeList = elemToFree;
209 if( elemToFree->nextChunkInFreeList != NULL ) // end-of-list?
210 elemToFree->nextChunkInFreeList->prevChunkInFreeList =elemToFree;
211 elemToFree->prevChunkInFreeList = _VMSMasterEnv->freeListHead;
212 }
213 }
215 }
218 /*Allocates memory from the external system -- higher overhead
219 *
220 *Because of Linux's malloc throwing bizarre random faults when malloc is
221 * used inside a VMS virtual processor, have to pass this as a request and
222 * have the core loop do it when it gets around to it -- will look for these
223 * chores leftover from the previous animation of masterVP the next time it
224 * goes to animate the masterVP -- so it takes two separate masterVP
225 * animations, separated by work, to complete an external malloc or
226 * external free request.
227 *
228 *Thinking core loop accepts signals -- just looks if signal-location is
229 * empty or not --
230 */
231 void *
232 VMS__malloc_in_ext( int32 sizeRequested )
233 {
234 /*
235 //This is running in the master, so no chance for multiple cores to be
236 // competing for the core's flag.
237 if( *(_VMSMasterEnv->coreLoopSignalAddr[ 0 ]) != 0 )
238 { //something has already signalled to core loop, so save the signal
239 // and look, next time master animated, to see if can send it.
240 //Note, the addr to put a signal is in the coreloop's frame, so just
241 // checks it each time through -- make it volatile to avoid GCC
242 // optimizations -- it's a coreloop local var that only changes
243 // after jumping away. The signal includes the addr to send the
244 //return to -- even if just empty return completion-signal
245 //
246 //save the signal in some queue that the master looks at each time
247 // it starts up -- one loc says if empty for fast common case --
248 //something like that -- want to hide this inside this call -- but
249 // think this has to come as a request -- req handler gives procr
250 // back to master loop, which gives it back to req handler at point
251 // it sees that core loop has sent return signal. Something like
252 // that.
253 saveTheSignal
255 }
256 coreSigData->type = malloc;
257 coreSigData->sizeToMalloc = sizeRequested;
258 coreSigData->locToSignalCompletion = &figureOut;
259 _VMSMasterEnv->coreLoopSignals[ 0 ] = coreSigData;
260 */
261 //just risk system-stack faults until get this figured out
262 return malloc( sizeRequested );
263 }
266 /*Frees memory that was allocated in the external system -- higher overhead
267 *
268 *As noted in external malloc comment, this is clunky 'cause the free has
269 * to be called in the core loop.
270 */
271 void
272 VMS__free_in_ext( void *ptrToFree )
273 {
274 //just risk system-stack faults until get this figured out
275 free( ptrToFree );
277 //TODO: fix this -- so
278 }
281 /*Designed to be called from the main thread outside of VMS, during init
282 */
283 MallocProlog *
284 VMS_ext__create_free_list()
285 { MallocProlog *freeListHead, *firstChunk;
287 //Note, this is running in the main thread -- all increases in malloc
288 // mem and all frees of it must be done in this thread, with the
289 // thread's original stack available
290 freeListHead = malloc( sizeof(MallocProlog) );
291 firstChunk = malloc( MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE );
292 if( firstChunk == NULL ) {printf("malloc error\n"); exit(1);}
294 freeListHead->prevChunkInFreeList = NULL;
295 //Use this addr to free the heap when cleanup
296 freeListHead->nextLowerInMem = firstChunk;
297 //to identify top-of-heap elem, compare this addr to elem's next higher
298 freeListHead->nextHigherInMem = (void*)( (char*)firstChunk +
299 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
300 freeListHead->nextChunkInFreeList = firstChunk;
302 firstChunk->nextChunkInFreeList = NULL;
303 firstChunk->prevChunkInFreeList = freeListHead;
304 //next Higher has to be set to top of chunk, so can calc size in malloc
305 firstChunk->nextHigherInMem = (void*)( (char*)firstChunk +
306 MALLOC_ADDITIONAL_MEM_FROM_OS_SIZE);
307 firstChunk->nextLowerInMem = NULL; //identifies as bott of heap
309 _VMSMasterEnv->amtOfOutstandingMem = 0; //none allocated yet
311 return freeListHead;
312 }
315 /*Designed to be called from the main thread outside of VMS, during cleanup
316 */
317 void
318 VMS_ext__free_free_list( MallocProlog *freeListHead )
319 {
320 //stashed a ptr to the one and only bug chunk malloc'd from OS in the
321 // free list head's next lower in mem pointer
322 free( freeListHead->nextLowerInMem );
324 //don't free the head -- it'll be in an array eventually -- free whole
325 // array when all the free lists linked from it have already been freed
326 }
