1 /*
2 * L_times.c - implements a circular buffer of timed events with
3 * names.
4 *
5 * Copyright (c) 1997 Phil Maker
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * Id: L_times.c,v 1.1.1.1 1997/11/23 11:45:50 pjm Exp
30 */
31
32 #include <L_times.h>
33
34 #include <stdio.h>
35 #include <nana_error.h>
36
L_times_create(unsigned long n)37 L_TIMES *L_times_create(unsigned long n) {
38 L_TIMES *p = malloc(sizeof(L_TIMES));
39 if(p != 0) {
40 p->ncalls = 0;
41 p->freeslot = 0;
42 p->maxsamples = n;
43 p->times = calloc(n, sizeof(NANA_TIME));
44 p->names = calloc(n, sizeof(char *));
45 p->wraparound = 1;
46 if(p->times == 0 || p->names == 0) {
47 return 0;
48 } else {
49 return p;
50 }
51 } else {
52 return 0;
53 }
54 }
55
L_times_delete(L_TIMES * l)56 void L_times_delete(L_TIMES *l) {
57 if(l != 0) {
58 if(l->times != 0) {
59 free(l->times);
60 }
61 if(l->names != 0) {
62 free(l->names);
63 }
64 free(l);
65 }
66 }
67
L_times_wraparound(L_TIMES * l,int wrap)68 void L_times_wraparound(L_TIMES *l, int wrap) {
69 if(l == 0) {
70 nana_error("L_times_wraparound: arg1 == 0");
71 } else {
72 l->wraparound = wrap;
73 }
74 }
75
L_times_add(L_TIMES * l,char * n,NANA_TIME t)76 void L_times_add(L_TIMES *l, char *n, NANA_TIME t) {
77 if(l->wraparound) { /* only keep latest data */
78 l->times[l->freeslot] = t;
79 l->names[l->freeslot] = n;
80 l->freeslot++;
81 if(l->freeslot == l->maxsamples) {
82 l->freeslot = 0;
83 }
84 l->ncalls++;
85 } else { /* ignore data once full */
86 if(l->freeslot < l->maxsamples) {
87 l->times[l->freeslot] = t;
88 l->names[l->freeslot] = n;
89 l->freeslot++;
90 } /* else full so ignore it */
91 l->ncalls++;
92 }
93 }
94
L_times_clear(L_TIMES * l)95 void L_times_clear(L_TIMES *l) {
96 if(l == 0) {
97 nana_error("L_times_clear: arg1 == 0");
98 } else {
99 l->freeslot = 0;
100 l->ncalls = 0;
101 }
102 }
103
L_times_dump(L_TIMES * l,FILE * f)104 void L_times_dump(L_TIMES *l, FILE *f) {
105 unsigned long i;
106 if(l == 0) {
107 nana_error("L_times_dump: arg1 == 0");
108 }
109
110 fprintf(f, "L_times_dump = %ld calls\n", l->ncalls);
111 if(l->wraparound) { /* wraparound */
112 if(l->ncalls > l->maxsamples) {
113 for(i = l->freeslot; i < l->maxsamples; i++) {
114 fprintf(f, "%f\t%s\n", l->times[i], l->names[i]);
115 }
116 }
117 for(i = 0; i < l->freeslot; i++) {
118 fprintf(f, "%f\t%s\n", l->times[i], l->names[i]);
119 }
120 } else { /* no wraparound so just [0..nsamples-1] */
121 for(i = 0; i < l->freeslot; i++) {
122 fprintf(f, "%f\t%s\n", l->times[i], l->names[i]);
123 }
124 }
125 }
126
127
128
129
130
131