8 #ifdef HAVE_VALGRIND_SUPPORT
17 typedef size_t ABTI_valgrind_id;
19 typedef struct ABTI_valgrind_id_list_t {
21 ABTI_valgrind_id valgrind_id;
22 struct ABTI_valgrind_id_list_t *p_next;
23 } ABTI_valgrind_id_list;
26 static ABTD_spinlock g_valgrind_id_list_lock =
27 ABTD_SPINLOCK_STATIC_INITIALIZER();
28 static int g_num_malloc_failures = 0;
29 static ABTI_valgrind_id_list *gp_valgrind_id_list_head = NULL;
30 static ABTI_valgrind_id_list *gp_valgrind_id_list_tail = NULL;
32 #include <valgrind/valgrind.h>
34 void ABTI_valgrind_register_stack(
const void *p_stack,
size_t size)
39 const void *p_start = (
char *)(p_stack);
40 const void *p_end = (
char *)(p_stack) + size;
42 ABTD_spinlock_acquire(&g_valgrind_id_list_lock);
43 ABTI_valgrind_id_list *p_valgrind_id_list =
44 (ABTI_valgrind_id_list *)malloc(
sizeof(ABTI_valgrind_id_list));
45 if (p_valgrind_id_list) {
46 ABTI_valgrind_id valgrind_id = VALGRIND_STACK_REGISTER(p_start, p_end);
47 p_valgrind_id_list->p_stack = p_stack;
48 p_valgrind_id_list->valgrind_id = valgrind_id;
49 p_valgrind_id_list->p_next = 0;
50 if (!gp_valgrind_id_list_head) {
51 gp_valgrind_id_list_head = p_valgrind_id_list;
52 gp_valgrind_id_list_tail = p_valgrind_id_list;
54 gp_valgrind_id_list_tail->p_next = p_valgrind_id_list;
55 gp_valgrind_id_list_tail = p_valgrind_id_list;
60 g_num_malloc_failures++;
62 ABTD_spinlock_release(&g_valgrind_id_list_lock);
65 void ABTI_valgrind_unregister_stack(
const void *p_stack)
70 ABTD_spinlock_acquire(&g_valgrind_id_list_lock);
71 if (gp_valgrind_id_list_head->p_stack == p_stack) {
72 VALGRIND_STACK_DEREGISTER(gp_valgrind_id_list_head->valgrind_id);
73 ABTI_valgrind_id_list *p_next = gp_valgrind_id_list_head->p_next;
74 free(gp_valgrind_id_list_head);
75 gp_valgrind_id_list_head = p_next;
77 gp_valgrind_id_list_tail = NULL;
80 ABTI_valgrind_id_list *p_prev = gp_valgrind_id_list_head;
81 ABTI_valgrind_id_list *p_current = gp_valgrind_id_list_head->p_next;
84 if (p_current->p_stack == p_stack) {
85 VALGRIND_STACK_DEREGISTER(p_current->valgrind_id);
86 p_prev->p_next = p_current->p_next;
88 gp_valgrind_id_list_tail = p_prev;
94 p_current = p_current->p_next;
96 if (!deregister_flag) {
99 ABTI_ASSERT(g_num_malloc_failures > 0);
100 g_num_malloc_failures--;
103 ABTD_spinlock_release(&g_valgrind_id_list_lock);