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
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]
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
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 *);
49 table_grow(int num_entries
)
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:"));
60 table_size
+= num_entries
;
61 temp
= realloc(table_start
, table_size
* sizeof (struct entry
));
63 fail(1, gettext("realloc:"));
68 timecompare(const void *i
, const void *j
)
72 result
= ((entry_t
*)i
)->time
- ((entry_t
*)j
)->time
;
73 if (result
< (longlong_t
) 0)
75 else if (result
== (longlong_t
) 0)
82 * insert an entry into the table. Automatically grows it if needed
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
;
95 table_get_num_elements(void)
103 qsort(table_start
, table_cur
, sizeof (struct entry
), &timecompare
);
107 table_print(void (*print_elem
)(entry_t
*))
111 for (i
= 0; i
< table_cur
; i
++) {
112 print_elem(&(table_start
[i
]));
117 table_get_entry_indexed(int n
)
120 return (&(table_start
[n
]));