]>
git.saurik.com Git - apple/libc.git/blob - db/btree/FreeBSD/bt_utils.c
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)bt_utils.c 8.8 (Berkeley) 7/20/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/db/btree/bt_utils.c,v 1.3 2003/01/01 18:48:42 schweikh Exp $");
43 #include <sys/param.h>
54 * Build return key/data pair.
58 * e: key/data pair to be returned
59 * key: user's key structure (NULL if not to be filled in)
60 * rkey: memory area to hold key
61 * data: user's data structure (NULL if not to be filled in)
62 * rdata: memory area to hold data
63 * copy: always copy the key/data item
66 * RET_SUCCESS, RET_ERROR.
69 __bt_ret(t
, e
, key
, rkey
, data
, rdata
, copy
)
72 DBT
*key
, *rkey
, *data
, *rdata
;
78 bl
= GETBLEAF(e
->page
, e
->index
);
81 * We must copy big keys/data to make them contigous. Otherwise,
82 * leave the page pinned and don't copy unless the user specified
88 if (bl
->flags
& P_BIGKEY
) {
89 if (__ovfl_get(t
, bl
->bytes
,
90 &key
->size
, &rkey
->data
, &rkey
->size
))
92 key
->data
= rkey
->data
;
93 } else if (copy
|| F_ISSET(t
, B_DB_LOCK
)) {
94 if (bl
->ksize
> rkey
->size
) {
95 p
= (void *)(rkey
->data
== NULL
?
96 malloc(bl
->ksize
) : realloc(rkey
->data
, bl
->ksize
));
100 rkey
->size
= bl
->ksize
;
102 memmove(rkey
->data
, bl
->bytes
, bl
->ksize
);
103 key
->size
= bl
->ksize
;
104 key
->data
= rkey
->data
;
106 key
->size
= bl
->ksize
;
107 key
->data
= bl
->bytes
;
112 return (RET_SUCCESS
);
114 if (bl
->flags
& P_BIGDATA
) {
115 if (__ovfl_get(t
, bl
->bytes
+ bl
->ksize
,
116 &data
->size
, &rdata
->data
, &rdata
->size
))
118 data
->data
= rdata
->data
;
119 } else if (copy
|| F_ISSET(t
, B_DB_LOCK
)) {
120 /* Use +1 in case the first record retrieved is 0 length. */
121 if (bl
->dsize
+ 1 > rdata
->size
) {
122 p
= (void *)(rdata
->data
== NULL
?
123 malloc(bl
->dsize
+ 1) :
124 realloc(rdata
->data
, bl
->dsize
+ 1));
128 rdata
->size
= bl
->dsize
+ 1;
130 memmove(rdata
->data
, bl
->bytes
+ bl
->ksize
, bl
->dsize
);
131 data
->size
= bl
->dsize
;
132 data
->data
= rdata
->data
;
134 data
->size
= bl
->dsize
;
135 data
->data
= bl
->bytes
+ bl
->ksize
;
138 return (RET_SUCCESS
);
142 * __BT_CMP -- Compare a key to a given record.
146 * k1: DBT pointer of first arg to comparison
147 * e: pointer to EPG for comparison
150 * < 0 if k1 is < record
151 * = 0 if k1 is = record
152 * > 0 if k1 is > record
167 * The left-most key on internal pages, at any level of the tree, is
168 * guaranteed by the following code to be less than any user key.
169 * This saves us from having to update the leftmost key on an internal
170 * page when the user inserts a new key in the tree smaller than
171 * anything we've yet seen.
174 if (e
->index
== 0 && h
->prevpg
== P_INVALID
&& !(h
->flags
& P_BLEAF
))
178 if (h
->flags
& P_BLEAF
) {
179 bl
= GETBLEAF(h
, e
->index
);
180 if (bl
->flags
& P_BIGKEY
)
187 bi
= GETBINTERNAL(h
, e
->index
);
188 if (bi
->flags
& P_BIGKEY
)
197 if (__ovfl_get(t
, bigkey
,
198 &k2
.size
, &t
->bt_rdata
.data
, &t
->bt_rdata
.size
))
200 k2
.data
= t
->bt_rdata
.data
;
202 return ((*t
->bt_cmp
)(k1
, &k2
));
206 * __BT_DEFCMP -- Default comparison routine.
226 * If a size_t doesn't fit in an int, this routine can lose.
227 * What we need is an integral type which is guaranteed to be
228 * larger than a size_t, and there is no such thing.
230 len
= MIN(a
->size
, b
->size
);
231 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
)
233 return ((int)*p1
- (int)*p2
);
234 return ((int)a
->size
- (int)b
->size
);
238 * __BT_DEFPFX -- Default prefix routine.
245 * Number of bytes needed to distinguish b from a.
255 len
= MIN(a
->size
, b
->size
);
256 for (p1
= a
->data
, p2
= b
->data
; len
--; ++p1
, ++p2
, ++cnt
)
260 /* a->size must be <= b->size, or they wouldn't be in this order. */
261 return (a
->size
< b
->size
? a
->size
+ 1 : a
->size
);