Debian Codegraph @

docs/programmer_reference/am_misc_struct.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <title>Storing C/C++ structures/objects</title>
7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" />
9 <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" />
10 <link rel="up" href="am_misc.html" title="Chapter 4.  Access Method Wrapup" />
11 <link rel="prev" href="am_misc_partial.html" title="Partial record storage and retrieval" />
12 <link rel="next" href="am_misc_perm.html" title="Retrieved key/data permanence for C/C++" />
13 </head>
14 <body>
15 <div xmlns="" class="navheader">
16 <div class="libver">
17 <p>Library Version 11.2.5.3</p>
18 </div>
19 <table width="100%" summary="Navigation header">
20 <tr>
21 <th colspan="3" align="center">Storing C/C++ structures/objects</th>
22 </tr>
23 <tr>
24 <td width="20%" align="left"><a accesskey="p" href="am_misc_partial.html">Prev</a> </td>
25 <th width="60%" align="center">Chapter 4. 
26 Access Method Wrapup
27 </th>
28 <td width="20%" align="right"> <a accesskey="n" href="am_misc_perm.html">Next</a></td>
29 </tr>
30 </table>
31 <hr />
32 </div>
33 <div class="sect1" lang="en" xml:lang="en">
34 <div class="titlepage">
35 <div>
36 <div>
37 <h2 class="title" style="clear: both"><a id="am_misc_struct"></a>Storing C/C++ structures/objects</h2>
38 </div>
39 </div>
40 </div>
41 <p>Berkeley DB can store any kind of data, that is, it is entirely 8-bit clean.
42 How you use this depends, to some extent, on the application language
43 you are using. In the C/C++ languages, there are a couple of different
44 ways to store structures and objects.</p>
45 <p>First, you can do some form of run-length encoding and copy your
46 structure into another piece of memory before storing it:</p>
47 <a id="prog_am23"></a>
48 <pre class="programlisting">struct {
49 char *data1;
50 u_int32_t data2;
51 ...
52 } info;
53 size_t len;
54 u_int8_t *p, data_buffer[1024];
55
56 p = &amp;data_buffer[0];
57 len = strlen(info.data1);
58 memcpy(p, &amp;len, sizeof(len));
59 p += sizeof(len);
60 memcpy(p, info.data1, len);
61 p += len;
62 memcpy(p, &amp;info.data2, sizeof(info.data2));
63 p += sizeof(info.data2);
64 ...
65 </pre>
66 <p>and so on, until all the fields of the structure have been loaded into
67 the byte array. If you want more examples, see the Berkeley DB logging
68 routines (for example, btree/btree_auto.c:__bam_split_log()). This
69 technique is generally known as "marshalling". If you use this
70 technique, you must then un-marshall the data when you read it back:</p>
71 <a id="prog_am24"></a>
72 <pre class="programlisting">struct {
73 char *data1;
74 u_int32_t data2;
75 ...
76 } info;
77 size_t len;
78 u_int8_t *p, data_buffer[1024];
79 ...
80 p = &amp;data_buffer[0];
81 memcpy(&amp;len, p, sizeof(len));
82 p += sizeof(len);
83 info.data1 = malloc(len);
84 memcpy(info.data1, p, len);
85 p += len;
86 memcpy(&amp;info.data2, p, sizeof(info.data2));
87 p += sizeof(info.data2);
88 ...
89 </pre>
90 <p>and so on.</p>
91 <p>The second way to solve this problem only works if you have just one
92 variable length field in the structure. In that case, you can declare
93 the structure as follows:</p>
94 <pre class="programlisting">struct {
95 int a, b, c;
96 u_int8_t buf[1];
97 } info;</pre>
98 <p>Then, let's say you have a string you want to store in this structure.
99 When you allocate the structure, you allocate it as:</p>
100 <pre class="programlisting">malloc(sizeof(struct info) + strlen(string));</pre>
101 <p>Since the allocated memory is contiguous, you can the initialize the
102 structure as:</p>
103 <pre class="programlisting">info.a = 1;
104 info.b = 2;
105 info.c = 3;
106 memcpy(&amp;info.buf[0], string, strlen(string) + 1);</pre>
107 <p>and give it to Berkeley DB to store, with a length of:</p>
108 <pre class="programlisting">sizeof(struct info) + strlen(string);</pre>
109 <p>In this case, the structure can be copied out of the database and used
110 without any additional work.</p>
111 </div>
112 <div class="navfooter">
113 <hr />
114 <table width="100%" summary="Navigation footer">
115 <tr>
116 <td width="40%" align="left"><a accesskey="p" href="am_misc_partial.html">Prev</a> </td>
117 <td width="20%" align="center">
118 <a accesskey="u" href="am_misc.html">Up</a>
119 </td>
120 <td width="40%" align="right"> <a accesskey="n" href="am_misc_perm.html">Next</a></td>
121 </tr>
122 <tr>
123 <td width="40%" align="left" valign="top">Partial record storage and retrieval </td>
124 <td width="20%" align="center">
125 <a accesskey="h" href="index.html">Home</a>
126 </td>
127 <td width="40%" align="right" valign="top"> Retrieved key/data permanence for C/C++</td>
128 </tr>
129 </table>
130 </div>
131 </body>
132 </html>