1<!--$Id: port.so,v 10.5 2006/08/24 16:32:28 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: Porting Berkeley DB to new architectures</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>Distribution</dl></b></td> 13<td align=right><a href="/test/faq.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/distrib/layout.html"><img src="/images/next.gif" alt="Next"></a> 14</td></tr></table> 15<p align=center><b>Porting Berkeley DB to new architectures</b></p> 16<p>Berkeley DB is generally easy to port to new architectures. Berkeley DB was 17designed to be as portable as possible, and has been ported to a wide 18variety of systems, from Wind River's Tornado system, to VMS, to 19Windows/NT and Windows/95, and most existing UNIX platforms. It runs 20on 16, 32 and 64-bit machines, little or big-endian. The difficulty of 21a port depends on how much of the ANSI C and POSIX 1003.1 standards the 22new architecture offers.</p> 23<p>An abstraction layer separates the main Berkeley DB code from the operating 24system and architecture specific components. This layer is comprised 25of approximately 2500 lines of C language code, found in the <b>os</b> 26subdirectory of the Berkeley DB distribution. The following list of files 27include functionality that may need to be modified or implemented in 28order to support a new architecture. Within each file, there is usually 29one, but sometimes several functions (for example, the 30<b>os_alloc.c</b> file contains the malloc, calloc, realloc, free, 31and strdup functions).</p> 32<table border=1 align=center> 33<tr><th>Source file</th><th>Description</th></tr> 34<tr><td>os_abs.c</td><td>Return if a filename is an absolute pathname</td></tr> 35<tr><td>os_alloc.c</td><td>ANSI C malloc, calloc, realloc, strdup, free front-ends</td></tr> 36<tr><td>os_clock.c</td><td>Return the current time-of-day</td></tr> 37<tr><td>os_config.c</td><td>Return run-time configuration information</td></tr> 38<tr><td>os_dir.c</td><td>Read the filenames from a directory</td></tr> 39<tr><td>os_errno.c</td><td>Set/get the ANSI C errno value</td></tr> 40<tr><td>os_fid.c</td><td>Create a unique ID for a file</td></tr> 41<tr><td>os_fsync.c</td><td>POSIX 1003.1 fsync front-end</td></tr> 42<tr><td>os_handle.c</td><td>Open file handles</td></tr> 43<tr><td>os_id.c</td><td>Return thread ID</td></tr> 44<tr><td>os_map.c</td><td>Map a shared memory area</td></tr> 45<tr><td>os_method.c</td><td>Run-time replacement of system calls</td></tr> 46<tr><td>os_oflags.c</td><td>Convert POSIX 1003.1 open flags, modes to Berkeley DB flags</td></tr> 47<tr><td>os_open.c</td><td>Open file handles</td></tr> 48<tr><td>os_region.c</td><td>Map a shared memory area</td></tr> 49<tr><td>os_rename.c</td><td>POSIX 1003.1 rename call</td></tr> 50<tr><td>os_root.c</td><td>Return if application has special permissions</td></tr> 51<tr><td>os_rpath.c</td><td>Return last pathname separator</td></tr> 52<tr><td>os_rw.c</td><td>POSIX 1003.1 read/write calls</td></tr> 53<tr><td>os_seek.c</td><td>POSIX 1003.1 seek call</td></tr> 54<tr><td>os_sleep.c</td><td>Cause a thread of control to release the CPU</td></tr> 55<tr><td>os_spin.c</td><td>Return the times to spin while waiting for a mutex</td></tr> 56<tr><td>os_stat.c</td><td>POSIX 1003.1 stat call</td></tr> 57<tr><td>os_tmpdir.c</td><td>Set the path for temporary files</td></tr> 58<tr><td>os_unlink.c</td><td>POSIX 1003.1 unlink call</td></tr> 59</table> 60<p>All but a few of these files contain relatively trivial pieces of code. 61Typically, there is only a single version of the code for all platforms 62Berkeley DB supports, and that code lives in the <b>os</b> directory of the 63distribution. Where different code is required, the code is either 64conditionally compiled or an entirely different version is written. For 65example, VxWorks versions of some of these files can be found in the 66distribution directory os_vxworks, and Windows versions can be found in 67os_windows.</p> 68<p>Historically, there are only two difficult questions to answer for each 69new port. The first question is how to handle shared memory. In order 70to write multiprocess database applications (not multithreaded, but 71threads of control running in different address spaces), Berkeley DB must be 72able to name pieces of shared memory and access them from multiple 73processes. On UNIX/POSIX systems, we use <b>mmap</b> and 74<b>shmget</b> for that purpose, but any interface that provides access 75to named shared memory is sufficient. If you have a simple, flat 76address space, you should be able to use the code in 77<b>os_vxworks/os_map.c</b> as a starting point for the port. If you 78are not intending to write multiprocess database applications, then 79this won't be necessary, as Berkeley DB can simply allocate memory from the 80heap if all threads of control will live in a single address space.</p> 81<p>The second question is mutex support. Berkeley DB requires some form of 82<b>self-blocking</b> mutual exclusion mutex. Blocking mutexes are 83preferred as they tend to be less CPU-expensive and less likely to cause 84thrashing. If blocking mutexes are not available, however, test-and-set 85will work as well. The code for mutexes is in two places in the system: 86the include file <b>dbinc/mutex.h</b>, and the distribution directory 87<b>mutex</b>.</p> 88<p>Berkeley DB uses the GNU autoconf tools for configuration on almost all of 89the platforms it supports. Specifically, the include file 90<b>db_config.h</b> configures the Berkeley DB build. The simplest way to 91begin a port is to configure and build Berkeley DB on a UNIX or UNIX-like 92system, and then take the <b>Makefile</b> and <b>db_config.h</b> 93file created by that configuration, and modify it by hand to reflect 94the needs of the new architecture. Unless you're already familiar with 95the GNU autoconf toolset, we don't recommend you take the time to 96integrate your changes back into the Berkeley DB autoconfiguration framework. 97Instead, send us context diffs of your changes and any new source files 98you created, and we'll integrate the changes into our source tree.</p> 99<p>Finally, we're happy to work with you on the port, or potentially, do 100the port ourselves, if that is of interest to you. Regardless, if you 101have any porting questions, just let us know, and we will be happy to 102answer them.</p> 103<table width="100%"><tr><td><br></td><td align=right><a href="/test/faq.html"><img src="/images/prev.gif" alt="Prev"></a><a href="/toc.html"><img src="/images/ref.gif" alt="Ref"></a><a href="/distrib/layout.html"><img src="/images/next.gif" alt="Next"></a> 104</td></tr></table> 105<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 106</body> 107</html> 108