Skip to content

Commit fa0c124

Browse files
deadlightrealMorcules
authored andcommitted
Made memory leak checking compile only on testing
1 parent 7e20f88 commit fa0c124

File tree

8 files changed

+32
-16
lines changed

8 files changed

+32
-16
lines changed

build/build_for_testing.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
cmake ../src -DCMAKE_BUILD_TYPE=Debug -DSANITIZER=thread -DSWIFT_NET_INTERNAL_TESTING
1+
cmake ../src -DCMAKE_BUILD_TYPE=Debug -DSANITIZER=thread -DSWIFT_NET_INTERNAL_TESTING=ON
22
make -B -j8

src/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,16 @@ else()
4848
endif()
4949

5050
add_library(swiftnet STATIC ${SOURCE_FILES})
51-
target_compile_options(swiftnet PRIVATE -O0)
52-
target_link_options(swiftnet PRIVATE -O0)
51+
52+
if (SWIFT_NET_INTERNAL_TESTING)
53+
add_compile_definitions(SWIFT_NET_INTERNAL_TESTING)
54+
55+
target_compile_options(swiftnet PRIVATE -O0)
56+
target_link_options(swiftnet PRIVATE -O0)
57+
else()
58+
target_compile_options(swiftnet PRIVATE -O3)
59+
target_link_options(swiftnet PRIVATE -O3)
60+
endif()
5361

5462
if(DEFINED ENV{VCPKG_ROOT} AND DEFINED CMAKE_VCPKG_TARGET_TRIPLET)
5563
set(VCPKG_INCLUDE_DIR "$ENV{VCPKG_ROOT}/installed/${CMAKE_VCPKG_TARGET_TRIPLET}/include")

src/cleanup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ void swiftnet_cleanup() {
3838

3939
allocator_destroy(&listener_memory_allocator);
4040

41+
#ifdef SWIFT_NET_INTERNAL_TESTING
4142
printf("Bytes leaked: %d\nItems leaked: %d\n", bytes_leaked, items_leaked);
43+
#endif
4244
}

src/initialize_swift_net.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
#ifdef SWIFT_NET_DEBUG
1313
struct SwiftNetDebugger debugger = {.flags = 0};
14+
#endif
15+
16+
#ifdef SWIFT_NET_INTERNAL_TESTING
1417
uint32_t bytes_leaked = 0;
1518
uint32_t items_leaked = 0;
1619
#endif

src/internal/datatype_allocator.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ static inline void free_stack_lock(struct SwiftNetMemoryAllocatorStack* const st
1010
atomic_store_explicit(&stack->owner, ALLOCATOR_STACK_FREE, memory_order_release);
1111
}
1212

13+
#ifdef SWIFT_NET_INTERNAL_TESTING
1314
static inline void unlock_ptr_status(struct SwiftNetMemoryAllocatorStack* const stack) {
1415
atomic_store_explicit(&stack->accessing_ptr_status, false, memory_order_release);
1516
}
@@ -27,6 +28,7 @@ static inline void lock_ptr_status(struct SwiftNetMemoryAllocatorStack* const st
2728
target = false;
2829
}
2930
}
31+
#endif
3032

