1<!--$Id: struct.so,v 10.8 2002/12/22 20:42:09 bostic Exp $-->
2<!--Copyright (c) 1997,2008 Oracle.  All rights reserved.-->
3<!--See the file LICENSE for redistribution information.-->
4<html>
5<head>
6<title>Berkeley DB Reference Guide: Storing C/C++ structures/objects</title>
7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
9</head>
10<body bgcolor=white>
11<table width="100%"><tr valign=top>
12<td><b><dl><dt>Berkeley DB Reference Guide:<dd>Access Methods</dl></b></td>
13<td align=right><a href="../am_misc/partial.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am_misc/perm.html"><img src="../../images/next.gif" alt="Next"></a>
14</td></tr></table>
15<p align=center><b>Storing C/C++ structures/objects</b></p>
16<p>Berkeley DB can store any kind of data, that is, it is entirely 8-bit clean.
17How you use this depends, to some extent, on the application language
18you are using.  In the C/C++ languages, there are a couple of different
19ways to store structures and objects.</p>
20<p>First, you can do some form of run-length encoding and copy your
21structure into another piece of memory before storing it:</p>
22<blockquote><pre>struct {
23	char *data1;
24	u_int32_t data2;
25	...
26} info;
27size_t len;
28u_int8_t *p, data_buffer[1024];
29<p>
30p = &data_buffer[0];
31len = strlen(info.data1);
32memcpy(p, &len, sizeof(len));
33p += sizeof(len);
34memcpy(p, info.data1, len);
35p += len;
36memcpy(p, &info.data2, sizeof(info.data2));
37p += sizeof(info.data2);
38...</pre></blockquote>
39<p>and so on, until all the fields of the structure have been loaded into
40the byte array.  If you want more examples, see the Berkeley DB logging
41routines (for example, btree/btree_auto.c:__bam_split_log()).  This
42technique is generally known as "marshalling".  If you use this
43technique, you must then un-marshall the data when you read it back:</p>
44<blockquote><pre>struct {
45	char *data1;
46	u_int32_t data2;
47	...
48} info;
49size_t len;
50u_int8_t *p;
51<p>
52p = &data_buffer[0];
53memcpy(&len, p, sizeof(len));
54p += sizeof(len);
55info.data1 = malloc(len);
56memcpy(info.data1, p, len);
57p += len;
58memcpy(&info.data2, p, sizeof(info.data2));
59p += sizeof(info.data2);
60...</pre></blockquote>
61<p>and so on.</p>
62<p>The second way to solve this problem only works if you have just one
63variable length field in the structure.  In that case, you can declare
64the structure as follows:</p>
65<blockquote><pre>struct {
66	int a, b, c;
67	u_int8_t buf[1];
68} info;</pre></blockquote>
69<p>Then, let's say you have a string you want to store in this structure.
70When you allocate the structure, you allocate it as:</p>
71<blockquote><pre>malloc(sizeof(struct info) + strlen(string));</pre></blockquote>
72<p>Since the allocated memory is contiguous, you can the initialize the
73structure as:</p>
74<blockquote><pre>info.a = 1;
75info.b = 2;
76info.c = 3;
77memcpy(&info.buf[0], string, strlen(string) + 1);</pre></blockquote>
78<p>and give it to Berkeley DB to store, with a length of:</p>
79<blockquote><pre>sizeof(struct info) + strlen(string);</pre></blockquote>
80<p>In this case, the structure can be copied out of the database and used
81without any additional work.</p>
82<table width="100%"><tr><td><br></td><td align=right><a href="../am_misc/partial.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../am_misc/perm.html"><img src="../../images/next.gif" alt="Next"></a>
83</td></tr></table>
84<p><font size=1>Copyright (c) 1996,2008 Oracle.  All rights reserved.</font>
85</body>
86</html>
87