Debian Codegraph @

vendor/Heap-Layers/heaps/buildingblock/adaptheap.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
19 #ifndef HL_ADAPTHEAP_H
20 #define HL_ADAPTHEAP_H
21
22 #include <assert.h>
23 #include <stdlib.h>
24
25 /**
26 * @class AdaptHeap
27 * @brief Maintains dictionary entries through freed objects.
28 * Sample dictionaries include DLList and SLList.
29 */
30
31 namespace HL {
32
33 template <class Dictionary, class SuperHeap>
34 class AdaptHeap : public SuperHeap {
35 public:
36
37 enum { Alignment = SuperHeap::Alignment };
38
39 /// Allocate an object (remove from the dictionary).
40 inline void * malloc (const size_t) {
41 void * ptr = (Entry *) dict.get();
42 if (ptr) {
43 assert (SuperHeap::getSize(ptr) >= sizeof(dict));
44 }
45 return ptr;
46 }
47
48 /// Deallocate the object (return to the dictionary).
49 inline void free (void * ptr) {
50 if (ptr) {
51 assert (SuperHeap::getSize(ptr) >= sizeof(dict));
52 Entry * entry = (Entry *) ptr;
53 dict.insert (entry);
54 }
55 }
56
57 /// Remove an object from the dictionary.
58 inline int remove (void * ptr) {
59 if (ptr) {
60 assert (SuperHeap::getSize(ptr) >= sizeof(dict));
61 dict.remove ((Entry *) ptr);
62 }
63 return 1;
64 }
65
66 /// Clear the dictionary.
67 inline void clear (void) {
68 Entry * ptr;
69 while ((ptr = (Entry *) dict.get()) != NULL) {
70 SuperHeap::free (ptr);
71 }
72 dict.clear();
73 SuperHeap::clear();
74 }
75
76
77 private:
78
79 /// The dictionary object.
80 Dictionary dict;
81
82 class Entry : public Dictionary::Entry {};
83 };
84
85 }
86
87 #endif // HL_ADAPTHEAP