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.10 (Sleepycat) 11/28/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 memory to be owned by the application: DB_DBT_MALLOC.
122 * We always allocate memory, even if we're copying out 0 bytes. This
123 * guarantees consistency, i.e., the application can always free memory
124 * without concern as to how many bytes of the record were requested.
127 * Never allocate 0 bytes, it's known to make malloc/realloc unhappy.
129 * Use the memory specified by the application: DB_DBT_USERMEM.
132 * If the length we're going to copy is 0, the application-supplied
133 * memory pointer is allowed to be NULL.
135 if (F_ISSET(dbt
, DB_DBT_MALLOC
)) {
136 dbt
->data
= db_malloc
== NULL
?
137 (void *)__db_malloc(len
) :
138 (void *)db_malloc(len
+ 1);
139 if (dbt
->data
== NULL
)
141 } else if (F_ISSET(dbt
, DB_DBT_USERMEM
)) {
142 if (len
!= 0 && (dbt
->data
== NULL
|| dbt
->ulen
< len
))
144 } else if (memp
== NULL
|| memsize
== NULL
) {
147 if (len
!= 0 && (*memsize
== 0 || *memsize
< len
)) {
148 *memp
= *memp
== NULL
?
149 (void *)__db_malloc(len
) :
150 (void *)__db_realloc(*memp
, len
);
161 memcpy(dbt
->data
, data
, len
);