2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: bt_overflow.c /main/3 1996/06/11 17:12:45 cde-hal $ */
25 * Copyright (c) 1990, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 #if defined(LIBC_SCCS) && !defined(lint)
61 static char sccsid[] = "@(#)bt_overflow.c 8.1 (Berkeley) 6/4/93";
62 #endif /* LIBC_SCCS and not lint */
64 #include <sys/param.h>
76 * Big key and data entries are stored on linked lists of pages. The initial
77 * reference is byte string stored with the key or data and is the page number
78 * and size. The actual record is stored in a chain of pages linked by the
79 * nextpg field of the PAGE header.
81 * The first page of the chain has a special property. If the record is used
82 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
86 * A single DBT is written to each chain, so a lot of space on the last page
87 * is wasted. This is a fairly major bug for some data sets.
91 * __OVFL_GET -- Get an overflow key/data item.
95 * p: pointer to { pgno_t, size_t }
96 * buf: storage address
100 * RET_ERROR, RET_SUCCESS
103 __ovfl_get(t, p, ssz, buf, bufsz)
114 memmove(&pg, p, sizeof(pgno_t));
115 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(size_t));
119 if (pg == P_INVALID || sz == 0)
122 /* Make the buffer bigger as necessary. */
124 if ((*buf = __fix_realloc(*buf, sz)) == NULL)
130 * Step through the linked list of pages, copying the data on each one
131 * into the buffer. Never copy more than the data's length.
133 plen = t->bt_psize - BTDATAOFF;
134 for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
135 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
139 memmove(p, (char *)h + BTDATAOFF, nb);
140 mpool_put(t->bt_mp, h, 0);
145 return (RET_SUCCESS);
149 * __OVFL_PUT -- Store an overflow key/data item.
154 * pgno: storage page number
157 * RET_ERROR, RET_SUCCESS
160 __ovfl_put(t, dbt, pg)
171 * Allocate pages and copy the key/data record into them. Store the
172 * number of the first page in the chain.
174 plen = t->bt_psize - BTDATAOFF;
175 for (last = NULL, p = dbt->data, sz = dbt->size;;
176 p = (char *)p + plen, last = h) {
177 if ((h = __bt_new(t, &npg)) == NULL)
181 h->nextpg = h->prevpg = P_INVALID;
182 h->flags = P_OVERFLOW;
183 h->lower = h->upper = 0;
186 memmove((char *)h + BTDATAOFF, p, nb);
189 last->nextpg = h->pgno;
190 mpool_put(t->bt_mp, last, MPOOL_DIRTY);
194 if ((sz -= nb) == 0) {
195 mpool_put(t->bt_mp, h, MPOOL_DIRTY);
199 return (RET_SUCCESS);
203 * __OVFL_DELETE -- Delete an overflow chain.
207 * p: pointer to { pgno_t, size_t }
210 * RET_ERROR, RET_SUCCESS
221 memmove(&pg, p, sizeof(pgno_t));
222 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(size_t));
225 if (pg == P_INVALID || sz == 0)
228 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
231 /* Don't delete chains used by internal pages. */
232 if (h->flags & P_PRESERVE) {
233 mpool_put(t->bt_mp, h, 0);
234 return (RET_SUCCESS);
237 /* Step through the chain, calling the free routine for each page. */
238 for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
243 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
246 return (RET_SUCCESS);