xref: /illumos-gate/usr/src/cmd/nscd/nscd_biggest.c (revision 2a8bcb4e)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 
30 #include "cache.h"
31 
32 nsc_keephot_t *
maken(int n)33 maken(int n)
34 {
35 	nsc_keephot_t	*ret;
36 
37 	++n;
38 	ret = (nsc_keephot_t *)calloc(n, sizeof (nsc_keephot_t));
39 	if (ret == NULL)
40 		return (NULL);
41 	ret[0].num = n - 1;
42 	return (ret);
43 }
44 
45 void *
insertn(nsc_keephot_t * table,uint_t n,void * data)46 insertn(nsc_keephot_t *table, uint_t n, void *data)
47 {
48 	void	*olddata;
49 	int	size, guess, base, last;
50 
51 	if (n < 1 || table[1].num > n) {
52 		return (data);
53 	}
54 
55 	size = table[0].num;
56 	if (table[size].num < n)  /* biggest so far */
57 		guess = size;
58 	else {
59 		base = 1;
60 		last = size;
61 		while (last >= base) {
62 			guess = (last+base)/2;
63 			if (table[guess].num == n)
64 				goto doit;
65 			if (table[guess].num > n)
66 				last = guess - 1;
67 			else
68 				base = guess + 1;
69 		}
70 		guess = last;
71 	}
72 
73 doit:
74 	olddata = table[1].ptr;
75 	(void) memmove(table + 1, table + 2,
76 			sizeof (nsc_keephot_t) * (guess-1));
77 	table[guess].ptr = data;
78 	table[guess].num = n;
79 	return (olddata);
80 }
81