| 1 | #include "internal.h" |
| 2 | |
| 3 | #define arena_check_assert_return_false(expr) \ |
| 4 | do { \ |
| 5 | if (!(expr)) { \ |
| 6 | printf("Arena check " #expr " failed\n"); \ |
| 7 | return false; \ |
| 8 | } \ |
| 9 | } while (0) |
| 10 | |
| 11 | bool arena_check(struct arena *a) { |
| 12 | cc_ignore(a); |
| 13 | |
| 14 | /* When not in DEBUG_ARENA, we have basically no checking to do |
| 15 | * because we have no way to know when the iteration through |
| 16 | * inmem_descs stops */ |
| 17 | #ifdef DEBUG_ARENA |
| 18 | struct arena_seg_inmem_desc *inmem_descs = arena_get_inmem_descs(a); |
| 19 | |
| 20 | size_t agg = ALIGN_UP(sizeof(struct arena_seg_inmem_desc) * a->seg_count, |
| 21 | ARENA_SEG_ALIGN) + |
| 22 | sizeof(struct arena); |
| 23 | |
| 24 | for (uint16_t i = 0; i < a->seg_count; i++) |
| 25 | agg += ALIGN_UP(inmem_descs[i].size, ARENA_SEG_ALIGN); |
| 26 | |
| 27 | arena_check_assert_return_false(agg == a->total_size); |
| 28 | |
| 29 | BITMAP_DECLARE(id_bitmap, ARENA_MAX_SEG) = {0}; |
| 30 | for (uint16_t i = 0; i < a->seg_count; i++) |
| 31 | arena_check_assert_return_false( |
| 32 | !bitmap_test_and_set(id_bitmap, inmem_descs[i].id)); |
| 33 | |
| 34 | arena_check_assert_return_false(a->seg_count == arena_seg_count(a)); |
| 35 | |
| 36 | #endif |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |