modules: update documentation examples so they compile under ccanlint.
[ccan] / ccan / likely / likely.c
1 #ifdef DEBUG
2 #include <ccan/likely/likely.h>
3 #include <ccan/hash/hash.h>
4 #include <ccan/hashtable/hashtable.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 static struct hashtable *htable;
8
9 struct trace {
10         const char *condstr;
11         const char *file;
12         unsigned int line;
13         bool expect;
14         unsigned long count, right;
15 };
16
17 /* We hash the pointers, which will be identical for same call. */
18 static unsigned long hash_trace(const struct trace *trace)
19 {
20         return hash_pointer(trace->condstr,
21                             hash_pointer(trace->file,
22                                          trace->line + trace->expect));
23 }
24
25 static bool hash_cmp(const void *htelem, void *cmpdata)
26 {
27         const struct trace *t1 = htelem, *t2 = cmpdata;
28         return t1->condstr == t2->condstr
29                 && t1->file == t2->file
30                 && t1->line == t2->line
31                 && t1->expect == t2->expect;
32 }
33
34 static unsigned long rehash(const void *elem, void *priv)
35 {
36         return hash_trace(elem);
37 }
38
39 static void init_trace(struct trace *trace,
40                        const char *condstr, const char *file, unsigned int line,
41                        bool expect)
42 {
43         trace->condstr = condstr;
44         trace->file = file;
45         trace->line = line;
46         trace->expect = expect;
47         trace->count = trace->right = 0;
48 }
49
50 static struct trace *add_trace(const char *condstr,
51                                const char *file, unsigned int line, bool expect)
52 {
53         struct trace *trace = malloc(sizeof(*trace));
54         init_trace(trace, condstr, file, line, expect);
55         hashtable_add(htable, hash_trace(trace), trace);
56         return trace;
57 }
58
59 long _likely_trace(bool cond, bool expect,
60                    const char *condstr,
61                    const char *file, unsigned int line)
62 {
63         struct trace *p, trace;
64
65         if (!htable)
66                 htable = hashtable_new(rehash, NULL);
67
68         init_trace(&trace, condstr, file, line, expect);
69         p = hashtable_find(htable, hash_trace(&trace), hash_cmp, &trace);
70         if (!p)
71                 p = add_trace(condstr, file, line, expect);
72
73         p->count++;
74         if (cond == expect)
75                 p->right++;
76
77         return cond;
78 }
79
80 struct get_stats_info {
81         struct trace *worst;
82         unsigned int min_hits;
83         double worst_ratio;
84 };
85
86 static double right_ratio(const struct trace *t)
87 {
88         return (double)t->right / t->count;
89 }
90
91 static bool get_stats(struct trace *trace, struct get_stats_info *info)
92 {
93         if (trace->count < info->min_hits)
94                 return false;
95
96         if (right_ratio(trace) < info->worst_ratio) {
97                 info->worst = trace;
98                 info->worst_ratio = right_ratio(trace);
99         }
100         return false;
101 }
102
103 const char *likely_stats(unsigned int min_hits, unsigned int percent)
104 {
105         struct get_stats_info info;
106         char *ret;
107
108         if (!htable)
109                 return NULL;
110
111         info.min_hits = min_hits;
112         info.worst = NULL;
113         info.worst_ratio = 2;
114
115         /* This is O(n), but it's not likely called that often. */
116         hashtable_traverse(htable, struct trace, get_stats, &info);
117
118         if (info.worst_ratio * 100 > percent)
119                 return NULL;
120
121         ret = malloc(strlen(info.worst->condstr) +
122                      strlen(info.worst->file) +
123                      sizeof(long int) * 8 +
124                      sizeof("%s:%u:%slikely(%s) correct %u%% (%lu/%lu)"));
125         sprintf(ret, "%s:%u:%slikely(%s) correct %u%% (%lu/%lu)",
126                 info.worst->file, info.worst->line,
127                 info.worst->expect ? "" : "un", info.worst->condstr,
128                 (unsigned)(info.worst_ratio * 100),
129                 info.worst->right, info.worst->count);
130
131         hashtable_del(htable, hash_trace(info.worst), info.worst);
132         free(info.worst);
133
134         return ret;
135 }
136 #endif /*DEBUG*/