Debian Codegraph @

vendor/Heap-Layers/heaps/utility/slopheap.h

raw symbols SCIP
1 // -*- C++ -*-
2
3 /*
4
5 Heap Layers: An Extensible Memory Allocation Infrastructure
6
7 Copyright (C) 2000-2020 by Emery Berger
8 http://www.emeryberger.com
9 [email protected]
10
11 Heap Layers is distributed under the terms of the Apache 2.0 license.
12
13 You may obtain a copy of the License at
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 */
17
18 #ifndef HL_SLOPHEAP_H
19 #define HL_SLOPHEAP_H
20
21 #include <cstddef>
22
23 /**
24 * @class SlopHeap
25 *
26 * SlopHeap is designed to guarantee that you always have an extra N bytes
27 * available after the most recent malloc. This is necessary for the current
28 * coalescing support, which can look past the last allocated object.
29 *
30 * @param SuperHeap The parent heap.
31 * @param SLOP The amount of extra memory required, in bytes.
32 */
33
34 namespace HL {
35
36 template <class SuperHeap, int SLOP = 16>
37 class SlopHeap : public SuperHeap {
38 public:
39 SlopHeap (void)
40 : remaining (0),
41 ptr (NULL)
42 {}
43
44 inline void * malloc (const size_t nbytes) {
45
46 // Put the usual case up front.
47 if (nbytes <= remaining) {
48 remaining -= nbytes;
49 char * p = ptr;
50 ptr += nbytes;
51 return (void *) p;
52 }
53
54 //
55 // We don't have enough space to satisfy the current
56 // request, so get more memory.
57 //
58
59 return getMoreMemory(nbytes);
60 }
61
62 inline void clear (void) {
63 ptr = NULL;
64 remaining = 0;
65 SuperHeap::clear ();
66 }
67
68 inline void free (void *) {}
69
70 private:
71
72 // Disabled.
73 inline int remove (void *);
74
75 void * getMoreMemory (size_t nbytes) {
76 char * newptr = (char *) SuperHeap::malloc (nbytes + SLOP);
77
78 if (newptr == NULL) {
79 return NULL;
80 }
81
82 //
83 // If this new memory is contiguous with the previous one,
84 // reclaim the "slop".
85 //
86
87 if ((ptr != NULL) && (ptr + remaining + SLOP == newptr)) {
88 remaining += SLOP;
89 } else {
90 ptr = newptr;
91 remaining = 0;
92 }
93 char * p = ptr;
94 ptr += nbytes;
95
96 return (void *) p;
97 }
98
99 char * ptr;
100 size_t remaining;
101
102 };
103
104 }
105
106 #endif