2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid[] = "@(#)db_ret.c 10.7 (Sleepycat) 9/15/97";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
33 * PUBLIC: int __db_ret __P((DB *,
34 * PUBLIC: PAGE *, u_int32_t, DBT *, void **, u_int32_t *));
37 __db_ret(dbp, h, indx, dbt, memp, memsize)
54 hk = P_ENTRY(h, indx);
55 if (HPAGE_PTYPE(hk) == H_OFFPAGE) {
56 memcpy(&ho, hk, sizeof(HOFFPAGE));
57 return (__db_goff(dbp, dbt,
58 ho.tlen, ho.pgno, memp, memsize));
60 len = LEN_HKEYDATA(h, dbp->pgsize, indx);
61 data = HKEYDATA_DATA(hk);
66 bk = GET_BKEYDATA(h, indx);
67 if (B_TYPE(bk->type) == B_OVERFLOW) {
69 return (__db_goff(dbp, dbt,
70 bo->tlen, bo->pgno, memp, memsize));
76 return (__db_pgfmt(dbp, h->pgno));
79 return (__db_retcopy(dbt, data, len, memp, memsize,
80 F_ISSET(dbt, DB_DBT_INTERNAL) ? NULL : dbp->db_malloc));
85 * Copy the returned data into the user's DBT, handling special flags.
87 * PUBLIC: int __db_retcopy __P((DBT *,
88 * PUBLIC: void *, u_int32_t, void **, u_int32_t *, void *(*)(size_t)));
91 __db_retcopy(dbt, data, len, memp, memsize, db_malloc)
97 void *(*db_malloc) __P((size_t));
99 /* If returning a partial record, reset the length. */
100 if (F_ISSET(dbt, DB_DBT_PARTIAL)) {
101 data = (u_int8_t *)data + dbt->doff;
102 if (len > dbt->doff) {
111 * Return the length of the returned record in the DBT size field.
112 * This satisfies the requirement that if we're using user memory
113 * and insufficient memory was provided, return the amount necessary
119 * Allocate any necessary memory.
121 * XXX: Never allocate 0 bytes.
123 if (F_ISSET(dbt, DB_DBT_MALLOC)) {
124 dbt->data = db_malloc == NULL ?
125 (void *)malloc(len + 1) :
126 (void *)db_malloc(len + 1);
127 if (dbt->data == NULL)
129 } else if (F_ISSET(dbt, DB_DBT_USERMEM)) {
132 } else if (memp == NULL || memsize == NULL) {
135 if (*memsize == 0 || *memsize < len) {
136 *memp = *memp == NULL ?
137 (void *)malloc(len + 1) :
138 (void *)realloc(*memp, len + 1);
148 memcpy(dbt->data, data, len);