1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: dbt.c,v 1.2 2008/01/08 20:58:08 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * __dbt_usercopy --
15 *	Take a copy of the user's data, if a callback is supplied.
16 *
17 * PUBLIC: int __dbt_usercopy __P((ENV *, DBT *));
18 */
19int
20__dbt_usercopy(env, dbt)
21	ENV *env;
22	DBT *dbt;
23{
24	void *buf;
25	int ret;
26
27	if (dbt == NULL || !F_ISSET(dbt, DB_DBT_USERCOPY) || dbt->size == 0 ||
28	    dbt->data != NULL)
29		return (0);
30
31	buf = NULL;
32	if ((ret = __os_umalloc(env, dbt->size, &buf)) != 0 ||
33	    (ret = env->dbt_usercopy(dbt, 0, buf, dbt->size,
34	    DB_USERCOPY_GETDATA)) != 0)
35		goto err;
36	dbt->data = buf;
37
38	return (0);
39
40err:	if (buf != NULL) {
41		__os_ufree(env, buf);
42		dbt->data = NULL;
43	}
44
45	return (ret);
46}
47
48/*
49 * __dbt_userfree --
50 *	Free a copy of the user's data, if necessary.
51 *
52 * PUBLIC: void __dbt_userfree __P((ENV *, DBT *, DBT *, DBT *));
53 */
54void
55__dbt_userfree(env, key, pkey, data)
56	ENV *env;
57	DBT *key, *pkey, *data;
58{
59	if (key != NULL &&
60	    F_ISSET(key, DB_DBT_USERCOPY) && key->data != NULL) {
61		__os_ufree(env, key->data);
62		key->data = NULL;
63	}
64	if (pkey != NULL &&
65	    F_ISSET(pkey, DB_DBT_USERCOPY) && pkey->data != NULL) {
66		__os_ufree(env, pkey->data);
67		pkey->data = NULL;
68	}
69	if (data != NULL &&
70	    F_ISSET(data, DB_DBT_USERCOPY) && data->data != NULL) {
71		__os_ufree(env, data->data);
72		data->data = NULL;
73	}
74}
75