xref: /freebsd/contrib/jemalloc/src/witness.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 #define JEMALLOC_WITNESS_C_
2 #include "jemalloc/internal/jemalloc_preamble.h"
3 #include "jemalloc/internal/jemalloc_internal_includes.h"
4 
5 #include "jemalloc/internal/assert.h"
6 #include "jemalloc/internal/malloc_io.h"
7 
8 void
9 witness_init(witness_t *witness, const char *name, witness_rank_t rank,
10     witness_comp_t *comp, void *opaque) {
11 	witness->name = name;
12 	witness->rank = rank;
13 	witness->comp = comp;
14 	witness->opaque = opaque;
15 }
16 
17 static void
18 witness_lock_error_impl(const witness_list_t *witnesses,
19     const witness_t *witness) {
20 	witness_t *w;
21 
22 	malloc_printf("<jemalloc>: Lock rank order reversal:");
23 	ql_foreach(w, witnesses, link) {
24 		malloc_printf(" %s(%u)", w->name, w->rank);
25 	}
26 	malloc_printf(" %s(%u)\n", witness->name, witness->rank);
27 	abort();
28 }
29 witness_lock_error_t *JET_MUTABLE witness_lock_error = witness_lock_error_impl;
30 
31 static void
32 witness_owner_error_impl(const witness_t *witness) {
33 	malloc_printf("<jemalloc>: Should own %s(%u)\n", witness->name,
34 	    witness->rank);
35 	abort();
36 }
37 witness_owner_error_t *JET_MUTABLE witness_owner_error =
38     witness_owner_error_impl;
39 
40 static void
41 witness_not_owner_error_impl(const witness_t *witness) {
42 	malloc_printf("<jemalloc>: Should not own %s(%u)\n", witness->name,
43 	    witness->rank);
44 	abort();
45 }
46 witness_not_owner_error_t *JET_MUTABLE witness_not_owner_error =
47     witness_not_owner_error_impl;
48 
49 static void
50 witness_depth_error_impl(const witness_list_t *witnesses,
51     witness_rank_t rank_inclusive, unsigned depth) {
52 	witness_t *w;
53 
54 	malloc_printf("<jemalloc>: Should own %u lock%s of rank >= %u:", depth,
55 	    (depth != 1) ?  "s" : "", rank_inclusive);
56 	ql_foreach(w, witnesses, link) {
57 		malloc_printf(" %s(%u)", w->name, w->rank);
58 	}
59 	malloc_printf("\n");
60 	abort();
61 }
62 witness_depth_error_t *JET_MUTABLE witness_depth_error =
63     witness_depth_error_impl;
64 
65 void
66 witnesses_cleanup(witness_tsd_t *witness_tsd) {
67 	witness_assert_lockless(witness_tsd_tsdn(witness_tsd));
68 
69 	/* Do nothing. */
70 }
71 
72 void
73 witness_prefork(witness_tsd_t *witness_tsd) {
74 	if (!config_debug) {
75 		return;
76 	}
77 	witness_tsd->forking = true;
78 }
79 
80 void
81 witness_postfork_parent(witness_tsd_t *witness_tsd) {
82 	if (!config_debug) {
83 		return;
84 	}
85 	witness_tsd->forking = false;
86 }
87 
88 void
89 witness_postfork_child(witness_tsd_t *witness_tsd) {
90 	if (!config_debug) {
91 		return;
92 	}
93 #ifndef JEMALLOC_MUTEX_INIT_CB
94 	witness_list_t *witnesses;
95 
96 	witnesses = &witness_tsd->witnesses;
97 	ql_new(witnesses);
98 #endif
99 	witness_tsd->forking = false;
100 }
101