resetting manifest requested domain to floor
[platform/upstream/ccache.git] / counters.c
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 counters_init(size_t initial_size)
29 {
30         struct counters *c = x_malloc(sizeof(*c));
31         c->data = NULL;
32         c->size = 0;
33         c->allocated = 0;
34         counters_resize(c, initial_size);
35         return c;
36 }
37
38 /*
39  * Free a struct counters.
40  */
41 void
42 counters_free(struct counters *c)
43 {
44         free(c->data);
45         free(c);
46 }
47
48 /*
49  * Set a new size. New data entries are set to 0.
50  */
51 void
52 counters_resize(struct counters *c, size_t new_size)
53 {
54         if (new_size > c->size) {
55                 size_t i;
56                 bool realloc = false;
57
58                 while (c->allocated < new_size) {
59                         c->allocated += 32 + c->allocated;
60                         realloc = true;
61                 }
62                 if (realloc) {
63                         c->data = x_realloc(c->data, c->allocated * sizeof(c->data[0]));
64                 }
65                 for (i = c->size; i < new_size; i++) {
66                         c->data[i] = 0;
67                 }
68         }
69
70         c->size = new_size;
71 }