1<!--$Id: db_set_alloc.so,v 10.3 2002/08/18 21:15:51 bostic Exp $--> 2<!--$Id: env_set_alloc.so,v 1.24 2008/01/19 14:12:57 bostic Exp $--> 3<!--Copyright (c) 1997,2008 Oracle. All rights reserved.--> 4<!--See the file LICENSE for redistribution information.--> 5<html> 6<head> 7<title>Berkeley DB: Db::set_alloc</title> 8<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> 9<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> 10</head> 11<body bgcolor=white> 12<table width="100%"><tr valign=top> 13<td> 14<b>Db::set_alloc</b> 15</td> 16<td align=right> 17<a href="/api_cxx/api_core.html"><img src="/images/api.gif" alt="API"></a> 18<a href="/ref/toc.html"><img src="/images/ref.gif" alt="Ref"></a></td> 19</tr></table> 20<hr size=1 noshade> 21<tt> 22<b><pre> 23#include <db_cxx.h> 24<p> 25extern "C" { 26 typedef void *(*db_malloc_fcn_type)(size_t); 27 typedef void *(*db_realloc_fcn_type)(void *, size_t); 28 typedef void *(*db_free_fcn_type)(void *); 29}; 30<p> 31int 32Db::set_alloc(db_malloc_fcn_type app_malloc, 33 db_realloc_fcn_type app_realloc, 34 db_free_fcn_type app_free); 35</pre></b> 36<hr size=1 noshade> 37<b>Description: Db::set_alloc</b> 38<p>Set the allocation functions used by the <a href="/api_cxx/env_class.html">DbEnv</a> and <a href="/api_cxx/db_class.html">Db</a> 39methods to allocate or free memory owned by the application.</p> 40<p>There are a number of interfaces in Berkeley DB where memory is allocated by 41the library and then given to the application. For example, the 42<a href="/api_cxx/dbt_class.html#DB_DBT_MALLOC">DB_DBT_MALLOC</a> flag, when specified in the <a href="/api_cxx/dbt_class.html">Dbt</a> object, 43will cause the <a href="/api_cxx/db_class.html">Db</a> methods to allocate and reallocate memory 44which then becomes the responsibility of the calling application. (See 45<a href="/api_cxx/dbt_class.html">Dbt</a> for more information.) Other examples are the Berkeley DB 46interfaces which return statistical information to the application: 47<a href="/api_cxx/db_stat.html">Db::stat</a>, <a href="/api_cxx/lock_stat.html">DbEnv::lock_stat</a>, <a href="/api_cxx/log_archive.html">DbEnv::log_archive</a>, 48<a href="/api_cxx/log_stat.html">DbEnv::log_stat</a>, <a href="/api_cxx/memp_stat.html">DbEnv::memp_stat</a>, and <a href="/api_cxx/txn_stat.html">DbEnv::txn_stat</a>. There is one 49method in Berkeley DB where memory is allocated by the application and then 50given to the library: <a href="/api_cxx/db_associate.html">Db::associate</a>.</p> 51<p>On systems in which there may be multiple library versions of the 52standard allocation routines (notably Windows NT), transferring memory 53between the library and the application will fail because the Berkeley DB 54library allocates memory from a different heap than the application uses 55to free it. To avoid this problem, the <a href="/api_cxx/env_set_alloc.html">DbEnv::set_alloc</a> and 56Db::set_alloc methods can be used to pass Berkeley DB references to the 57application's allocation routines.</p> 58<p>It is not an error to specify only one or two of the possible allocation 59function parameters to these interfaces; however, in that case the 60specified interfaces must be compatible with the standard library 61interfaces, as they will be used together. The functions specified must 62match the calling conventions of the ANSI C X3.159-1989 (ANSI C) library routines of 63the same name.</p> 64<p>Because databases opened within Berkeley DB environments use the allocation 65interfaces specified to the environment, it is an error to attempt to 66set those interfaces in a database created within an environment.</p> 67<p>The Db::set_alloc method may not be called after the <a href="/api_cxx/db_open.html">Db::open</a> method is called. 68</p> 69<p>The Db::set_alloc method 70either returns a non-zero error value 71or throws an exception that encapsulates a non-zero error value on 72failure, and returns 0 on success. 73</p> 74<br><b>Errors</b> 75<p>The Db::set_alloc method 76may fail and throw 77<a href="/api_cxx/except_class.html">DbException</a>, 78encapsulating one of the following non-zero errors, or return one of 79the following non-zero errors:</p> 80<br> 81<b>EINVAL</b><ul compact><li>If called in a database environment, or called after <a href="/api_cxx/db_open.html">Db::open</a> was 82called; or if an 83invalid flag value or parameter was specified.</ul> 84<br> 85<hr size=1 noshade> 86<br><b>Class</b> 87<a href="/api_cxx/db_class.html">Db</a> 88<br><b>See Also</b> 89<a href="/api_cxx/db_list.html">Databases and Related Methods</a> 90</tt> 91<table width="100%"><tr><td><br></td><td align=right> 92<a href="/api_cxx/api_core.html"><img src="/images/api.gif" alt="API"></a><a href="/ref/toc.html"><img src="/images/ref.gif" alt="Ref"></a> 93</td></tr></table> 94<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 95</body> 96</html> 97