Debian Codegraph @

vendor/Heap-Layers/legacy/reap/regionsimulator.h

raw
1 /* -*- C++ -*- */
2
3 /**
4 * @file regionsimulator.h
5 * @author Emery Berger
6 *
7 * An experimental layer for replacing the memory allocator used by
8 * gcc (obstacks) and other region-based allocators with real malloc &
9 * free calls.
10 */
11
12
13 #ifndef _REGIONSIMULATOR_H_
14 #define _REGIONSIMULATOR_H_
15
16 #ifdef __cplusplus
17
18 #ifndef MALLOC_TRACE
19 #define MALLOC_TRACE 0
20 #endif
21
22
23 #include "..\heaplayers\traceheap.h"
24 #include "..\heaplayers\dynarray.h"
25
26 #include <assert.h>
27
28
29 #if 0
30 // A dynamic array that automatically grows to fit
31 // any index requested for assignment.
32 //
33 // This array also features a clear() method,
34 // to free the entire array, and a trim(n) method,
35 // which tells the array it is no bigger than n elements.
36
37 template <class ObjType>
38 class DynamicArray {
39 public:
40 DynamicArray (void)
41 : internalArray (NULL),
42 internalArrayLength (0)
43 {}
44
45 ~DynamicArray (void)
46 {
47 clear();
48 }
49
50 // clear deletes everything in the array.
51
52 inline void clear (void) {
53 if (internalArray != NULL) {
54 delete [] internalArray;
55 internalArray = NULL;
56 internalArrayLength = 0;
57 //printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength);
58 }
59 }
60
61 // Read-only access to an array element;
62 // asserts that this index is in range.
63
64 inline const ObjType& operator[] (int index) const {
65 assert (index < internalArrayLength);
66 assert (index >= 0);
67 return internalArray[index];
68 }
69
70 // Access a particular array index by reference,
71 // growing the array if necessary.
72
73 inline ObjType& operator[] (int index) {
74 assert (index >= 0);
75 if (index >= internalArrayLength) {
76
77 // This index is beyond the current size of the array.
78 // Grow the array by doubling and copying the old array into the new.
79
80 const int newSize = index * 2 + 1;
81 ObjType * arr = new ObjType[newSize];
82 // printf ("grow! %d to %d\n", internalArrayLength, newSize);
83 #if MALLOC_TRACE
84 printf ("m %x %d\n", arr, newSize * sizeof(ObjType));
85 #endif
86 if (internalArray != NULL) {
87 memcpy (arr, internalArray, internalArrayLength * sizeof(ObjType));
88 delete [] internalArray;
89 #if MALLOC_TRACE
90 printf ("f %x\n", internalArray);
91 #endif
92 }
93 internalArray = arr;
94 internalArrayLength = newSize;
95 // printf ("\ninternalArrayLength %x = %d\n", this, internalArrayLength);
96 }
97 return internalArray[index];
98 }
99
100 // trim informs the array that it is now only nelts long
101 // as far as the client is concerned. This may trigger
102 // shrinking of the array.
103
104 inline void trim (int nelts) {
105
106 // Halve the array if the number of elements
107 // drops below one-fourth of the array size.
108
109 if (internalArray != NULL) {
110 if (nelts * 4 < internalArrayLength) {
111 const int newSize = nelts * 2;
112 ObjType * arr = new ObjType[newSize];
113 // printf ("trim! %d to %d\n", internalArrayLength, newSize);
114 #if MALLOC_TRACE
115 printf ("m %x %d\n", arr, newSize * sizeof(ObjType));
116 #endif
117 memcpy (arr, internalArray, sizeof(ObjType) * nelts);
118 delete [] internalArray;
119 #if MALLOC_TRACE
120 printf ("f %x\n", internalArray);
121 #endif
122 internalArray = arr;
123 internalArrayLength = newSize;
124 }
125 assert (nelts <= internalArrayLength);
126 }
127 }
128
129
130 private:
131
132 // The pointer to the current array.
133
134 ObjType * internalArray;
135
136 // The length of the internal array, in elements.
137
138 int internalArrayLength;
139 };
140 #endif
141
142
143 #if 1
144 class MallocStack {
145 public:
146 MallocStack (void)
147 : numMallocs (0)
148 {
149 mallocs[0] = NULL;
150 }
151
152 int length (void) const { return numMallocs; }
153
154 inline void push (void * ptr) {
155 numMallocs++;
156 mallocs[numMallocs] = ptr;
157 }
158
159 inline void * pop (void) {
160 void * ptr = NULL;
161 if (numMallocs > 0) {
162 ptr = mallocs[numMallocs];
163 numMallocs--;
164 // The array has shrunk, so potentially trim it.
165 mallocs.trim (numMallocs + 1);
166 }
167 return ptr;
168 }
169
170 inline void * top (void) {
171 void * ptr = NULL;
172 if (numMallocs > 0) {
173 ptr = mallocs[numMallocs];
174 }
175 return ptr;
176 }
177
178 inline void clear (void) {
179 mallocs.clear();
180 }
181
182 private:
183
184 // The number of mallocs recorded above.
185 // 0 == no mallocs.
186 // 1 == mallocs[1] has the one malloc, etc.
187 // i.e., we waste entry zero.
188
189 int numMallocs;
190
191 // The array of remembered malloc pointers.
192
193 DynamicArray<void *> mallocs;
194
195
196 };
197 #else
198 class MallocStack {
199 public:
200
201 inline int length (void) const {
202 return theStack.size();
203 }
204
205 inline void push (void * p) {
206 theStack.push_front(p);
207 }
208
209 inline void * top (void) const {
210 if (length() <= 0) {
211 return NULL;
212 } else {
213 return theStack.front();
214 }
215 }
216
217 inline void * pop (void) {
218 if (length() <= 0) {
219 return NULL;
220 }
221 void * ptr = top();
222 theStack.pop_front();
223 return ptr;
224 }
225
226 private:
227
228 list<void *> theStack;
229
230 };
231
232 #endif
233
234
235 /**
236 * @class RegionSimulator
237 * @author Emery Berger
238 * RegionSimulator acts like a region (or obstack),
239 * tracking all mallocs for a subsequent freeAll.
240 * free emulates obstack frees by freeing all objects
241 * allocated after a given point.
242 *
243 */
244
245 #if defined(_WIN32)
246 #include <windows.h>
247 #endif
248
249 template <class SuperHeap>
250 class RegionSimulator : public SuperHeap {
251 public:
252
253
254 RegionSimulator (void)
255 : magicNumber (MAGIC_NUMBER),
256 parent (NULL),
257 child (NULL),
258 sibling (NULL)
259 {
260 initCurrentObject();
261 sanityCheck();
262 }
263
264 ~RegionSimulator (void) {
265 sanityCheck();
266
267 // freeAll();
268 SuperHeap::free (currentObject);
269
270 #if 1
271 // Free everything still on our allocated object array.
272
273 #if 1
274 void * ptr;
275 while ((ptr = mallocStack.pop())) {
276 if ((size_t) ptr & 1 == 0)
277 SuperHeap::free (ptr);
278 }
279 #endif
280
281 // Now iterate over the children and destroy them all.
282
283 #if 1
284 while (child != NULL) {
285 RegionSimulator * nextChild = child->sibling;
286 delete child;
287 child = nextChild;
288 }
289 #endif
290
291 // Inform our parent that we're gone.
292
293 if (parent != NULL) {
294 parent->removeChild (this);
295 }
296 #endif
297
298 }
299
300 // Make the current object big enough to accomodate this request.
301
302 inline void * malloc (size_t sz)
303 {
304 sanityCheck();
305
306 void * ptr;
307
308 // If the current object has been exposed,
309 // we need to grow it. Otherwise, we can allocate
310 // the size requested directly.
311
312 if (isCurrentObjectExposed) {
313
314 // Round up the request size, and grow the current object
315 // large enough to hold sz bytes.
316 // Finalize and return the grown object.
317
318 grow (sz - currentObjectSize);
319 ptr = currentObject;
320 assert (currentObjectSize >= sz);
321 finalize();
322
323 } else
324
325 {
326
327 // Allocate the object directly
328 // and record it.
329
330 ptr = SuperHeap::malloc (sz);
331 mallocStack.push (ptr);
332
333 #if MALLOC_TRACE
334 printf ("M %x %d\n", ptr, sz);
335 #endif
336 }
337
338
339 sanityCheck();
340 return ptr;
341 }
342
343
344 private:
345
346 // Hide free.
347 void free (void *);
348
349 public:
350
351 // Obstack support.
352
353 // Free every pointer allocated AFTER this pointer.
354 inline void freeAfter (void * ptr) {
355 sanityCheck();
356
357 if (ptr != currentObject) {
358
359 // Free everything, in reverse allocated order,
360 // until we find ptr. We have to mask off a 1 when
361 // we do these comparisons because we add 1 bits to
362 // indicate when objects have already been freed.
363
364 while ((mallocStack.top() != NULL)
365 && (((size_t) mallocStack.top() & ~1) != (size_t) ptr))
366 {
367
368 void * popPtr = mallocStack.top();
369
370 // If this object has not yet been freed, free it.
371
372 if (((size_t) popPtr & 1) == 0) {
373 void * addr = (void *) ((size_t) popPtr & ~1);
374 #if MALLOC_TRACE
375 printf ("F %x\n", addr);
376 #endif
377 SuperHeap::free (addr);
378 }
379 mallocStack.pop();
380 }
381
382 sanityCheck();
383
384 } else {
385
386 // Just free the current object and start a new one.
387
388 #if MALLOC_TRACE
389 printf ("M %x %d\n", currentObject, currentObjectSize);
390 printf ("F %x\n", currentObject);
391 #endif
392 SuperHeap::free (currentObject);
393 initCurrentObject();
394 }
395
396 }
397
398
399 // Free every pointer allocated to date.
400 inline void freeAll (void)
401 {
402 void * ptr;
403 sanityCheck();
404
405 while ((ptr = mallocStack.pop())) {
406 // printf ("F %x\n", ptr);
407 if (((size_t) ptr & 1) == 0) {
408 SuperHeap::free ((void *) ((size_t) ptr & ~1));
409 }
410 }
411 mallocStack.clear();
412
413 while (child != NULL) {
414 RegionSimulator * nextChild = child->sibling;
415 delete child;
416 child = nextChild;
417 }
418
419 #if 0
420 RegionSimulator * ch = child;
421 while (ch != NULL) {
422 RegionSimulator * nextChild = ch->sibling;
423 ch->freeAll();
424 ch = nextChild;
425 }
426 #endif
427
428 sanityCheck();
429
430 }
431
432 // Return the start of the current allocated object.
433 inline void * getObjectBase (void) {
434 sanityCheck();
435 isCurrentObjectExposed = true;
436 return currentObject;
437 }
438
439 // Finalize an object.
440 inline void finalize (void) {
441 sanityCheck();
442
443 // Add the current allocated object to our array
444 // and start a new object.
445
446 mallocStack.push (currentObject);
447
448 #if MALLOC_TRACE
449 printf ("M %x %d\n", currentObject, currentObjectSize);
450 #endif
451
452 initCurrentObject();
453
454 sanityCheck();
455 }
456
457 // "Grow" the current object by a certain amount.
458
459 inline void * grow (size_t sz) {
460
461 sanityCheck();
462
463 // Copy out the old object into a new one.
464 // This is effectively a realloc.
465 // If we actually still have space in our
466 // current allocation, though, do nothing.
467
468 const int requestedObjectSize = align(currentObjectSize + sz);
469
470 if (requestedObjectSize > actualObjectSize) {
471
472 // Get a new object and copy everything into it,
473 // modifying CurrentObjectPosition so it points
474 // into the right place in the new object.
475
476 void * ptr = SuperHeap::malloc (requestedObjectSize);
477 #if MALLOC_TRACE
478 printf ("m %x %d\n", ptr, requestedObjectSize);
479 #endif
480
481 actualObjectSize = requestedObjectSize;
482 if (ptr == NULL)
483 abort();
484 memcpy (ptr, currentObject, currentObjectSize);
485 SuperHeap::free (currentObject);
486 #if MALLOC_TRACE
487 printf ("f %x\n", currentObject);
488 #endif
489
490 currentObjectPosition = (char *) ptr + (currentObjectPosition - (char *) currentObject);
491
492 // If someone has already obtained a "handle" on the current object
493 // via getObjectBase, record this object as already freed -- we may
494 // have to free backwards to this pointer.
495
496 if (isCurrentObjectExposed) {
497
498 // Record this object.
499 // We OR the address with 1 to mark this object as already freed.
500
501 mallocStack.push ((void *) ((size_t) currentObject | 1));
502 }
503
504 currentObject = ptr;
505 }
506
507 // We've "grown" the object, so we update its perceived size,
508 // advance the current position so it points to the previous
509 // starting position plus the size. Return the previous starting position.
510
511 // Because calling grow can result in a new object,
512 // the current object can be considered no longer exposed (if it was before).
513
514 isCurrentObjectExposed = false;
515
516 currentObjectSize += sz;
517 char * oldPosition = currentObjectPosition;
518 currentObjectPosition += sz;
519 sanityCheck();
520 return oldPosition;
521 }
522
523 // ch should be a new region, i.e., it should not have any siblings yet.
524
525 void addChild (RegionSimulator * ch)
526 {
527 if (child == NULL) {
528 child = ch;
529 } else {
530 assert (ch->sibling == NULL);
531 ch->sibling = child;
532 child = ch;
533 }
534 ch->parent = this;
535 }
536
537 private:
538
539 #if 1
540
541 void removeChild (RegionSimulator * ch)
542 {
543 assert (ch != NULL);
544 if (child == ch) {
545 child = child->sibling;
546 } else {
547 child->removeSibling (ch);
548 }
549 }
550
551 void removeSibling (RegionSimulator * sib)
552 {
553 RegionSimulator * prev = NULL;
554 RegionSimulator * curr = sibling;
555 int i = 0;
556 while (curr && (curr != sib)) {
557 prev = curr;
558 curr = curr->sibling;
559 i++;
560 }
561 if (prev == NULL) {
562 sibling = sibling->sibling;
563 } else {
564 prev->sibling = curr->sibling;
565 }
566 }
567
568 RegionSimulator * parent;
569 RegionSimulator * child;
570 RegionSimulator * sibling;
571 #endif
572
573 enum { INITIAL_OBJECT_SIZE = sizeof(double) };
574
575
576 // Allocate a new object and initialize the associated variables.
577
578 inline void initCurrentObject (void) {
579 currentObject = SuperHeap::malloc (INITIAL_OBJECT_SIZE);
580 if (currentObject == NULL)
581 abort();
582 currentObjectPosition = (char *) currentObject;
583 currentObjectSize = 0;
584 actualObjectSize = INITIAL_OBJECT_SIZE;
585 isCurrentObjectExposed = false;
586 }
587
588 // Round up to a double word.
589
590 inline static size_t align (int sz) {
591 return (sz + (sizeof(double) - 1)) & ~(sizeof(double) - 1);
592 }
593
594 inline void sanityCheck (void) {
595
596 // Check some class invariants:
597 //
598 // - Verify that we really are in a RegionSimulator layer
599 // by checking a magic number.
600 // - The current object must exist.
601 // - The current position needs to be after the start of the object.
602
603 assert (magicNumber == MAGIC_NUMBER);
604 assert (currentObject != NULL);
605 assert (currentObjectPosition >= (char *) currentObject);
606 }
607
608 // With this magic number, we're either in this layer
609 // or we've found ourselves in a Java class file :).
610
611 enum { MAGIC_NUMBER = 0xCafeBabe };
612
613 // The current object, the current position in that object,
614 // and the current object's perceived and actual sizes.
615
616 void * currentObject;
617 char * currentObjectPosition;
618 size_t currentObjectSize;
619 size_t actualObjectSize;
620
621 // Have we exposed the start of this object to the outside world?
622 // If we have, we will have to record this object in the mallocs array
623 // even if we free it.
624
625 bool isCurrentObjectExposed;
626
627 // A magic number just for sanity checking.
628
629 int magicNumber;
630
631 // The stack of malloc'ed objects.
632
633 MallocStack mallocStack;
634
635 };
636
637
638
639 #if 0
640 extern char * PseudoObstackName;
641 template <class SuperHeap>
642 class PseudoObstack : public RegionSimulator<SuperHeap> {};
643 #endif
644
645 #define C_LINKAGE extern "C"
646
647 #else /* __cplusplus */
648
649 #define C_LINKAGE extern
650
651 #endif /* __cplusplus */
652
653 #include <stdlib.h>
654
655 /* C_LINKAGE void regionCreate (void ** reg); */
656 C_LINKAGE void regionCreate (void ** reg, void ** parent);
657 C_LINKAGE void regionDestroy (void ** reg);
658 C_LINKAGE void * regionAllocate (void ** reg, size_t sz);
659 C_LINKAGE void regionFreeAll (void ** reg);
660
661 /* Obstack functions */
662
663 struct obstack {
664 void * theObstack;
665 };
666
667 C_LINKAGE void obstack_init (struct obstack *obstack);
668 C_LINKAGE void * obstack_alloc (struct obstack *obstack, size_t size);
669 C_LINKAGE void * obstack_copy (struct obstack *obstack, void *address, size_t size);
670 C_LINKAGE void * obstack_copy0 (struct obstack *obstack, void *address, size_t size);
671 C_LINKAGE void obstack_free (struct obstack *obstack, void *block);
672 C_LINKAGE void obstack_blank (struct obstack *obstack, size_t size);
673 C_LINKAGE void obstack_grow (struct obstack *obstack, void *data, size_t size);
674 C_LINKAGE void obstack_grow0 (struct obstack *obstack, void *data, int size);
675 C_LINKAGE void obstack_1grow (struct obstack *obstack, long c);
676 C_LINKAGE void obstack_ptr_grow (struct obstack *obstack, void *data);
677 C_LINKAGE void obstack_int_grow (struct obstack *obstack, int data);
678 C_LINKAGE void * obstack_finish (struct obstack * obstack);
679 C_LINKAGE void * obstack_base (struct obstack *obstack);
680 C_LINKAGE void * obstack_next_free (struct obstack *obstack);
681 C_LINKAGE int _obstack_get_alignment (struct obstack *);
682
683 #endif /* _REGIONSIMULATOR_H_ */
684