8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / tnf / tnfdump / table.c
blob581b2b7dd3627bcc7f68f57505d2f5c8d704f4f8
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdlib.h>
29 #include <locale.h>
31 #include "state.h"
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.
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 */
43 #define GUESS_NUM_ELEM (16 * 1024)
45 static void table_grow (int);
46 static int timecompare (const void *, const void *);
48 static void
49 table_grow(int num_entries)
51 entry_t *temp;
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;
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;
67 static int
68 timecompare(const void *i, const void *j)
70 hrtime_t result;
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);
82 * insert an entry into the table. Automatically grows it if needed
84 void
85 table_insert(entry_t *element)
87 if (table_cur >= table_size) {
88 table_grow(GUESS_NUM_ELEM);
90 /* copy the structure to the array, increment cur index */
91 table_start[table_cur++] = *element;
94 int
95 table_get_num_elements(void)
97 return (table_size);
100 void
101 table_sort(void)
103 qsort(table_start, table_cur, sizeof (struct entry), &timecompare);
106 void
107 table_print(void (*print_elem)(entry_t *))
109 int i;
111 for (i = 0; i < table_cur; i++) {
112 print_elem(&(table_start[i]));
116 entry_t *
117 table_get_entry_indexed(int n)
119 if (n < table_cur)
120 return (&(table_start[n]));
121 return (NULL);