-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_debug.c
60 lines (46 loc) · 1.18 KB
/
heap_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <limits.h>
#include "heap_debug.h"
#include "heap_debug_util.h"
#include "heap_internal.h"
/* Debugging routines used for checking heap sanity. */
void
heap_print(FILE *out, const struct heap *heap)
{
size_t prev, ix;
fprintf(out, "heap: {nelems: %zu, elemsize: %zu, "
"order: <fn>, context: %p, elems: ",
heap->nelems, heap->elemsize, heap->context);
hex_print_elems(out, " ", heap->base, heap->nelems, heap->elemsize);
fputs(" }\n", out);
}
void
heap_debug_print(struct heap *heap)
{
return heap_print(stderr, heap);
}
int
heap_node_sanity(struct heap *heap, size_t ix)
{
void *elem;
/* invalid index .. what? */
if (ix >= heap->nelems)
return 0;
elem = offset(heap, ix);
if (childl(ix) < heap->nelems &&
compare(heap, elem, offset(heap, childl(ix))) < 0)
return 0;
if (childr(ix) < heap->nelems &&
compare(heap, elem, offset(heap, childr(ix))) < 0)
return 0;
return 1;
}
int
heap_sanity(struct heap *heap)
{
size_t ix;
for (ix = 0; ix < heap->nelems; ++ix) {
if (!heap_node_sanity(heap, ix))
return 0;
}
return 1;
}