1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : gb_memory.h // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #ifndef GB_MEMORY_H |
---|
12 | #define GB_MEMORY_H |
---|
13 | |
---|
14 | #if defined(DEBUG) |
---|
15 | #define MEMORY_TEST 1 |
---|
16 | // MEMORY_TEST == 1 uses malloc and normal pointers for internal ARBDB memory |
---|
17 | // -> memory checkers like valgrand work |
---|
18 | // -> loading DB is slower (file must be parsed completely) |
---|
19 | #else |
---|
20 | #define MEMORY_TEST 0 |
---|
21 | // MEMORY_TEST == 0 uses own allocation and relative pointers for internal ARBDB memory |
---|
22 | // -> memory checkers like valgrand won't be very useful |
---|
23 | // -> debugging is difficult, cause it's difficult to follow pointers |
---|
24 | // -> DB loads w/o delay, cause it's mmap'ped into memory |
---|
25 | #endif |
---|
26 | |
---|
27 | #if defined(UNIT_TESTS) // UT_DIFF |
---|
28 | #undef MEMORY_TEST |
---|
29 | #define MEMORY_TEST 0 // test mmapped-DB version in unittests (recommended setting; same as in RELEASE) |
---|
30 | // #define MEMORY_TEST 1 // test DEBUG DB version in unittests |
---|
31 | #endif |
---|
32 | |
---|
33 | struct gb_if_entries; |
---|
34 | struct gb_index_files; |
---|
35 | struct gb_header_list; |
---|
36 | struct GBDATA; |
---|
37 | struct GBCONTAINER; |
---|
38 | |
---|
39 | #if (MEMORY_TEST==1) |
---|
40 | |
---|
41 | typedef void *GB_REL_ADD; |
---|
42 | typedef char *GB_REL_STRING; |
---|
43 | typedef GBDATA *GB_REL_GBDATA; // @@@ use GBENTRY? |
---|
44 | typedef GBCONTAINER *GB_REL_CONTAINER; |
---|
45 | typedef gb_header_list *GB_REL_HLS; |
---|
46 | typedef gb_if_entries *GB_REL_IFES; |
---|
47 | typedef gb_index_files *GB_REL_IFS; |
---|
48 | typedef gb_if_entries **GB_REL_PIFES; |
---|
49 | |
---|
50 | #define UNUSED_IN_MEMTEST(param) param = param |
---|
51 | |
---|
52 | #else |
---|
53 | |
---|
54 | typedef long GB_REL_ADD; |
---|
55 | typedef long GB_REL_STRING; |
---|
56 | typedef long GB_REL_GBDATA; |
---|
57 | typedef long GB_REL_CONTAINER; |
---|
58 | typedef long GB_REL_HLS; |
---|
59 | typedef long GB_REL_IFES; |
---|
60 | typedef long GB_REL_IFS; |
---|
61 | typedef long GB_REL_PIFES; |
---|
62 | |
---|
63 | #endif |
---|
64 | |
---|
65 | // ------------------------------- |
---|
66 | // GB_RESOLVE / GB_SETREL |
---|
67 | // |
---|
68 | // set GB_REL_* "pointers" |
---|
69 | |
---|
70 | #if (MEMORY_TEST==1) |
---|
71 | |
---|
72 | #define GB_RESOLVE(typ, struct_add, member_name) ((typ)((struct_add)->member_name)) |
---|
73 | #define GB_SETREL(struct_add, member_name, address) (struct_add)->member_name = (address) |
---|
74 | |
---|
75 | #else |
---|
76 | |
---|
77 | #define GB_RESOLVE(typ, struct_add, member_name) \ |
---|
78 | ((typ)(((struct_add)->member_name) \ |
---|
79 | ? (typ) (((char*)(struct_add))+((struct_add)->member_name)) \ |
---|
80 | : NULp)) |
---|
81 | |
---|
82 | #define GB_SETREL(struct_add, member_name, address) \ |
---|
83 | do { \ |
---|
84 | char *pntr = (char *)(address); \ |
---|
85 | if (pntr) { \ |
---|
86 | (struct_add)->member_name = (char*)(pntr)-(char*)(struct_add); \ |
---|
87 | } \ |
---|
88 | else { \ |
---|
89 | (struct_add)->member_name = 0; \ |
---|
90 | } \ |
---|
91 | } while (0) |
---|
92 | |
---|
93 | |
---|
94 | #endif |
---|
95 | |
---|
96 | // -------------------------------- |
---|
97 | // ARBDB memory functions |
---|
98 | |
---|
99 | enum ARB_MEMORY_INDEX { |
---|
100 | GBM_CB_INDEX = -1, // Note: historical name. originally was used to allocate database-callback-memory (which is now done with new and delete) |
---|
101 | GBM_HASH_INDEX = -2, |
---|
102 | GBM_HEADER_INDEX = -3, |
---|
103 | GBM_UNDO = -4, |
---|
104 | GBM_DICT_INDEX = -5, |
---|
105 | GBM_USER_INDEX = -6 |
---|
106 | }; |
---|
107 | |
---|
108 | // gbm_get_mem returns a block filled with zero (like calloc() does) |
---|
109 | |
---|
110 | #if defined(DEBUG) |
---|
111 | #if defined(DEVEL_RALF) |
---|
112 | #define FILL_MEM_ON_FREE 0xdb |
---|
113 | #endif |
---|
114 | #endif |
---|
115 | |
---|
116 | #if (MEMORY_TEST==1) |
---|
117 | |
---|
118 | inline void *gbm_get_mem(size_t size, long ) { return ARB_calloc<char>(size); } |
---|
119 | #if defined(FILL_MEM_ON_FREE) |
---|
120 | inline void gbm_free_mem(void *block, size_t size, long ) { memset(block, FILL_MEM_ON_FREE, size); free(block); } |
---|
121 | #else // !defined(FILL_MEM_ON_FREE) |
---|
122 | inline void gbm_free_mem(void *block, size_t , long ) { free(block); } |
---|
123 | #endif |
---|
124 | |
---|
125 | #else // MEMORY_TEST==0 |
---|
126 | |
---|
127 | void *gbmGetMemImpl(size_t size, long index); |
---|
128 | void gbmFreeMemImpl(void *data, size_t size, long index); |
---|
129 | |
---|
130 | inline void *gbm_get_mem(size_t size, long index) { return gbmGetMemImpl(size, index); } |
---|
131 | inline void gbm_free_mem(void *block, size_t size, long index) { |
---|
132 | #if defined(FILL_MEM_ON_FREE) |
---|
133 | memset(block, FILL_MEM_ON_FREE, size); |
---|
134 | #endif |
---|
135 | gbmFreeMemImpl(block, size, index); |
---|
136 | } |
---|
137 | |
---|
138 | #endif // MEMORY_TEST |
---|
139 | |
---|
140 | #else |
---|
141 | #error gb_memory.h included twice |
---|
142 | #endif // GB_MEMORY_H |
---|