Skip to content

Commit 59e0620

Browse files
authored
Add buddy_alloc_size function. (#144)
1 parent 4e836a6 commit 59e0620

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

buddy_alloc.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ enum buddy_safe_free_status {
138138
/* A (safer) free with a size. Will not free unless the size fits the target span. */
139139
enum buddy_safe_free_status buddy_safe_free(struct buddy *buddy, void *ptr, size_t requested_size);
140140

141+
/* Reports the allocation size. This could be bigger than the requested size,
142+
it's the exact size that this allocation occupies in the arena.
143+
Returns 0 on failure, and a non-zero value on success. */
144+
size_t buddy_alloc_size(struct buddy *buddy, void *ptr);
145+
141146
/*
142147
* Reservation functions
143148
*/
@@ -975,6 +980,34 @@ enum buddy_safe_free_status buddy_safe_free(struct buddy* buddy, void* ptr, size
975980
return BUDDY_SAFE_FREE_SUCCESS;
976981
}
977982

983+
size_t buddy_alloc_size(struct buddy *buddy, void *ptr) {
984+
unsigned char* dst, * main;
985+
struct buddy_tree* tree;
986+
struct buddy_tree_pos pos;
987+
988+
if (buddy == NULL) {
989+
return 0;
990+
}
991+
if (ptr == NULL) {
992+
return 0;
993+
}
994+
dst = (unsigned char*)ptr;
995+
main = buddy_main(buddy);
996+
if ((dst < main) || (dst >= (main + buddy->memory_size))) {
997+
return 0;
998+
}
999+
1000+
/* Find an allocated position tracking this address */
1001+
tree = buddy_tree(buddy);
1002+
pos = position_for_address(buddy, dst);
1003+
1004+
if (!buddy_tree_valid(tree, pos)) {
1005+
return 0;
1006+
}
1007+
1008+
return size_for_depth(buddy, pos.depth);
1009+
}
1010+
9781011
void buddy_reserve_range(struct buddy *buddy, void *ptr, size_t requested_size) {
9791012
buddy_toggle_range_reservation(buddy, ptr, requested_size, 1);
9801013
}

tests.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,145 @@ void test_buddy_safe_free_invalid_free_07(void) {
11091109
free(buddy_buf_control);
11101110
}
11111111

1112+
void test_buddy_alloc_size_coverage(void) {
1113+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1114+
unsigned char data_buf[4096];
1115+
struct buddy *buddy;
1116+
START_TEST;
1117+
buddy = buddy_init(buddy_buf, data_buf+1024, 1024);
1118+
assert(buddy != NULL);
1119+
assert(buddy_alloc_size(NULL, NULL) == 0);
1120+
assert(buddy_alloc_size(buddy, NULL) == 0);
1121+
assert(buddy_alloc_size(buddy, data_buf) == 0);
1122+
assert(buddy_alloc_size(buddy, data_buf+2048) == 0);
1123+
assert(buddy_alloc_size(buddy, data_buf+1024) == 0);
1124+
free(buddy_buf);
1125+
}
1126+
1127+
void test_buddy_alloc_size_01(void) {
1128+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1129+
unsigned char data_buf[4096];
1130+
struct buddy *buddy;
1131+
void *slot;
1132+
START_TEST;
1133+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1134+
slot = buddy_malloc(buddy, 64);
1135+
assert(buddy_alloc_size(buddy, slot) == 64);
1136+
buddy_free(buddy, slot);
1137+
slot = buddy_malloc(buddy, 128);
1138+
assert(buddy_alloc_size(buddy, slot) == 128);
1139+
buddy_free(buddy, slot);
1140+
slot = buddy_malloc(buddy, 256);
1141+
assert(buddy_alloc_size(buddy, slot) == 256);
1142+
buddy_free(buddy, slot);
1143+
free(buddy_buf);
1144+
}
1145+
1146+
void test_buddy_alloc_size_02(void) {
1147+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1148+
unsigned char data_buf[4096];
1149+
struct buddy *buddy;
1150+
void *slot;
1151+
START_TEST;
1152+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1153+
slot = buddy_malloc(buddy, 63);
1154+
assert(buddy_alloc_size(buddy, slot) == 64);
1155+
buddy_free(buddy, slot);
1156+
slot = buddy_malloc(buddy, 127);
1157+
assert(buddy_alloc_size(buddy, slot) == 128);
1158+
buddy_free(buddy, slot);
1159+
slot = buddy_malloc(buddy, 255);
1160+
assert(buddy_alloc_size(buddy, slot) == 256);
1161+
buddy_free(buddy, slot);
1162+
free(buddy_buf);
1163+
}
1164+
1165+
void test_buddy_alloc_size_03(void) {
1166+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1167+
unsigned char data_buf[4096];
1168+
struct buddy *buddy;
1169+
void *slot1, *slot2, *slot3;
1170+
START_TEST;
1171+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1172+
slot1 = buddy_malloc(buddy, 63);
1173+
slot2 = buddy_malloc(buddy, 127);
1174+
slot3 = buddy_malloc(buddy, 255);
1175+
assert(buddy_alloc_size(buddy, slot1) == 64);
1176+
assert(buddy_alloc_size(buddy, slot2) == 128);
1177+
assert(buddy_alloc_size(buddy, slot3) == 256);
1178+
buddy_free(buddy, slot1);
1179+
buddy_free(buddy, slot2);
1180+
buddy_free(buddy, slot3);
1181+
free(buddy_buf);
1182+
}
1183+
1184+
void test_buddy_alloc_size_invalid_01(void) {
1185+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1186+
unsigned char data_buf[4096];
1187+
struct buddy *buddy;
1188+
void *slot;
1189+
START_TEST;
1190+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1191+
slot = buddy_malloc(buddy, 64);
1192+
assert(slot == data_buf);
1193+
assert(buddy_alloc_size(buddy, (unsigned char *)slot - 1) == 0);
1194+
buddy_free(buddy, slot);
1195+
free(buddy_buf);
1196+
}
1197+
1198+
void test_buddy_alloc_size_invalid_02(void) {
1199+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1200+
unsigned char data_buf[4096];
1201+
struct buddy *buddy;
1202+
void *slot;
1203+
START_TEST;
1204+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1205+
slot = buddy_malloc(buddy, 64);
1206+
assert(slot == data_buf);
1207+
assert(buddy_alloc_size(buddy, (unsigned char *)slot + 4096) == 0);
1208+
buddy_free(buddy, slot);
1209+
free(buddy_buf);
1210+
}
1211+
1212+
void test_buddy_alloc_size_invalid_03(void) {
1213+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1214+
unsigned char data_buf[4096];
1215+
struct buddy *buddy;
1216+
void *slot;
1217+
START_TEST;
1218+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1219+
slot = buddy_malloc(buddy, 64);
1220+
assert(buddy_alloc_size(buddy, (unsigned char *)slot + 1) == 0);
1221+
buddy_free(buddy, slot);
1222+
free(buddy_buf);
1223+
}
1224+
1225+
void test_buddy_alloc_size_invalid_04(void) {
1226+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1227+
unsigned char data_buf[4096];
1228+
struct buddy *buddy;
1229+
void *slot;
1230+
START_TEST;
1231+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1232+
slot = buddy_malloc(buddy, 64);
1233+
assert(buddy_alloc_size(buddy, (unsigned char *)slot + 4095) == 0);
1234+
buddy_free(buddy, slot);
1235+
free(buddy_buf);
1236+
}
1237+
1238+
void test_buddy_alloc_size_invalid_05(void) {
1239+
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
1240+
unsigned char data_buf[4096];
1241+
struct buddy *buddy;
1242+
void *slot;
1243+
START_TEST;
1244+
buddy = buddy_init(buddy_buf, data_buf, 4096);
1245+
slot = buddy_malloc(buddy, 64);
1246+
buddy_free(buddy, slot);
1247+
assert(buddy_alloc_size(buddy, slot) == 0);
1248+
free(buddy_buf);
1249+
}
1250+
11121251
void test_buddy_demo(void) {
11131252
size_t arena_size = 65536;
11141253
/* You need space for the metadata and for the arena */
@@ -2503,6 +2642,16 @@ int main(void) {
25032642
test_buddy_safe_free_invalid_free_06();
25042643
test_buddy_safe_free_invalid_free_07();
25052644

2645+
test_buddy_alloc_size_coverage();
2646+
test_buddy_alloc_size_01();
2647+
test_buddy_alloc_size_02();
2648+
test_buddy_alloc_size_03();
2649+
test_buddy_alloc_size_invalid_01();
2650+
test_buddy_alloc_size_invalid_02();
2651+
test_buddy_alloc_size_invalid_03();
2652+
test_buddy_alloc_size_invalid_04();
2653+
test_buddy_alloc_size_invalid_05();
2654+
25062655
test_buddy_demo();
25072656
test_buddy_demo_embedded();
25082657

0 commit comments

Comments
 (0)