Skip to content

Commit a2ca7d1

Browse files
deadlightrealMorcules
authored andcommitted
Works.
1 parent 0345835 commit a2ca7d1

File tree

2 files changed

+40
-78
lines changed

2 files changed

+40
-78
lines changed

src/internal/datatype_allocator.c

Lines changed: 35 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include "internal.h"
22
#include <stdatomic.h>
33
#include <limits.h>
4+
#include <stdlib.h>
45
#include <unistd.h>
56

67
static inline void free_stack_lock(struct SwiftNetMemoryAllocatorStack* const stack) {
78
atomic_store_explicit(&stack->owner, ALLOCATOR_STACK_FREE, memory_order_release);
89
}
910

1011
struct SwiftNetMemoryAllocatorStack* const find_free_pointer_stack(const struct SwiftNetMemoryAllocator* const allocator) {
11-
for (struct SwiftNetMemoryAllocatorStack* current_stack = atomic_load(&allocator->free_memory_pointers.first_item); current_stack != NULL; current_stack = atomic_load_explicit(&current_stack->next, memory_order_acquire)) {
12+
for (struct SwiftNetMemoryAllocatorStack* current_stack = atomic_load(&allocator->data.first_item); current_stack != NULL; current_stack = atomic_load_explicit(&current_stack->next, memory_order_acquire)) {
1213
uint8_t thread_none = ALLOCATOR_STACK_FREE;
1314

1415
if (!atomic_compare_exchange_strong_explicit(
@@ -34,7 +35,7 @@ struct SwiftNetMemoryAllocatorStack* const find_free_pointer_stack(const struct
3435
}
3536

3637
struct SwiftNetMemoryAllocatorStack* const find_valid_pointer_stack(const struct SwiftNetMemoryAllocator* const allocator) {
37-
for (struct SwiftNetMemoryAllocatorStack* current_stack = atomic_load(&allocator->free_memory_pointers.first_item); current_stack != NULL; current_stack = atomic_load_explicit(&current_stack->next, memory_order_acquire)) {
38+
for (struct SwiftNetMemoryAllocatorStack* current_stack = atomic_load(&allocator->data.first_item); current_stack != NULL; current_stack = atomic_load_explicit(&current_stack->next, memory_order_acquire)) {
3839
uint8_t thread_none = ALLOCATOR_STACK_FREE;
3940

4041
if (!atomic_compare_exchange_strong_explicit(&current_stack->owner, &thread_none, ALLOCATOR_STACK_OCCUPIED, memory_order_acquire, memory_order_relaxed)) {
@@ -55,54 +56,40 @@ struct SwiftNetMemoryAllocatorStack* const find_valid_pointer_stack(const struct
5556

5657
struct SwiftNetMemoryAllocator allocator_create(const uint32_t item_size, const uint32_t chunk_item_amount) {
5758
void* const allocated_memory = malloc(item_size * chunk_item_amount);
58-
void* const stack_allocated_memory = malloc(chunk_item_amount * sizeof(void*));
59+
void* const pointers_memory = malloc(chunk_item_amount * sizeof(void*));
5960

60-
if (unlikely(allocated_memory == NULL || stack_allocated_memory == NULL)) {
61+
if (unlikely(allocated_memory == NULL || pointers_memory == NULL)) {
6162
PRINT_ERROR("Failed to allocate memory");
6263
exit(EXIT_FAILURE);
6364
}
6465

65-
struct SwiftNetMemoryAllocatorStack* const first_stack_pointers = malloc(sizeof(struct SwiftNetMemoryAllocatorStack));
66-
if (unlikely(first_stack_pointers == NULL)) {
66+
struct SwiftNetMemoryAllocatorStack* const first_stack = malloc(sizeof(struct SwiftNetMemoryAllocatorStack));
67+
if (unlikely(first_stack == NULL)) {
6768
PRINT_ERROR("Failed to allocate memory");
6869
exit(EXIT_FAILURE);
6970
}
7071

71-
first_stack_pointers->data = stack_allocated_memory;
72-
first_stack_pointers->size = chunk_item_amount;
73-
first_stack_pointers->next = NULL;
74-
first_stack_pointers->previous = NULL;
75-
first_stack_pointers->owner = ALLOCATOR_STACK_FREE;
76-
77-
struct SwiftNetMemoryAllocatorStack* const first_stack_data = malloc(sizeof(struct SwiftNetMemoryAllocatorStack));
78-
if (unlikely(first_stack_data == NULL)) {
79-
PRINT_ERROR("Failed to allocate memory");
80-
exit(EXIT_FAILURE);
81-
}
82-
83-
first_stack_data->data = allocated_memory;
84-
first_stack_data->size = 0;
85-
first_stack_data->next = NULL;
86-
first_stack_data->previous = NULL;
72+
first_stack->data = allocated_memory;
73+
first_stack->pointers = pointers_memory;
74+
first_stack->size = chunk_item_amount;
75+
first_stack->next = NULL;
76+
first_stack->previous = NULL;
77+
first_stack->owner = ALLOCATOR_STACK_FREE;
8778

8879
struct SwiftNetMemoryAllocator new_allocator = (struct SwiftNetMemoryAllocator){
89-
.free_memory_pointers = (struct SwiftNetChunkStorageManager){
90-
.first_item = first_stack_pointers,
91-
.last_item = first_stack_pointers,
92-
},
9380
.data = (struct SwiftNetChunkStorageManager){
94-
.first_item = first_stack_data,
95-
.last_item = first_stack_data,
81+
.first_item = first_stack,
82+
.last_item = first_stack,
9683
},
9784
.item_size = item_size,
9885
.chunk_item_amount = chunk_item_amount,
9986
};
10087

10188
for (uint32_t i = 0; i < chunk_item_amount; i++) {
102-
(*((void **)stack_allocated_memory + i) = (uint8_t*)allocated_memory + (i * item_size));
89+
(*((void **)pointers_memory + i) = (uint8_t*)allocated_memory + (i * item_size));
10390
}
10491

105-
new_allocator.creating_stack = STACK_CREATING_UNLOCKED;
92+
atomic_store_explicit(&new_allocator.creating_stack, STACK_CREATING_UNLOCKED, memory_order_release);
10693

10794
return new_allocator;
10895
}
@@ -122,46 +109,32 @@ static void create_new_stack(struct SwiftNetMemoryAllocator* const memory_alloca
122109
const uint32_t item_size = memory_allocator->item_size;
123110

124111
void* const allocated_memory = malloc(memory_allocator->item_size * chunk_item_amount);
125-
void* const stack_allocated_memory = malloc(chunk_item_amount * sizeof(void*));
126-
if (unlikely(allocated_memory == NULL || stack_allocated_memory == NULL)) {
112+
void* const allocated_memory_pointers = malloc(chunk_item_amount * sizeof(void*));
113+
if (unlikely(allocated_memory == NULL || allocated_memory_pointers == NULL)) {
127114
PRINT_ERROR("Failed to allocate memory");
128115
exit(EXIT_FAILURE);
129116
}
130117

131-
struct SwiftNetMemoryAllocatorStack* const stack_pointers = malloc(sizeof(struct SwiftNetMemoryAllocatorStack));
132-
if (unlikely(stack_pointers == NULL)) {
133-
PRINT_ERROR("Failed to allocate memory");
134-
exit(EXIT_FAILURE);
135-
}
136-
137-
stack_pointers->data = stack_allocated_memory;
138-
stack_pointers->size = chunk_item_amount;
139-
stack_pointers->previous = atomic_load(&memory_allocator->free_memory_pointers.last_item);
140-
stack_pointers->next = NULL;
141-
stack_pointers->owner = ALLOCATOR_STACK_FREE;
142-
143-
struct SwiftNetMemoryAllocatorStack* const stack_data = malloc(sizeof(struct SwiftNetMemoryAllocatorStack));
144-
if (unlikely(stack_data == NULL)) {
118+
struct SwiftNetMemoryAllocatorStack* const stack = malloc(sizeof(struct SwiftNetMemoryAllocatorStack));
119+
if (unlikely(stack == NULL)) {
145120
PRINT_ERROR("Failed to allocate memory");
146121
exit(EXIT_FAILURE);
147122
}
148123

149-
stack_data->data = allocated_memory;
150-
stack_data->size = 0;
151-
stack_data->next = NULL;
152-
stack_data->previous = atomic_load(&memory_allocator->data.last_item);
124+
stack->pointers = allocated_memory_pointers;
125+
stack->data = allocated_memory;
126+
stack->size = chunk_item_amount;
127+
stack->previous = atomic_load(&memory_allocator->data.last_item);
128+
stack->next = NULL;
129+
stack->owner = ALLOCATOR_STACK_FREE;
153130

154131
for (uint32_t i = 0; i < chunk_item_amount; i++) {
155-
((void **)stack_allocated_memory)[i] = (uint8_t*)allocated_memory + (i * item_size);
132+
((void **)allocated_memory_pointers)[i] = (uint8_t*)allocated_memory + (i * item_size);
156133
}
157134

158-
atomic_store(&((struct SwiftNetMemoryAllocatorStack*)atomic_load(&memory_allocator->data.last_item))->next, stack_data);
159-
atomic_store(&memory_allocator->data.last_item, stack_data);
160-
161-
atomic_store(&((struct SwiftNetMemoryAllocatorStack*)atomic_load(&memory_allocator->free_memory_pointers.last_item))->next, stack_pointers);
162-
atomic_store(&memory_allocator->free_memory_pointers.last_item, stack_pointers);
163-
164-
atomic_store(&memory_allocator->creating_stack, STACK_CREATING_UNLOCKED);
135+
atomic_store_explicit(&((struct SwiftNetMemoryAllocatorStack*)atomic_load(&memory_allocator->data.last_item))->next, stack, memory_order_release);
136+
atomic_store_explicit(&memory_allocator->data.last_item, stack, memory_order_release);
137+
atomic_store_explicit(&memory_allocator->creating_stack, STACK_CREATING_UNLOCKED, memory_order_release);
165138
}
166139

167140
void* allocator_allocate(struct SwiftNetMemoryAllocator* const memory_allocator) {
@@ -176,7 +149,7 @@ void* allocator_allocate(struct SwiftNetMemoryAllocator* const memory_allocator)
176149

177150
const uint32_t size = atomic_fetch_add(&valid_stack->size, -1);;
178151

179-
void** const ptr_to_data = ((void**)valid_stack->data) + size - 1;
152+
void** const ptr_to_data = ((void**)valid_stack->pointers) + size - 1;
180153

181154
void* item_ptr = *ptr_to_data;
182155

@@ -220,14 +193,15 @@ void allocator_free(struct SwiftNetMemoryAllocator* const memory_allocator, void
220193

221194
const uint32_t size = atomic_fetch_add(&free_stack->size, 1);
222195

223-
((volatile void**)free_stack->data)[size] = memory_location;
196+
((void**)free_stack->pointers)[size] = memory_location;
224197

225198
free_stack_lock(free_stack);
226199
}
227200

228201
void allocator_destroy(struct SwiftNetMemoryAllocator* const memory_allocator) {
229-
for (struct SwiftNetMemoryAllocatorStack* current_stack_pointers = atomic_load(&memory_allocator->free_memory_pointers.first_item); ; ) {
202+
for (struct SwiftNetMemoryAllocatorStack* current_stack_pointers = atomic_load(&memory_allocator->data.first_item); ; ) {
230203
free(current_stack_pointers->data);
204+
free(current_stack_pointers->pointers);
231205

232206
struct SwiftNetMemoryAllocatorStack* const next_stack = atomic_load(&current_stack_pointers->next);
233207
if (next_stack == NULL) {
@@ -239,19 +213,4 @@ void allocator_destroy(struct SwiftNetMemoryAllocator* const memory_allocator) {
239213

240214
current_stack_pointers = next_stack;
241215
}
242-
243-
244-
for (struct SwiftNetMemoryAllocatorStack* current_stack_data = atomic_load(&memory_allocator->data.first_item); ; ) {
245-
free(current_stack_data->data);
246-
247-
struct SwiftNetMemoryAllocatorStack* const next_stack = atomic_load(&current_stack_data->next);
248-
if (next_stack == NULL) {
249-
free(current_stack_data);
250-
break;
251-
}
252-
253-
free(current_stack_data);
254-
255-
current_stack_data = next_stack;
256-
}
257216
}

src/swift_net.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define SWIFT_NET_REQUESTS
2525
#endif
2626

27-
#ifndef SWIFT_NET_DISABLE_DEBUGGING
27+
#ifndef jWIFT_NET_DISABLE_DEBUGGING
2828
#define SWIFT_NET_DEBUG
2929
#endif
3030

@@ -184,10 +184,14 @@ struct SwiftNetSentSuccessfullyCompletedPacketSignal {
184184

185185
struct SwiftNetMemoryAllocatorStack {
186186
_Atomic uint32_t size;
187+
void* pointers;
187188
void* data;
188189
_Atomic(void*) next;
189190
_Atomic(void*) previous;
190191
_Atomic uint8_t owner;
192+
#ifdef SWIFT_NET_DEBUG
193+
uint8_t* ptr_status;
194+
#endif
191195
};
192196

193197
struct SwiftNetChunkStorageManager {
@@ -196,7 +200,6 @@ struct SwiftNetChunkStorageManager {
196200
};
197201

198202
struct SwiftNetMemoryAllocator {
199-
struct SwiftNetChunkStorageManager free_memory_pointers;
200203
struct SwiftNetChunkStorageManager data;
201204
uint32_t item_size;
202205
uint32_t chunk_item_amount;

0 commit comments

Comments
 (0)