11
11
12
12
#include " src/__support/CPP/array.h"
13
13
#include " src/__support/CPP/cstddef.h"
14
+ #include " src/__support/CPP/new.h"
14
15
#include " src/__support/CPP/span.h"
15
16
#include " src/__support/fixedvector.h"
16
17
@@ -92,19 +93,12 @@ bool FreeList<NUM_BUCKETS>::add_chunk(span<cpp::byte> chunk) {
92
93
if (chunk.size () < sizeof (FreeListNode))
93
94
return false ;
94
95
95
- union {
96
- FreeListNode *node;
97
- cpp::byte *bytes;
98
- } aliased;
99
-
100
- aliased.bytes = chunk.data ();
101
-
96
+ // Add it to the correct list.
102
97
size_t chunk_ptr = find_chunk_ptr_for_size (chunk.size (), false );
103
98
104
- // Add it to the correct list.
105
- aliased.node ->size = chunk.size ();
106
- aliased.node ->next = chunks_[chunk_ptr];
107
- chunks_[chunk_ptr] = aliased.node ;
99
+ FreeListNode *node =
100
+ ::new (chunk.data ()) FreeListNode{chunks_[chunk_ptr], chunk.size ()};
101
+ chunks_[chunk_ptr] = node;
108
102
109
103
return true ;
110
104
}
@@ -123,17 +117,13 @@ span<cpp::byte> FreeList<NUM_BUCKETS>::find_chunk(size_t size) const {
123
117
124
118
// Now iterate up the buckets, walking each list to find a good candidate
125
119
for (size_t i = chunk_ptr; i < chunks_.size (); i++) {
126
- union {
127
- FreeListNode *node;
128
- cpp::byte *data;
129
- } aliased;
130
- aliased.node = chunks_[static_cast <unsigned short >(i)];
120
+ FreeListNode *node = chunks_[static_cast <unsigned short >(i)];
131
121
132
- while (aliased. node != nullptr ) {
133
- if (aliased. node ->size >= size)
134
- return span<cpp::byte>(aliased. data , aliased. node ->size );
122
+ while (node != nullptr ) {
123
+ if (node->size >= size)
124
+ return span<cpp::byte>(reinterpret_cast <cpp::byte *>(node), node->size );
135
125
136
- aliased. node = aliased. node ->next ;
126
+ node = node->next ;
137
127
}
138
128
}
139
129
@@ -146,34 +136,27 @@ template <size_t NUM_BUCKETS>
146
136
bool FreeList<NUM_BUCKETS>::remove_chunk(span<cpp::byte> chunk) {
147
137
size_t chunk_ptr = find_chunk_ptr_for_size (chunk.size (), true );
148
138
149
- // Walk that list, finding the chunk.
150
- union {
151
- FreeListNode *node;
152
- cpp::byte *data;
153
- } aliased, aliased_next;
154
-
155
139
// Check head first.
156
140
if (chunks_[chunk_ptr] == nullptr )
157
141
return false ;
158
142
159
- aliased. node = chunks_[chunk_ptr];
160
- if (aliased. data == chunk.data ()) {
161
- chunks_[chunk_ptr] = aliased. node ->next ;
143
+ FreeListNode * node = chunks_[chunk_ptr];
144
+ if (reinterpret_cast <cpp::byte *>(node) == chunk.data ()) {
145
+ chunks_[chunk_ptr] = node->next ;
162
146
return true ;
163
147
}
164
148
165
149
// No? Walk the nodes.
166
- aliased. node = chunks_[chunk_ptr];
150
+ node = chunks_[chunk_ptr];
167
151
168
- while (aliased.node ->next != nullptr ) {
169
- aliased_next.node = aliased.node ->next ;
170
- if (aliased_next.data == chunk.data ()) {
152
+ while (node->next != nullptr ) {
153
+ if (reinterpret_cast <cpp::byte *>(node->next ) == chunk.data ()) {
171
154
// Found it, remove this node out of the chain
172
- aliased. node ->next = aliased_next. node ->next ;
155
+ node->next = node-> next ->next ;
173
156
return true ;
174
157
}
175
158
176
- aliased. node = aliased. node ->next ;
159
+ node = node->next ;
177
160
}
178
161
179
162
return false ;
0 commit comments