Skip to content

Commit 2bd51e5

Browse files
authored
Guard buddy_tree_buddy with BUDDY_EXPERIMENTAL_CHANGE_TRACKING (#135)
1 parent 90c455a commit 2bd51e5

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

buddy_alloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ static bool buddy_tree_can_shrink(struct buddy_tree *t);
393393
* Integration functions
394394
*/
395395

396+
#ifdef BUDDY_EXPERIMENTAL_CHANGE_TRACKING
396397
/* Get a pointer to the parent buddy struct */
397398
static struct buddy* buddy_tree_buddy(struct buddy_tree* t);
399+
#endif /* BUDDY_EXPERIMENTAL_CHANGE_TRACKING */
398400

399401
/*
400402
* Debug functions
@@ -1851,9 +1853,11 @@ static bool buddy_tree_can_shrink(struct buddy_tree *t) {
18511853
return true;
18521854
}
18531855

1856+
#ifdef BUDDY_EXPERIMENTAL_CHANGE_TRACKING
18541857
static struct buddy* buddy_tree_buddy(struct buddy_tree* t) {
18551858
return (struct buddy*)(((unsigned char*)t) - sizeof(struct buddy));
18561859
}
1860+
#endif /* BUDDY_EXPERIMENTAL_CHANGE_TRACKING */
18571861

18581862
static void buddy_tree_debug(struct buddy_tree *t, struct buddy_tree_pos pos,
18591863
size_t start_size) {

tests.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <stdlib.h>
1313
#include <string.h>
1414

15+
#define BUDDY_EXPERIMENTAL_CHANGE_TRACKING
16+
1517
#define BUDDY_ALLOC_IMPLEMENTATION
1618
#include "buddy_alloc.h"
1719
#undef BUDDY_ALLOC_IMPLEMENTATION
@@ -1894,15 +1896,14 @@ void test_buddy_invalid_slot_alignment(void) {
18941896
assert(buddy_embed_alignment(arena, 4096, 3) == NULL);
18951897
}
18961898

1897-
#ifdef BUDDY_EXPERIMENTAL_CHANGE_TRACKING
18981899
struct buddy_change_tracker_context {
1899-
size_t total_length;
19001900
size_t total_calls;
19011901
};
19021902

19031903
void buddy_change_tracker_cb(void* context, unsigned char* addr, size_t length) {
19041904
struct buddy_change_tracker_context *tracker_context = (struct buddy_change_tracker_context *) context;
1905-
tracker_context->total_length += length;
1905+
(void) addr;
1906+
(void) length;
19061907
tracker_context->total_calls++;
19071908
}
19081909

@@ -1913,18 +1914,12 @@ void test_buddy_change_tracking() {
19131914
void *slot;
19141915
START_TEST;
19151916
buddy_enable_change_tracking(buddy, &context, buddy_change_tracker_cb);
1916-
assert(context.total_length == 0);
19171917
assert(context.total_calls == 0);
19181918
slot = buddy_malloc(buddy, 512);
1919-
assert(context.total_length == 2);
19201919
assert(context.total_calls == 2);
19211920
buddy_free(buddy, slot);
1922-
assert(context.total_length == 4);
19231921
assert(context.total_calls == 4);
19241922
}
1925-
#else
1926-
#define test_buddy_change_tracking()
1927-
#endif /* BUDDY_EXPERIMENTAL_CHANGE_TRACKING */
19281923

19291924
void test_buddy_tree_init(void) {
19301925
unsigned char buddy_tree_buf[4096];

0 commit comments

Comments
 (0)