Debian Codegraph @

vendor/Heap-Layers/heaps/buildingblock/coalesceheap.h

raw symbols SCIP
1 /* -*- C++ -*- */
2
3 #ifndef HL_COALESCEHEAP_H
4 #define HL_COALESCEHEAP_H
5
6 /*
7
8 Heap Layers: An Extensible Memory Allocation Infrastructure
9
10 Copyright (C) 2000-2020 by Emery Berger
11 http://www.emeryberger.com
12 [email protected]
13
14 Heap Layers is distributed under the terms of the Apache 2.0 license.
15
16 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 */
20
21 #include <assert.h>
22
23 #include "heaplayers.h"
24
25 /**
26 * @class CoalesceHeap
27 * @brief Applies splitting and coalescing.
28 * @see CoalesceableHeap
29 * @see RequireCoalesceable
30 */
31
32 namespace HL {
33
34 template <class super>
35 class CoalesceHeap : public super {
36 public:
37
38 inline void * malloc (const size_t sz)
39 {
40 void * ptr = super::malloc (sz);
41 if (ptr != NULL) {
42 super::markInUse (ptr);
43 void * splitPiece = split (ptr, sz);
44 if (splitPiece != NULL) {
45 super::markFree (splitPiece);
46 super::free (splitPiece);
47 }
48 }
49 return ptr;
50 }
51
52
53 inline void free (void * ptr)
54 {
55 // Try to coalesce this object with its predecessor & successor.
56 if ((super::getNext(super::getPrev(ptr)) != ptr) || (super::getPrev(super::getNext(ptr)) != ptr)) {
57 // We're done with this object.
58 super::free (ptr);
59 return;
60 }
61 assert (super::getPrev(super::getNext(ptr)) == ptr);
62 // Try to coalesce with the previous object..
63 void * prev = super::getPrev(ptr);
64 void * next = super::getNext (ptr);
65 assert (prev != ptr);
66
67 if (super::isPrevFree(ptr)) {
68 assert (super::isFree(prev));
69 super::remove (prev);
70 coalesce (prev, ptr);
71 ptr = prev;
72 }
73 if (super::isFree(next)) {
74 super::remove (next);
75 coalesce (ptr, next);
76 }
77
78 super::markFree (ptr);
79
80 // We're done with this object.
81 super::free (ptr);
82 }
83
84 private:
85
86
87 // Combine the first object with the second.
88 inline static void coalesce (void * first, const void * second) {
89 // A few sanity checks first.
90 assert (super::getNext(first) == second);
91 assert (super::getPrev(second) == first);
92 // Now coalesce.
93 size_t newSize = ((size_t) second - (size_t) first) + super::getSize(second);
94 super::setSize (first, newSize);
95 setPrevSize (super::getNext(first), newSize);
96 }
97
98 // Split an object if it is big enough.
99 inline static void * split (void * obj, const size_t requestedSize) {
100 assert (super::getSize(obj) >= requestedSize);
101 // We split aggressively (for now; this could be a parameter).
102 const size_t actualSize = super::getSize(obj);
103 if (actualSize - requestedSize >= sizeof(typename super::Header) + sizeof(double)) {
104 // Split the object.
105 super::setSize(obj, requestedSize);
106 void * splitPiece = (char *) obj + requestedSize + sizeof(typename super::Header);
107 super::makeObject ((void *) super::getHeader(splitPiece),
108 requestedSize,
109 actualSize - requestedSize - sizeof(typename super::Header));
110 assert (!super::isFree(splitPiece));
111 // Now that we have a new successor (splitPiece), we need to
112 // mark obj as in use.
113 (super::getHeader(splitPiece))->markPrevInUse();
114 assert (super::getSize(splitPiece) >= sizeof(double));
115 assert (super::getSize(obj) >= requestedSize);
116 return splitPiece;
117 } else {
118 return NULL;
119 }
120 }
121
122 };
123
124 }
125
126 #endif