-
Notifications
You must be signed in to change notification settings - Fork 1
/
robin_hood.patch
166 lines (154 loc) · 6.02 KB
/
robin_hood.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--- robin_hood_new2.h 2020-10-28 19:08:17.245463100 +0530
+++ robin_hood_new.h 2020-10-28 19:03:34.035734400 +0530
@@ -39,6 +39,7 @@
9 // for adding functionality in a backwards-compatible manner
#define ROBIN_HOOD_VERSION_PATCH 0 // for backwards-compatible bug fixes
+#include "gc.h"
#include <algorithm>
#include <cstdlib>
#include <cstring>
@@ -407,11 +408,29 @@
// Deallocates all allocated memory.
void reset() noexcept {
+ size_t numAllocatedElements = calcNumElementsToAlloc() / 2;
while(mListForFree) {
- T *tmp = *mListForFree;
- std::free(mListForFree);
+ T * tmp = *mListForFree;
+ uint8_t *m = (uint8_t *)mListForFree - 1;
+ // check the tag
+ switch(*m) {
+ case 0:
+ // it is automatically allocated. so use
+ // the calculated size + 1 (for the flag).
+ GcObject_free(
+ m, (numAllocatedElements * ALIGNED_SIZE) +
+ ALIGNMENT + 1);
+ break;
+ case 1:
+ // it is manually allocated. so retrieve the
+ // size from the pointer.
+ size_t *s = (size_t *)m - 1;
+ GcObject_free(s, *s);
+ break;
+ }
mListForFree =
reinterpret_cast_no_cast_align_warning<T **>(tmp);
+ numAllocatedElements /= 2;
}
mHead = nullptr;
}
@@ -444,14 +463,31 @@
// free()). If the provided data is not large enough to make use of,
// it is immediately freed. Otherwise it is reused and freed in the
// destructor.
+ //
+ // We pad it with a byte of value 1 in the beginning, to denote it
+ // has been added manually. We also store the size of the block,
+ // to later make use of it in free.
void addOrFree(void *ptr, const size_t numBytes) noexcept {
// calculate number of available elements in ptr
- if(numBytes < ALIGNMENT + ALIGNED_SIZE) {
+ if(numBytes < ALIGNMENT + ALIGNED_SIZE + sizeof(uint8_t) +
+ sizeof(size_t)) {
// not enough data for at least one element. Free and
// return.
- std::free(ptr);
+ GcObject_free(ptr, numBytes);
} else {
- add(ptr, numBytes);
+ // manually allocated or not, each block stores
+ // at least one uint8_t to denote the type. so, it
+ // will be safe to decrement the pointer to one
+ // uint8_t in either case. But size will only be stored
+ // if it is manually allocated. So we store the size
+ // first, then the tag bit, so that we can always
+ // safely decrement any pointer to atleast 1 uint8_t.
+ size_t *s = (size_t *)ptr;
+ *s = numBytes;
+ uint8_t *p = (uint8_t *)(s + 1);
+ *p = 1; // mark as manually allocated
+ // now add the rest of the block
+ add(p + 1, numBytes - sizeof(uint8_t) - sizeof(size_t));
}
}
@@ -515,8 +551,12 @@
mHead = headT;
}
- // Called when no memory is available (mHead == 0).
- // Don't inline this slow path.
+ // We need to distinguish between memory allocated
+ // by the allocator, and memory added to the list
+ // manually. To do so, we put an uint8_t before each
+ // chunk of memory, which when 0, denotes the block
+ // has been allocated by the allocator. When 1,
+ // denotes the block has been added manually.
ROBIN_HOOD(NOINLINE) T *performAllocation() {
size_t const numElementsToAlloc = calcNumElementsToAlloc();
@@ -525,7 +565,12 @@
// << " bytes" << std::endl;
size_t const bytes =
ALIGNMENT + ALIGNED_SIZE * numElementsToAlloc;
- add(assertNotNull<std::bad_alloc>(std::malloc(bytes)), bytes);
+ uint8_t *m = (uint8_t *)assertNotNull<std::bad_alloc>(
+ GcObject_malloc(bytes + sizeof(uint8_t)));
+ // set the first uint8_t to zero.
+ *m = 0;
+ // now add the block after skipping the first byte
+ add(m + 1, bytes);
return mHead;
}
@@ -564,10 +609,8 @@
struct NodeAllocator<T, MinSize, MaxSize, true> {
// we are not using the data, so just free it.
- void
- addOrFree(void * ptr,
- size_t ROBIN_HOOD_UNUSED(numBytes) /*unused*/) noexcept {
- std::free(ptr);
+ void addOrFree(void *ptr, size_t numBytes /*unused*/) noexcept {
+ GcObject_free(ptr, numBytes);
}
};
@@ -1684,7 +1727,7 @@
auto const numElementsWithBuffer =
calcNumElementsWithBuffer(o.mMask + 1);
mKeyVals = static_cast<Node *>(
- detail::assertNotNull<std::bad_alloc>(std::malloc(
+ detail::assertNotNull<std::bad_alloc>(GcObject_malloc(
calcNumBytesTotal(numElementsWithBuffer))));
// no need for calloc because clonData does memcpy
mInfo = reinterpret_cast<uint8_t *>(mKeyVals +
@@ -1741,13 +1784,15 @@
// we need to realloc.
if(0 != mMask) {
// only deallocate if we actually have data!
- std::free(mKeyVals);
+ GcObject_free(mKeyVals, calcNumBytesTotal(
+ calcNumElementsWithBuffer(
+ mMask + 1)));
}
auto const numElementsWithBuffer =
calcNumElementsWithBuffer(o.mMask + 1);
mKeyVals = static_cast<Node *>(
- detail::assertNotNull<std::bad_alloc>(std::malloc(
+ detail::assertNotNull<std::bad_alloc>(GcObject_malloc(
calcNumBytesTotal(numElementsWithBuffer))));
// no need for calloc here because cloneData performs a
@@ -2345,7 +2390,7 @@
// calloc also zeroes everything
mKeyVals = reinterpret_cast<Node *>(
- detail::assertNotNull<std::bad_alloc>(std::calloc(
+ detail::assertNotNull<std::bad_alloc>(GcObject_calloc(
1, calcNumBytesTotal(numElementsWithBuffer))));
mInfo = reinterpret_cast<uint8_t *>(mKeyVals +
numElementsWithBuffer);
@@ -2563,7 +2608,9 @@
// [-Werror=free-nonheap-object]
if(mKeyVals !=
reinterpret_cast_no_cast_align_warning<Node *>(&mMask)) {
- std::free(mKeyVals);
+ GcObject_free(mKeyVals,
+ calcNumBytesTotal(
+ calcNumElementsWithBuffer(mMask + 1)));
}
}