2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)db_ret.c 10.13 (Sleepycat) 5/7/98";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
30 * PUBLIC: int __db_ret __P((DB *,
31 * PUBLIC: PAGE *, u_int32_t, DBT *, void **, u_int32_t *));
34 __db_ret(dbp
, h
, indx
, dbt
, memp
, memsize
)
51 hk
= P_ENTRY(h
, indx
);
52 if (HPAGE_PTYPE(hk
) == H_OFFPAGE
) {
53 memcpy(&ho
, hk
, sizeof(HOFFPAGE
));
54 return (__db_goff(dbp
, dbt
,
55 ho
.tlen
, ho
.pgno
, memp
, memsize
));
57 len
= LEN_HKEYDATA(h
, dbp
->pgsize
, indx
);
58 data
= HKEYDATA_DATA(hk
);
63 bk
= GET_BKEYDATA(h
, indx
);
64 if (B_TYPE(bk
->type
) == B_OVERFLOW
) {
66 return (__db_goff(dbp
, dbt
,
67 bo
->tlen
, bo
->pgno
, memp
, memsize
));
73 return (__db_pgfmt(dbp
, h
->pgno
));
76 return (__db_retcopy(dbt
, data
, len
, memp
, memsize
,
77 F_ISSET(dbt
, DB_DBT_INTERNAL
) ? NULL
: dbp
->db_malloc
));
82 * Copy the returned data into the user's DBT, handling special flags.
84 * PUBLIC: int __db_retcopy __P((DBT *,
85 * PUBLIC: void *, u_int32_t, void **, u_int32_t *, void *(*)(size_t)));
88 __db_retcopy(dbt
, data
, len
, memp
, memsize
, db_malloc
)
94 void *(*db_malloc
) __P((size_t));
96 /* If returning a partial record, reset the length. */
97 if (F_ISSET(dbt
, DB_DBT_PARTIAL
)) {
98 data
= (u_int8_t
*)data
+ dbt
->doff
;
99 if (len
> dbt
->doff
) {
108 * Return the length of the returned record in the DBT size field.
109 * This satisfies the requirement that if we're using user memory
110 * and insufficient memory was provided, return the amount necessary
116 * Allocate memory to be owned by the application: DB_DBT_MALLOC.
119 * We always allocate memory, even if we're copying out 0 bytes. This
120 * guarantees consistency, i.e., the application can always free memory
121 * without concern as to how many bytes of the record were requested.
124 * Never allocate 0 bytes, it's known to make malloc/realloc unhappy.
126 * Use the memory specified by the application: DB_DBT_USERMEM.
129 * If the length we're going to copy is 0, the application-supplied
130 * memory pointer is allowed to be NULL.
132 if (F_ISSET(dbt
, DB_DBT_MALLOC
)) {
133 dbt
->data
= db_malloc
== NULL
?
134 (void *)__db_malloc(len
) :
135 (void *)db_malloc(len
+ 1);
136 if (dbt
->data
== NULL
)
138 } else if (F_ISSET(dbt
, DB_DBT_USERMEM
)) {
139 if (len
!= 0 && (dbt
->data
== NULL
|| dbt
->ulen
< len
))
141 } else if (memp
== NULL
|| memsize
== NULL
) {
144 if (len
!= 0 && (*memsize
== 0 || *memsize
< len
)) {
145 *memp
= *memp
== NULL
?
146 (void *)__db_malloc(len
) :
147 (void *)__db_realloc(*memp
, len
);
158 memcpy(dbt
->data
, data
, len
);