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