Debian Codegraph @

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

raw symbols SCIP
1 /* -*- C++ -*- */
2
3 #ifndef HL_CHUNKHEAP_H_
4 #define HL_CHUNKHEAP_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 /**
24 * @class ChunkHeap
25 * @brief Allocates memory from the superheap in chunks.
26 * @param ChunkSize The minimum size for allocating memory from the superheap.
27 */
28
29 namespace HL {
30
31 template <int ChunkSize, class SuperHeap>
32 class ChunkHeap : public SuperHeap {
33 public:
34
35 inline ChunkHeap (void)
36 : buffer (NULL),
37 eob (NULL)
38 {}
39
40 inline void * malloc (const size_t sz) {
41 void * ptr = buffer;
42 buffer += sz;
43 if (buffer <= eob) {
44 assert (eob != NULL);
45 assert ((size_t) (eob - (char *) ptr + 1) >= sz);
46 return ptr;
47 }
48 buffer -= sz; // we didn't succeed, back up
49 return getMoreMemory(sz);
50 }
51
52 inline void clear (void) {
53 buffer = NULL;
54 eob = NULL;
55 SuperHeap::clear ();
56 }
57
58 private:
59
60 // Disabled.
61 inline int remove (void *);
62
63 void * getMoreMemory (size_t sz) {
64 assert (sz > 0);
65 // Round sz to the next chunk size.
66 size_t reqSize = (((sz-1) / ChunkSize) + 1) * ChunkSize;
67 char * buf = (char *) SuperHeap::malloc (reqSize);
68 if (buf == NULL) {
69 return NULL;
70 }
71 // If the current end of buffer is not the same as the new buffer,
72 // reset the buffer pointer.
73 if (eob != buf) {
74 buffer = buf;
75 }
76 else {
77 // we still have a bit leftover at the end of previous buffer
78 reqSize += eob - buffer;
79 }
80 eob = buffer + reqSize;
81
82 void * ptr = buffer;
83 buffer += sz;
84 return ptr;
85 }
86
87 /// The current allocation buffer.
88 char * buffer;
89
90 /// The end of the buffer.
91 char * eob;
92 };
93
94 }
95
96 #endif