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
8void
9witness_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
17static void
18witness_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}
29witness_lock_error_t *JET_MUTABLE witness_lock_error = witness_lock_error_impl;
30
31static void
32witness_owner_error_impl(const witness_t *witness) {
33	malloc_printf("<jemalloc>: Should own %s(%u)\n", witness->name,
34	    witness->rank);
35	abort();
36}
37witness_owner_error_t *JET_MUTABLE witness_owner_error =
38    witness_owner_error_impl;
39
40static void
41witness_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}
46witness_not_owner_error_t *JET_MUTABLE witness_not_owner_error =
47    witness_not_owner_error_impl;
48
49static void
50witness_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}
62witness_depth_error_t *JET_MUTABLE witness_depth_error =
63    witness_depth_error_impl;
64
65void
66witnesses_cleanup(witness_tsd_t *witness_tsd) {
67	witness_assert_lockless(witness_tsd_tsdn(witness_tsd));
68
69	/* Do nothing. */
70}
71
72void
73witness_prefork(witness_tsd_t *witness_tsd) {
74	if (!config_debug) {
75		return;
76	}
77	witness_tsd->forking = true;
78}
79
80void
81witness_postfork_parent(witness_tsd_t *witness_tsd) {
82	if (!config_debug) {
83		return;
84	}
85	witness_tsd->forking = false;
86}
87
88void
89witness_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