xref: /6.0.3/forestdb/src/common.h (revision 56236603)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2010 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#ifndef _JSAHN_COMMON_H
19#define _JSAHN_COMMON_H
20
21#include <stdlib.h>
22#include <stdint.h>
23
24#undef NDEBUG
25#include <assert.h>
26
27#if !defined(__APPLE__) && !defined(__FreeBSD__)
28#include <malloc.h>
29#endif
30
31#include "option.h"
32#include "arch.h"
33#include "debug.h"
34#include "bitwise_utils.h"
35
36#ifndef _MEMPOOL
37    #define mempool_alloc malloc
38    #define mempool_free free
39#endif
40
41#define _MEMORY_OVERRIDE
42
43#define alca(type, n) ((type*)alloca(sizeof(type) * (n)))
44
45#define seq_memcpy(dest, src, size, offset_var) \
46    memcpy(dest, src, size); \
47    offset_var += size
48
49typedef uint64_t bid_t;
50#define BLK_NOT_FOUND (UINT64_C(0xffffffffffffffff))
51
52typedef uint8_t file_status_t;
53enum{
54    // No compaction now or before or in progress
55    FILE_NORMAL = 0,
56    // fdb_compact has begun - switch to using the new_file for new fdb_set()s
57    FILE_COMPACT_OLD = 1,
58    // this is a new file in the compaction process
59    FILE_COMPACT_NEW = 2,
60    // all open handles on the file has been closed
61    FILE_CLOSED = 3,
62    // compaction completed successfully, and file needs to be removed once all
63    // open handles refering to this file are closed or switched to new_file
64    FILE_REMOVED_PENDING = 4,
65};
66
67#define BLK_MARKER_BNODE (0xff)
68#define BLK_MARKER_DBHEADER (0xee)
69#define BLK_MARKER_DOC (0xdd)
70#define BLK_MARKER_SB (0xcc) // superblock
71#define BLK_MARKER_BAD (0xbd) //corrupted
72#define BLK_MARKER_SIZE (1)
73#define DOCBLK_META_SIZE (16)
74#define BMP_REVNUM_MASK 0xffff
75
76struct docblk_meta {
77    bid_t next_bid;
78    uint16_t sb_bmp_revnum_hash;
79    uint8_t reserved[5];
80    uint8_t marker;
81};
82
83#define randomize() srand((unsigned)time(NULL))
84#define random(num) ((rand())%(num))
85
86#define random_custom(prev, num) (prev) = ((prev)+811)&((num)-1)
87
88
89/* Custom assert */
90
91void fdb_assert_die(const char* expression, const char* file, int line,
92                    uint64_t val, uint64_t expected);
93
94#define fdb_assert(cond, val, expected)   \
95    ((void)((cond) ? (void)0 : fdb_assert_die(#cond, __FILE__, __LINE__,\
96                                              (uint64_t)(val), (uint64_t)(expected))))
97
98#endif // _JSAHN_COMMON_H
99