Determining the Scope of the Modifications
Prev | Chapter 2. Creating a New Berkeley DB Binary | Next

## Determining the Scope of the Modifications

Once you have a good build of Berkeley DB on a UNIX or UNIX-like system, look over the code to determine what type of code changes you need to make so that you can successfully build Berkeley DB on your target system. This process involves determining:

- Do Changes Need to be Made to the Operating System Functionality?

- Are Some Standard Functions Missing on the Target Platform?

- How Will the Port Handle Shared Memory?

- What Type of Mutexes Will the Port Use?

- Do Any Other Changes Need to be Made?

### Do Changes Need to be Made to the Operating System Functionality?

Berkeley DB uses about forty operating system primitives. The Berkeley DB distribution contains files which are wrappers around these operating system primitives that act as an abstraction layer to separate the main Berkeley DB code from operating system and architecture-specific components. You must port these files (or versions of these files) whenever you port Berkeley DB to a new platform.

Within a Berkeley DB distribution, typically, there is only a single version of these files for all platforms that Berkeley DB supports. Those versions of the files live in the `os` directory of the distribution and follow the ANSI C and POSIX 1003.1 standards. Within each file, there is usually one, but sometimes several functions (for example, the `os_alloc.c` file contains the `malloc`, `realloc`, `strdup`, and `free` functions). The following table describes the files in the os directory of the Berkeley DB distribution.

Source file | Description
--- | ---
os_abort.c | abort()
os_abs.c | Return if a filename is an absolute path name
os_addrinfo.c | getaddrinfo(), freeaddrinfo()
os_alloc.c | malloc(), realloc(), strdup(), free()
os_clock.c | clock_gettime()
os_config.c | Minor run-time configuration information
os_cpu.c | Return the number of CPUs
os_ctime.c | Format a time-stamp
os_dir.c | Return a list of files for a directory
os_errno.c | Library and system error translation
os_fid.c | Return a unique identifier for a file
os_flock.c | Acquire/release a lock on a byte in a file
os_fsync.c | fsync()
os_getenv.c | Retrieve an environment variable
os_handle.c | Return a file handle
os_pid.c | Return a unique identifier for a threa
os_map.c | Shared memory mapping
os_mkdir.c | mkdir()
os_open.c | Return a file handle
os_rename.c | Rename a file
os_root.c | Return if application has special permissions
os_rpath.c | Return last separator in a path
os_rw.c | read(), write()
os_seek.c | lseek()
os_stack.c | Output a stack trace to the message file handle
os_stat.c | stat()
os_tmpdir.c | Return the directory name used by the system for temporary files
os_truncate.c | ftruncate()
os_uid.c | Return unique 32-bit id
os_unlink.c | unlink()
os.yield.c | yield()

When the operating system primitives on the target platform are identical or close to the POSIX semantics that Berkeley DB requires, then no code changes or minimal code changes to the files in the `os` directory are required. If the operating system primitives are quite different, then some code changes may be required to bridge the gap between the requirements of Berkeley DB and what the operating system provides.

Where different code is required, you write an entirely different version of the file and place it in an `os`_xxx directory where xxx represents a platform name. There are `os`_xxx subdirectories in the Berkeley DB distribution for several established non-POSIX platforms. For example, there is a `os_vxworks` directory that contains VxWorks versions of some of the files in the os directory, and Windows versions of some files are in the `os_windows` directory. If your target platform needs a different version of a file, you will need to write that file and place it in a new `os`_xxx directory that you create for your target platform.

### Are Some Standard Functions Missing on the Target Platform?

In some cases, the target platform may not provide the few POSIX functions required by Berkeley DB or the functions provided by the target platform may not operate in a standard compliant way. Berkeley DB provides replacement functions in the `clib` directory of the Berkeley DB distribution.

You need to determine how your target platfrom handles these functions:

- When the target platform does not have a POSIX function required by Berkeley DB, no action is required on your part. When Berekely DB cannot find one of these functions on the target platform, it automatically uses the replacement functions supplied in the `clib` directory of the Berkeley DB distribution. For example, if the target platform does not have the `atoi` or `strtol` functions, Berkeley DB uses `clib/atoi.c` and `clib/strtol.c`.

- When the target platform has a function required by Berekely DB, but that function operates in a non-standard compliant way, you can code to the replacement functions supplied in the `clib` directory.

### How Will the Port Handle Shared Memory?

In order to write multiprocess database applications (not multithreaded, but threads of control running in different address spaces), Berkeley DB must be able to name pieces of shared memory and access them from multiple processes.

On UNIX/POSIX systems, Berkeley DB uses `mmap` and `shmget` for that purpose, but any interface that provides access to named shared memory is sufficient. If you have a simple, flat address space, you should be able to use the code in `os_vxworks/os_map.c` as a starting point for the port.

If you are not intending to write multiprocess database applications, then this won't be necessary, as Berkeley DB can simply allocate memory from the heap if all threads of control will live in a single address space.

### What Type of Mutexes Will the Port Use?

Berkeley DB requires some form of self-blocking mutual exclusion mutex. Blocking mutexes are preferred as they tend to be less CPU-expensive and less likely to cause thrashing. If blocking mutexes are not available, however, test-and-set will work as well. The code for mutexes is in two places in the system: the include file `dbinc/mutex_int.h`, and the distribution directory `mutex`.

### Do Any Other Changes Need to be Made?

In most cases, you do not need to make any changes to the Berkeley DB source code that is not in the abstraction layer (that is, in the `os` directory) as that code is designed to be platform-independent code. However, in some situations, the compiler for the target platform is non-standard and may raise errors when compiling some aspects of the Berkeley DB code (for example, additional casting may be required, or a certain type may cause a problem). In these cases, you will need to modify the generic Berkeley DB code in order to have error-free compilation.

Prev | Up | Next
Chapter 2. Creating a New Berkeley DB Binary | Home | Building on the Target Platform