Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions buddy_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ static struct buddy_tree_pos buddy_tree_right_adjacent(struct buddy_tree_pos pos
static size_t buddy_tree_index(struct buddy_tree_pos pos);

/* Return the interval of the deepest positions spanning the indicated position */
static struct buddy_tree_interval buddy_tree_interval(struct buddy_tree *t, struct buddy_tree_pos pos);
static struct buddy_tree_interval to_buddy_tree_interval(struct buddy_tree *t, struct buddy_tree_pos pos);

/* Checks if one interval contains another */
static bool buddy_tree_interval_contains(struct buddy_tree_interval outer,
Expand Down Expand Up @@ -430,7 +430,7 @@ struct bitset_range {
uint8_t to_index;
};

static inline struct bitset_range bitset_range(size_t from_pos, size_t to_pos);
static inline struct bitset_range to_bitset_range(size_t from_pos, size_t to_pos);

static void bitset_set_range(unsigned char *bitset, struct bitset_range range);

Expand Down Expand Up @@ -515,7 +515,7 @@ static unsigned char *address_for_position(struct buddy *buddy, struct buddy_tre
static struct buddy_tree_pos position_for_address(struct buddy *buddy, const unsigned char *addr);
static unsigned char *buddy_main(struct buddy *buddy);
static unsigned int buddy_relative_mode(struct buddy *buddy);
static struct buddy_tree *buddy_tree(struct buddy *buddy);
static struct buddy_tree *buddy_tree_for(struct buddy *buddy);
static size_t buddy_effective_memory_size(struct buddy *buddy);
static size_t buddy_virtual_slots(struct buddy *buddy);
static void buddy_toggle_virtual_slots(struct buddy *buddy, unsigned int state);
Expand Down Expand Up @@ -664,7 +664,7 @@ static struct buddy *buddy_resize_standard(struct buddy *buddy, size_t new_memor

/* Calculate new tree order and resize it */
new_buddy_tree_order = buddy_tree_order_for_memory(new_memory_size, buddy->alignment);
buddy_tree_resize(buddy_tree(buddy), (uint8_t) new_buddy_tree_order);
buddy_tree_resize(buddy_tree_for(buddy), (uint8_t) new_buddy_tree_order);

/* Store the new memory size and reconstruct any virtual slots */
buddy->memory_size = new_memory_size;
Expand Down Expand Up @@ -726,7 +726,7 @@ bool buddy_is_full(struct buddy *buddy) {
if (buddy == NULL) {
return false;
}
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = buddy_tree_root();
return buddy_tree_status(tree, pos) == buddy_tree_order(tree);
}
Expand All @@ -740,7 +740,7 @@ size_t buddy_arena_size(struct buddy *buddy) {

size_t buddy_arena_free_size(struct buddy *buddy) {
size_t result = 0;
struct buddy_tree *tree = buddy_tree(buddy);
struct buddy_tree *tree = buddy_tree_for(buddy);
size_t tree_order = buddy_tree_order(tree);

struct buddy_tree_walk_state state = buddy_tree_walk_state_root();
Expand Down Expand Up @@ -792,7 +792,7 @@ void *buddy_malloc(struct buddy *buddy, size_t requested_size) {
}

target_depth = depth_for_size(buddy, requested_size);
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = buddy_tree_find_free(tree, (uint8_t) target_depth);

if (! buddy_tree_valid(tree, pos)) {
Expand Down Expand Up @@ -854,7 +854,7 @@ void *buddy_realloc(struct buddy *buddy, void *ptr, size_t requested_size, bool
}

/* Find the position tracking this address */
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
origin = position_for_address(buddy, (unsigned char *) ptr);
if (! buddy_tree_valid(tree, origin)) {
return NULL;
Expand Down Expand Up @@ -922,7 +922,7 @@ void buddy_free(struct buddy *buddy, void *ptr) {
}

/* Find the position tracking this address */
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = position_for_address(buddy, dst);

if (! buddy_tree_valid(tree, pos)) {
Expand Down Expand Up @@ -953,7 +953,7 @@ enum buddy_safe_free_status buddy_safe_free(struct buddy* buddy, void* ptr, size
}

/* Find an allocated position tracking this address */
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = position_for_address(buddy, dst);

if (!buddy_tree_valid(tree, pos)) {
Expand Down Expand Up @@ -1002,7 +1002,7 @@ size_t buddy_alloc_size(struct buddy *buddy, void *ptr) {
}

/* Find an allocated position tracking this address */
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = position_for_address(buddy, dst);

if (!buddy_tree_valid(tree, pos)) {
Expand Down Expand Up @@ -1039,7 +1039,7 @@ void *buddy_walk(struct buddy *buddy,
}
main = buddy_main(buddy);
effective_memory_size = buddy_effective_memory_size(buddy);
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
tree_order = buddy_tree_order(tree);

state = buddy_tree_walk_state_root();
Expand Down Expand Up @@ -1088,12 +1088,12 @@ unsigned char buddy_fragmentation(struct buddy *buddy) {
if (buddy == NULL) {
return 0;
}
return buddy_tree_fragmentation(buddy_tree(buddy));
return buddy_tree_fragmentation(buddy_tree_for(buddy));
}

#ifdef BUDDY_EXPERIMENTAL_CHANGE_TRACKING
void buddy_enable_change_tracking(struct buddy* buddy, void* context, void (*tracker) (void*, unsigned char*, size_t)) {
struct buddy_tree *t = buddy_tree(buddy);
struct buddy_tree *t = buddy_tree_for(buddy);
struct buddy_change_tracker *header = (struct buddy_change_tracker *) buddy_main(buddy);

/* Allocate memory for the change tracking header */
Expand Down Expand Up @@ -1127,7 +1127,7 @@ static inline size_t size_for_depth(struct buddy *buddy, size_t depth) {
return ceiling_power_of_two(buddy->memory_size) >> (depth-1);
}

static struct buddy_tree *buddy_tree(struct buddy *buddy) {
static struct buddy_tree *buddy_tree_for(struct buddy *buddy) {
return (struct buddy_tree*) ((unsigned char *)buddy + sizeof(*buddy));
}

Expand All @@ -1152,7 +1152,7 @@ static unsigned char *address_for_position(struct buddy *buddy, struct buddy_tre

static struct buddy_tree_pos deepest_position_for_offset(struct buddy *buddy, size_t offset) {
size_t index = offset / buddy->alignment;
struct buddy_tree_pos pos = buddy_tree_leftmost_child(buddy_tree(buddy));
struct buddy_tree_pos pos = buddy_tree_leftmost_child(buddy_tree_for(buddy));
pos.index += index;
return pos;
}
Expand All @@ -1170,7 +1170,7 @@ static struct buddy_tree_pos position_for_address(struct buddy *buddy, const uns
return INVALID_POS; /* invalid alignment */
}

tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = deepest_position_for_offset(buddy, offset);

/* Find the actual allocated position tracking this address */
Expand Down Expand Up @@ -1216,7 +1216,7 @@ static void buddy_toggle_virtual_slots(struct buddy *buddy, unsigned int state)
/* Node memory size is already aligned to buddy->alignment */
delta = effective_memory_size - memory_size;

tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
pos = buddy_tree_right_child(buddy_tree_root());
while (delta) {
size_t current_pos_size = size_for_depth(buddy, buddy_tree_depth(pos));
Expand Down Expand Up @@ -1273,7 +1273,7 @@ static void buddy_toggle_range_reservation(struct buddy *buddy, void *ptr, size_
}

/* Find the deepest position tracking this address */
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
offset = (size_t) (dst - main);
pos = deepest_position_for_offset(buddy, offset);

Expand Down Expand Up @@ -1307,20 +1307,20 @@ static bool buddy_is_free(struct buddy *buddy, size_t from) {
to = effective_memory_size -
((virtual_slots ? (virtual_slots + 1) : 1) * buddy->alignment);

tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);

query_range.from = deepest_position_for_offset(buddy, from);
query_range.to = deepest_position_for_offset(buddy, to);

pos = deepest_position_for_offset(buddy, from);
while(buddy_tree_valid(tree, pos) && (pos.index < query_range.to.index)) {
struct buddy_tree_interval current_test_range = buddy_tree_interval(tree, pos);
struct buddy_tree_interval current_test_range = to_buddy_tree_interval(tree, pos);
struct buddy_tree_interval parent_test_range =
buddy_tree_interval(tree, buddy_tree_parent(pos));
to_buddy_tree_interval(tree, buddy_tree_parent(pos));
while(buddy_tree_interval_contains(query_range, parent_test_range)) {
pos = buddy_tree_parent(pos);
current_test_range = parent_test_range;
parent_test_range = buddy_tree_interval(tree, buddy_tree_parent(pos));
parent_test_range = to_buddy_tree_interval(tree, buddy_tree_parent(pos));
}
/* pos is now tracking an overlapping segment */
if (! buddy_tree_is_free(tree, pos)) {
Expand Down Expand Up @@ -1371,7 +1371,7 @@ void buddy_debug(struct buddy *buddy) {
BUDDY_PRINTF("\n");
BUDDY_PRINTF("virtual slots: %zu\n", buddy_virtual_slots(buddy));
BUDDY_PRINTF("allocator tree follows:\n");
buddy_tree_debug(buddy_tree(buddy), buddy_tree_root(), buddy_effective_memory_size(buddy));
buddy_tree_debug(buddy_tree_for(buddy), buddy_tree_root(), buddy_effective_memory_size(buddy));
}

/*
Expand Down Expand Up @@ -1522,8 +1522,8 @@ static void buddy_tree_grow(struct buddy_tree *t, uint8_t desired_order) {

/* Clear right section */
bitset_clear_range(buddy_tree_bits(t),
bitset_range(next_internal.bitset_location + (next_internal.local_offset * node_count),
next_internal.bitset_location + (next_internal.local_offset * node_count * 2) - 1));
to_bitset_range(next_internal.bitset_location + (next_internal.local_offset * node_count),
next_internal.bitset_location + (next_internal.local_offset * node_count * 2) - 1));

/* Handle the upper level */
current_order -= 1u;
Expand Down Expand Up @@ -1676,11 +1676,11 @@ static inline size_t buddy_tree_size_for_order(struct buddy_tree *t,

static void write_to_internal_position(struct buddy_tree* t, struct internal_position pos, size_t value) {
unsigned char *bitset = buddy_tree_bits(t);
struct bitset_range clear_range = bitset_range(pos.bitset_location, pos.bitset_location + pos.local_offset - 1);
struct bitset_range clear_range = to_bitset_range(pos.bitset_location, pos.bitset_location + pos.local_offset - 1);

bitset_clear_range(bitset, clear_range);
if (value) {
bitset_set_range(bitset, bitset_range(pos.bitset_location, pos.bitset_location+value-1));
bitset_set_range(bitset, to_bitset_range(pos.bitset_location, pos.bitset_location+value-1));
}

#ifdef BUDDY_EXPERIMENTAL_CHANGE_TRACKING
Expand All @@ -1693,14 +1693,14 @@ static size_t read_from_internal_position(unsigned char *bitset, struct internal
if (! bitset_test(bitset, pos.bitset_location)) {
return 0; /* Fast test without complete extraction */
}
return bitset_count_range(bitset, bitset_range(pos.bitset_location, pos.bitset_location+pos.local_offset-1));
return bitset_count_range(bitset, to_bitset_range(pos.bitset_location, pos.bitset_location+pos.local_offset-1));
}

static inline unsigned char compare_with_internal_position(unsigned char *bitset, struct internal_position pos, size_t value) {
return bitset_test(bitset, pos.bitset_location+value-1);
}

static struct buddy_tree_interval buddy_tree_interval(struct buddy_tree *t, struct buddy_tree_pos pos) {
static struct buddy_tree_interval to_buddy_tree_interval(struct buddy_tree *t, struct buddy_tree_pos pos) {
struct buddy_tree_interval result;
size_t depth;

Expand Down Expand Up @@ -2050,7 +2050,7 @@ static const uint8_t bitset_char_mask[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 128},
};

static inline struct bitset_range bitset_range(size_t from_pos, size_t to_pos) {
static inline struct bitset_range to_bitset_range(size_t from_pos, size_t to_pos) {
struct bitset_range range = {0};
range.from_bucket = from_pos / CHAR_BIT;
range.to_bucket = to_pos / CHAR_BIT;
Expand Down
22 changes: 11 additions & 11 deletions tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ void test_bitset_range(void) {
for (size_t i = 0; i < bitset_length; i++) {
for (size_t j = 0; j <= i; j++) {
memset(buf, 0, 4);
bitset_set_range(buf, bitset_range(j, i));
bitset_set_range(buf, to_bitset_range(j, i));
for (size_t k = 0; k < bitset_length; k++) {
if ((k >= j) && (k <= i)) {
assert(bitset_test(buf, k));
} else {
assert(!bitset_test(buf, k));
}
}
bitset_clear_range(buf, bitset_range(j, i));
bitset_clear_range(buf, to_bitset_range(j, i));
for (size_t k = j; k < i; k++) {
assert(!bitset_test(buf, k));
}
Expand Down Expand Up @@ -186,16 +186,16 @@ void test_bitset_shift(void) {
void test_bitset_shift_invalid(void) {
unsigned char buf[4096] = {0};
START_TEST;
bitset_set_range(buf, bitset_range(1, 0)); /* no-op */
bitset_set_range(buf, to_bitset_range(1, 0)); /* no-op */
assert(!bitset_test(buf, 0));
assert(!bitset_test(buf, 1));
bitset_set_range(buf, bitset_range(0, 1));
bitset_set_range(buf, to_bitset_range(0, 1));
assert(bitset_test(buf, 0));
assert(bitset_test(buf, 1));
bitset_clear_range(buf, bitset_range(1, 0)) /* no-op */;
bitset_clear_range(buf, to_bitset_range(1, 0)) /* no-op */;
assert(bitset_test(buf, 0));
assert(bitset_test(buf, 1));
bitset_clear_range(buf, bitset_range(0, 1));
bitset_clear_range(buf, to_bitset_range(0, 1));
assert(!bitset_test(buf, 0));
assert(!bitset_test(buf, 1));
}
Expand Down Expand Up @@ -2499,10 +2499,10 @@ void test_buddy_tree_interval(void) {
START_TEST;
t = buddy_tree_init(buddy_tree_buf, 3);
pos = buddy_tree_leftmost_child(t);
interval = buddy_tree_interval(t, pos);
interval = to_buddy_tree_interval(t, pos);
assert(interval.from.index == pos.index);
assert(interval.to.index == pos.index);
interval = buddy_tree_interval(t, buddy_tree_parent(pos));
interval = to_buddy_tree_interval(t, buddy_tree_parent(pos));
assert(interval.from.index == pos.index);
assert(interval.to.index == buddy_tree_right_adjacent(pos).index);
}
Expand All @@ -2515,8 +2515,8 @@ void test_buddy_tree_interval_contains(void) {
START_TEST;
t = buddy_tree_init(buddy_tree_buf, 3);
pos = buddy_tree_leftmost_child(t);
interval_low = buddy_tree_interval(t, pos);
interval_high = buddy_tree_interval(t, buddy_tree_parent(pos));
interval_low = to_buddy_tree_interval(t, pos);
interval_high = to_buddy_tree_interval(t, buddy_tree_parent(pos));
assert(buddy_tree_interval_contains(interval_low, interval_low) == 1);
assert(buddy_tree_interval_contains(interval_high, interval_low) == 1);
assert(buddy_tree_interval_contains(interval_high, interval_high) == 1);
Expand All @@ -2529,7 +2529,7 @@ void test_buddy_tree_buddy() {
struct buddy_tree *tree;
START_TEST;
buddy = buddy_embed(buf, 4096);
tree = buddy_tree(buddy);
tree = buddy_tree_for(buddy);
assert(buddy_tree_buddy(tree) == buddy);
}

Expand Down
Loading