3133
struct SwiftNetMemoryAllocatorStack* const find_free_pointer_stack(const struct SwiftNetMemoryAllocator* const allocator) {
3234
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)) {
@@ -111,7 +113,7 @@ struct SwiftNetMemoryAllocator allocator_create(const uint32_t item_size, const
111113

112114
atomic_store_explicit(&new_allocator.creating_stack, STACK_CREATING_UNLOCKED, memory_order_release);
113115

114-
#ifdef SWIFT_NET_DEBUG
116+
#ifdef SWIFT_NET_INTERNAL_TESTING
115117
atomic_store_explicit(&first_stack->accessing_ptr_status, false, memory_order_release);
116118
first_stack->ptr_status = calloc(sizeof(uint8_t), (chunk_item_amount / 8) + 1);
117119
#endif
@@ -157,7 +159,7 @@ static void create_new_stack(struct SwiftNetMemoryAllocator* const memory_alloca
157159
((void **)allocated_memory_pointers)[i] = (uint8_t*)allocated_memory + (i * item_size);
158160
}
159161

160-
#ifdef SWIFT_NET_DEBUG
162+
#ifdef SWIFT_NET_INTERNAL_TESTING
161163
atomic_store_explicit(&stack->accessing_ptr_status, false, memory_order_release);
162164
stack->ptr_status = calloc(sizeof(uint8_t), (chunk_item_amount / 8) + 1);
163165
#endif
@@ -183,7 +185,7 @@ void* allocator_allocate(struct SwiftNetMemoryAllocator* const memory_allocator)
183185

184186
void* item_ptr = *ptr_to_data;
185187

186-
#ifdef SWIFT_NET_DEBUG
188+
#ifdef SWIFT_NET_INTERNAL_TESTING
187189
const uint32_t offset = item_ptr - valid_stack->data;
188190
const uint32_t index = offset / memory_allocator->item_size;
189191

@@ -202,7 +204,7 @@ void* allocator_allocate(struct SwiftNetMemoryAllocator* const memory_allocator)
202204
return item_ptr;
203205
}
204206

205-
#ifdef SWIFT_NET_DEBUG
207+
#ifdef SWIFT_NET_INTERNAL_TESTING
206208
static inline bool is_already_free(struct SwiftNetMemoryAllocator* const memory_allocator, void* const memory_location) {
207209
for (struct SwiftNetMemoryAllocatorStack* stack = atomic_load_explicit(&memory_allocator->data.first_item, memory_order_acquire); stack != NULL; stack = atomic_load_explicit(&stack->next, memory_order_acquire)) {
208210
if (
@@ -235,7 +237,7 @@ void* allocator_allocate(struct SwiftNetMemoryAllocator* const memory_allocator)
235237
#endif
236238

237239
void allocator_free(struct SwiftNetMemoryAllocator* const memory_allocator, void* const memory_location) {
238-
#ifdef SWIFT_NET_DEBUG
240+
#ifdef SWIFT_NET_INTERNAL_TESTING
239241
const bool already_free = is_already_free(memory_allocator, memory_location);
240242

241243
if (already_free == true) {
@@ -257,7 +259,7 @@ void allocator_free(struct SwiftNetMemoryAllocator* const memory_allocator, void
257259

258260
((void**)free_stack->pointers)[size] = memory_location;
259261

260-
#ifdef SWIFT_NET_DEBUG
262+
#ifdef SWIFT_NET_INTERNAL_TESTING
261263
const uint32_t offset = memory_location - free_stack->data;
262264
const uint32_t index = offset / memory_allocator->item_size;
263265

@@ -286,7 +288,7 @@ void allocator_destroy(struct SwiftNetMemoryAllocator* const memory_allocator) {
286288
break;
287289
}
288290

289-
#ifdef SWIFT_NET_DEBUG
291+
#ifdef SWIFT_NET_INTERNAL_TESTING
290292
lock_ptr_status(current_stack);
291293

292294
const uint32_t total_items = memory_allocator->chunk_item_amount;

src/internal/internal.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@ extern int swiftnet_pcap_send(pcap_t *pcap, const uint8_t *data, int len);
142142

143143
extern void* check_existing_listener(const char* interface_name, void* const connection, const enum ConnectionType connection_type, const bool loopback);
144144

145-
#ifdef SWIFT_NET_DEBUG
146-
extern struct SwiftNetDebugger debugger;
147-
145+
#ifdef SWIFT_NET_INTERNAL_TESTING
148146
extern uint32_t bytes_leaked;
149147
extern uint32_t items_leaked;
148+
#endif
149+
150+
#ifdef SWIFT_NET_DEBUG
151+
extern struct SwiftNetDebugger debugger;
150152

151153
static inline bool check_debug_flag(enum SwiftNetDebugFlags flag) {
152154
return (debugger.flags & flag) != 0;

src/swift_net.h

Lines changed: 2 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 jWIFT_NET_DISABLE_DEBUGGING
27+
#ifndef SWIFT_NET_DISABLE_DEBUGGING
2828
#define SWIFT_NET_DEBUG
2929
#endif
3030

@@ -189,7 +189,7 @@ struct SwiftNetMemoryAllocatorStack {
189189
_Atomic(void*) next;
190190
_Atomic(void*) previous;
191191
_Atomic uint8_t owner;
192-
#ifdef SWIFT_NET_DEBUG
192+
#ifdef SWIFT_NET_INTERNAL_TESTING
193193
uint8_t* ptr_status;
194194
_Atomic bool accessing_ptr_status;
195195
#endif

tests/build_tests.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
../build/build_for_testing.sh
21
cmake . -DCMAKE_BUILD_TYPE=Debug -DSANITIZER=thread
32
make -B -j8

0 commit comments

Comments
 (0)