Fix type warning
[gfxprim.git] / core / GP_Counter.c
bloba7809d35ddf2017e62577d655c890c13b41d4e02
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2011 Tomas Gavenciak <gavento@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <string.h>
24 #include <stdio.h>
25 #include "GP_Common.h"
26 #include "GP_Counter.h"
29 * Internal struct used in counter list
32 struct GP_CounterRecord {
33 char name[GP_COUNTER_NAME_LEN];
34 GP_Counter counter;
38 /*
39 * variables local to module (static)
42 #ifdef GP_IMPLEMENT_COUNTERS
44 static GP_Counter_t GP_counters[GP_COUNTER_MAX];
45 static int GP_used_counters = 0;
46 static struct GP_CounterRecord GP_counter_list[GP_COUNTER_MAX];
47 static GP_Counter_t GP_counter_list_overflow = 0;
49 #endif /* GP_IMPLEMENT_COUNTERS */
51 void GP_PrintCounters(struct FILE *f)
53 #ifdef GP_IMPLEMENT_COUNTERS
54 int i;
55 GP_CHECK(f != NULL);
56 if (GP_used_counters == 0)
57 fprintf((FILE *) f, "[ no counters defined ]\n");
58 for (i = 0; i < GP_used_counters; i++)
59 fprintf((FILE *) f, "%*s : %lld\n", -GP_COUNTER_NAME_LEN,
60 GP_counter_list[i].name, *(GP_counter_list[i].counter));
61 if (GP_counter_list_overflow > 0)
62 fprintf((FILE *) f, "[ unable to allocate new counter %lld times ]\n",
63 GP_counter_list_overflow);
64 #endif /* GP_IMPLEMENT_COUNTERS */
67 GP_Counter GP_GetCounter(const char *name)
69 #ifdef GP_IMPLEMENT_COUNTERS
70 int l = 0;
71 int r = GP_used_counters;
73 GP_CHECK(name != NULL);
74 GP_CHECK(strlen(name) + 1 <= GP_COUNTER_NAME_LEN);
76 /* Bisect GP_counter_list to find either the counter or a place for it
77 * interval [l, r) (not incl. r) */
78 while(r > l) {
79 int med = (r + l) / 2; /* Here never equal to r, might be l */
80 int cmp = strcmp(GP_counter_list[med].name, name);
81 if (cmp == 0)
82 return GP_counter_list[med].counter;
83 if (cmp < 0)
84 l = med + 1;
85 else
86 r = med;
88 GP_CHECK(l == r);
89 if ((l < GP_used_counters) && (strcmp(GP_counter_list[l].name, name) == 0))
90 return GP_counter_list[l].counter;
92 /* Add a new counter */
93 if (GP_used_counters >= GP_COUNTER_MAX) {
94 GP_counter_list_overflow ++;
95 return NULL;
98 /* Move the counter records up and initialize a new one */
99 memmove(GP_counter_list + l + 1, GP_counter_list + l,
100 sizeof(struct GP_CounterRecord) * GP_used_counters - l);
101 strcpy(GP_counter_list[l].name, name);
102 GP_counter_list[l].counter = GP_counters + GP_used_counters;
104 GP_used_counters ++;
105 return GP_counter_list[l].counter;
106 #else /* GP_IMPLEMENT_COUNTERS */
107 return NULL;
108 #endif /* GP_IMPLEMENT_COUNTERS */