1 /*****************************************************************************
2 * This file is part of gfxprim library. *
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. *
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. *
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 *
19 * Copyright (C) 2011 Tomas Gavenciak <gavento@ucw.cz> *
21 *****************************************************************************/
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
];
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
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
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) */
79 int med
= (r
+ l
) / 2; /* Here never equal to r, might be l */
80 int cmp
= strcmp(GP_counter_list
[med
].name
, name
);
82 return GP_counter_list
[med
].counter
;
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
++;
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
;
105 return GP_counter_list
[l
].counter
;
106 #else /* GP_IMPLEMENT_COUNTERS */
108 #endif /* GP_IMPLEMENT_COUNTERS */