Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2010 Joel Rosdahl
3 : : *
4 : : * This program is free software; you can redistribute it and/or modify it
5 : : * under the terms of the GNU General Public License as published by the Free
6 : : * Software Foundation; either version 3 of the License, or (at your option)
7 : : * any later version.
8 : : *
9 : : * This program is distributed in the hope that it will be useful, but WITHOUT
10 : : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 : : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 : : * more details.
13 : : *
14 : : * You should have received a copy of the GNU General Public License along with
15 : : * this program; if not, write to the Free Software Foundation, Inc., 51
16 : : * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 : : */
18 : :
19 : : /* A simple array of unsigned integers used for the statistics counters. */
20 : :
21 : : #include "ccache.h"
22 : :
23 : : /*
24 : : * Allocate and initialize a struct counters. Data entries up to the size are
25 : : * set to 0.
26 : : */
27 : : struct counters *
28 : 13 : counters_init(size_t initial_size)
29 : : {
30 : 13 : struct counters *c = x_malloc(sizeof(*c));
31 : 13 : c->data = NULL;
32 : 13 : c->size = 0;
33 : 13 : c->allocated = 0;
34 : 13 : counters_resize(c, initial_size);
35 : 13 : return c;
36 : : }
37 : :
38 : : /*
39 : : * Free a struct counters.
40 : : */
41 : : void
42 : 11 : counters_free(struct counters *c)
43 : : {
44 : 11 : free(c->data);
45 : 11 : free(c);
46 : 11 : }
47 : :
48 : : /*
49 : : * Set a new size. New data entries are set to 0.
50 : : */
51 : : void
52 : 114 : counters_resize(struct counters *c, size_t new_size)
53 : : {
54 [ + + ]: 114 : if (new_size > c->size) {
55 : : size_t i;
56 : 111 : bool realloc = false;
57 : :
58 [ + + ]: 126 : while (c->allocated < new_size) {
59 : 15 : c->allocated += 32 + c->allocated;
60 : 15 : realloc = true;
61 : : }
62 [ + + ]: 111 : if (realloc) {
63 : 14 : c->data = x_realloc(c->data, c->allocated * sizeof(c->data[0]));
64 : : }
65 [ + + ]: 529 : for (i = c->size; i < new_size; i++) {
66 : 418 : c->data[i] = 0;
67 : : }
68 : : }
69 : :
70 : 114 : c->size = new_size;
71 : 114 : }
|