summaryrefslogtreecommitdiffstats
path: root/src/lib-old-stats/stats.c
blob: 284de26373143b255ed101b1c9d17c33f05b3595 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "stats.h"

struct stats_item {
	struct stats_vfuncs v;
	size_t pos;
};

static ARRAY(struct stats_item *) stats_items = ARRAY_INIT;
static unsigned int stats_total_size = 0;
static bool stats_allocated = FALSE;

struct stats_item *stats_register(const struct stats_vfuncs *vfuncs)
{
	struct stats_item *item;

	if (stats_allocated)
		i_panic("stats_register() called after stats_alloc_size() was already called - this will break existing allocations");

	if (!array_is_created(&stats_items))
		i_array_init(&stats_items, 8);

	item = i_new(struct stats_item, 1);
	item->v = *vfuncs;
	item->pos = stats_total_size;
	array_push_back(&stats_items, &item);

	stats_total_size += vfuncs->alloc_size();
	return item;
}

static bool stats_item_find(struct stats_item *item, unsigned int *idx_r)
{
	struct stats_item *const *itemp;

	array_foreach(&stats_items, itemp) {
		if (*itemp == item) {
			*idx_r = array_foreach_idx(&stats_items, itemp);
			return TRUE;
		}
	}
	return FALSE;
}

static struct stats_item *stats_item_find_by_name(const char *name)
{
	struct stats_item *item;

	array_foreach_elem(&stats_items, item) {
		if (strcmp(item->v.short_name, name) == 0)
			return item;
	}
	return NULL;
}

void stats_unregister(struct stats_item **_item)
{
	struct stats_item *item = *_item;
	unsigned int idx;

	*_item = NULL;

	if (!stats_item_find(item, &idx))
		i_unreached();
	array_delete(&stats_items, idx, 1);

	i_free(item);
	if (array_count(&stats_items) == 0) {
		array_free(&stats_items);
		/* all stats should have been freed by now. allow
		   re-registering and using stats. */
		stats_allocated = FALSE;
	}
}

struct stats *stats_alloc(pool_t pool)
{
	return p_malloc(pool, stats_alloc_size());
}

size_t stats_alloc_size(void)
{
	stats_allocated = TRUE;
	return stats_total_size;
}

void stats_copy(struct stats *dest, const struct stats *src)
{
	memcpy(dest, src, stats_total_size);
}

unsigned int stats_field_count(void)
{
	struct stats_item *item;
	unsigned int count = 0;

	array_foreach_elem(&stats_items, item)
		count += item->v.field_count();
	return count;
}

const char *stats_field_name(unsigned int n)
{
	struct stats_item *item;
	unsigned int i = 0, count;

	array_foreach_elem(&stats_items, item) {
		count = item->v.field_count();
		if (i + count > n)
			return item->v.field_name(n - i);
		i += count;
	}
	i_unreached();
}

void stats_field_value(string_t *str, const struct stats *stats,
		       unsigned int n)
{
	struct stats_item *item;
	unsigned int i = 0, count;

	array_foreach_elem(&stats_items, item) {
		count = item->v.field_count();
		if (i + count > n) {
			const void *item_stats =
				CONST_PTR_OFFSET(stats, item->pos);
			item->v.field_value(str, item_stats, n - i);
			return;
		}
		i += count;
	}
	i_unreached();
}

bool stats_diff(const struct stats *stats1, const struct stats *stats2,
		struct stats *diff_stats_r, const char **error_r)
{
	struct stats_item *item;
	bool ret = TRUE;

	array_foreach_elem(&stats_items, item) {
		if (!item->v.diff(CONST_PTR_OFFSET(stats1, item->pos),
				  CONST_PTR_OFFSET(stats2, item->pos),
				  PTR_OFFSET(diff_stats_r, item->pos),
				  error_r))
			ret = FALSE;
	}
	return ret;
}

void stats_add(struct stats *dest, const struct stats *src)
{
	struct stats_item *item;

	array_foreach_elem(&stats_items, item) {
		item->v.add(PTR_OFFSET(dest, item->pos),
			    CONST_PTR_OFFSET(src, item->pos));
	}
}

bool stats_have_changed(const struct stats *prev, const struct stats *cur)
{
	struct stats_item *item;

	array_foreach_elem(&stats_items, item) {
		if (item->v.have_changed(CONST_PTR_OFFSET(prev, item->pos),
					 CONST_PTR_OFFSET(cur, item->pos)))
			return TRUE;
	}
	return FALSE;
}

void stats_export(buffer_t *buf, const struct stats *stats)
{
	struct stats_item *item;

	array_foreach_elem(&stats_items, item) {
		buffer_append(buf, item->v.short_name,
			      strlen(item->v.short_name)+1);
		item->v.export(buf, CONST_PTR_OFFSET(stats, item->pos));
	}
}

bool stats_import(const unsigned char *data, size_t size,
		  const struct stats *old_stats, struct stats *stats,
		  const char **error_r)
{
	struct stats_item *item;
	const unsigned char *p;
	size_t pos;

	memcpy(stats, old_stats, stats_total_size);
	while (size > 0) {
		const char *next_name = (const void *)data;

		p = memchr(data, '\0', size);
		if (p == NULL) {
			*error_r = "Expected name, but NUL is missing";
			return FALSE;
		}
		item = stats_item_find_by_name(next_name);
		if (item == NULL) {
			*error_r = t_strdup_printf("Unknown stats name: '%s'", next_name);
			return FALSE;
		}
		size -= (p+1) - data;
		data = p+1;
		if (!item->v.import(data, size, &pos,
				    PTR_OFFSET(stats, item->pos), error_r))
			return FALSE;
		i_assert(pos <= size);
		data += pos;
		size -= pos;
	}
	return TRUE;
}

void *stats_fill_ptr(struct stats *stats, struct stats_item *item)
{
	return PTR_OFFSET(stats, item->pos);
}

void stats_reset(struct stats *stats)
{
	memset(stats, 0, stats_total_size);
}