xref: /titanic_41/usr/src/cmd/tnf/tnfdump/table.c (revision ed5289f91b9bf164dccd6c75398362be77a4478d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdlib.h>
29 #include <locale.h>
30 
31 #include "state.h"
32 
33 /*
34  * This file defines routines on a table data structure. There is one
35  * static table that has the following operations: insert, sort, print,
36  * and get an element by index.
37  */
38 
39 static entry_t	*table_start = NULL;	/* start of table */
40 static int	table_cur = 0;		/* current size of table */
41 static int	table_size = 0;		/* max number of elements */
42 
43 #define	GUESS_NUM_ELEM		(16 * 1024)
44 
45 static void table_grow		(int);
46 static int timecompare		(const void *, const void *);
47 
48 static void
49 table_grow(int num_entries)
50 {
51 	entry_t *temp;
52 
53 	if (table_start == NULL) {
54 		table_size = num_entries;
55 		table_start = malloc(table_size * sizeof (struct entry));
56 		if (table_start == NULL)
57 			fail(1, gettext("malloc:"));
58 		return;
59 	}
60 	table_size += num_entries;
61 	temp = realloc(table_start, table_size * sizeof (struct entry));
62 	if (temp == NULL)
63 		fail(1, gettext("realloc:"));
64 	table_start = temp;
65 }
66 
67 static int
68 timecompare(const void *i, const void *j)
69 {
70 	hrtime_t	result;
71 
72 	result = ((entry_t *)i)->time - ((entry_t *)j)->time;
73 	if (result < (longlong_t) 0)
74 		return (-1);
75 	else if (result == (longlong_t) 0)
76 		return (0);
77 	else
78 		return (1);
79 }
80 
81 /*
82  * insert an entry into the table.  Automatically grows it if needed
83  */
84 void
85 table_insert(entry_t *element)
86 {
87 	if (table_cur >= table_size) {
88 		table_grow(GUESS_NUM_ELEM);
89 	}
90 	/* copy the structure to the array, increment cur index */
91 	table_start[table_cur++] = *element;
92 }
93 
94 int
95 table_get_num_elements(void)
96 {
97 	return (table_size);
98 }
99 
100 void
101 table_sort(void)
102 {
103 	qsort(table_start, table_cur, sizeof (struct entry), &timecompare);
104 }
105 
106 void
107 table_print(void (*print_elem)(entry_t *))
108 {
109 	int i;
110 
111 	for (i = 0; i < table_cur; i++) {
112 		print_elem(&(table_start[i]));
113 	}
114 }
115 
116 entry_t *
117 table_get_entry_indexed(int n)
118 {
119 	if (n < table_cur)
120 		return (&(table_start[n]));
121 	return (NULL);
122 }
123