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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ out/*

# visual studio cmake settings
CMakeSettings.json

# Pelles C project files
# Pelles C is awesome by the way, check it out
*.ppj
*.ppx
*.tag
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tests.out: $(TESTS_SRC) $(LIB_SRC) test-cppcheck check-recursion
$(CC) $(CFLAGS) $(TESTS_SRC) -o $@

test-cppcheck: $(TESTS_SRC)
$(CPPCHECK) --error-exitcode=1 --quiet $^
$(CPPCHECK) --error-exitcode=1 --inline-suppr --quiet $^

test-cpp-translation-unit: $(TESTCXX_SRC)
$(CXX) $(CXXFLAGS) $(TESTCXX_SRC) -o $@
Expand Down
2 changes: 1 addition & 1 deletion bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ void *freeing_callback(void *ctx, void *addr, size_t slot_size, size_t allocated
struct buddy *buddy = (struct buddy*) ctx;
buddy_free(buddy, addr);
return NULL;
}
}
50 changes: 34 additions & 16 deletions buddy_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ void bitset_debug(unsigned char *bitset, size_t length);
/* Returns the number of set bits in the given byte */
static unsigned int popcount_byte(unsigned char b);

/* Count the number of trailing zeroes in the given value */
static unsigned char count_trailing_zeroes(size_t val);

/* Returns the index of the highest bit set (1-based) */
static size_t highest_bit_position(size_t value);

Expand Down Expand Up @@ -760,6 +763,7 @@ static unsigned int is_valid_alignment(size_t alignment) {
}

static size_t buddy_tree_order_for_memory(size_t memory_size, size_t alignment) {
// cppcheck-suppress zerodiv
size_t blocks = memory_size / alignment;
return highest_bit_position(ceiling_power_of_two(blocks));
}
Expand Down Expand Up @@ -1106,16 +1110,16 @@ void buddy_enable_change_tracking(struct buddy* buddy, void* context, void (*tra


static size_t depth_for_size(struct buddy *buddy, size_t requested_size) {
size_t depth, effective_memory_size;
size_t depth, effective_memory_size, p2_of_requested_size;
if (requested_size < buddy->alignment) {
requested_size = buddy->alignment;
}
depth = 1;
effective_memory_size = buddy_effective_memory_size(buddy);
while ((effective_memory_size / requested_size) >> 1u) {
depth++;
effective_memory_size >>= 1u;
}

p2_of_requested_size = ceiling_power_of_two(requested_size);
depth = count_trailing_zeroes(effective_memory_size) + 1
- count_trailing_zeroes(p2_of_requested_size);
return depth;
}

Expand Down Expand Up @@ -2133,21 +2137,35 @@ void bitset_debug(unsigned char *bitset, size_t length) {
Bits
*/

static const unsigned char popcount_lookup[256] = {
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
};

static inline unsigned int popcount_byte(unsigned char b) {
static const unsigned char popcount_lookup[256] = {
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
};
return popcount_lookup[b];
}

static unsigned char count_trailing_zeroes(size_t val) {
/* Implementation from https://www.chessprogramming.org/BitScan */
static const signed char lookup67[67+1] = {
64, 0, 1, 39, 2, 15, 40, 23,
3, 12, 16, 59, 41, 19, 24, 54,
4, -1, 13, 10, 17, 62, 60, 28,
42, 30, 20, 51, 25, 44, 55, 47,
5, 32, -1, 38, 14, 22, 11, 58,
18, 53, 63, 9, 61, 27, 29, 50,
43, 46, 31, 37, 21, 57, 52, 8,
26, 49, 45, 36, 56, 7, 48, 35,
6, 34, 33, -1 };
return ((unsigned char) lookup67[(val & -val) % 67]);
}

/* Returns the highest set bit position for the given value. Returns zero for zero. */
static size_t highest_bit_position(size_t value) {
size_t result = 0;
Expand Down
Loading