CUBRID Engine  latest
memory_alloc.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
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 
19 
20 /*
21  * memory_alloc.h - Memory allocation module
22  */
23 
24 #ifndef _MEMORY_ALLOC_H_
25 #define _MEMORY_ALLOC_H_
26 
27 #ident "$Id$"
28 
29 #include "config.h"
30 
31 #include "dbtype_def.h"
32 #include "thread_compat.hpp"
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <limits.h>
37 #include <sys/types.h>
38 #include <stddef.h>
39 #include <string.h>
40 #if !defined(WINDOWS)
41 #include <stdint.h>
42 #endif
43 
44 #if defined (__cplusplus)
45 #include <memory>
46 #include <functional>
47 #endif
48 
49 /* Ceiling of positive division */
50 #define CEIL_PTVDIV(dividend, divisor) \
51  (((dividend) == 0) ? 0 : (((dividend) - 1) / (divisor)) + 1)
52 
53 /* Make sure that sizeof returns and integer, so I can use in the operations */
54 #define DB_SIZEOF(val) (sizeof(val))
55 
56 /*
57  * Macros related to alignments
58  */
59 #define CHAR_ALIGNMENT sizeof(char)
60 #define SHORT_ALIGNMENT sizeof(short)
61 #define INT_ALIGNMENT sizeof(int)
62 #define LONG_ALIGNMENT sizeof(long)
63 #define FLOAT_ALIGNMENT sizeof(float)
64 #define DOUBLE_ALIGNMENT sizeof(double)
65 #if __WORDSIZE == 32
66 #define PTR_ALIGNMENT 4
67 #else
68 #define PTR_ALIGNMENT 8
69 #endif
70 #define MAX_ALIGNMENT DOUBLE_ALIGNMENT
71 
72 #if defined(NDEBUG)
73 #define PTR_ALIGN(addr, boundary) \
74  ((char *)((((UINTPTR)(addr) + ((UINTPTR)((boundary)-1)))) \
75  & ~((UINTPTR)((boundary)-1))))
76 #else
77 #define PTR_ALIGN(addr, boundary) \
78  (memset((void*)(addr), 0,\
79  DB_WASTED_ALIGN((UINTPTR)(addr), (UINTPTR)(boundary))),\
80  (char *)((((UINTPTR)(addr) + ((UINTPTR)((boundary)-1)))) \
81  & ~((UINTPTR)((boundary)-1))))
82 #endif
83 
84 #define DB_ALIGN(offset, align) \
85  (((offset) + (align) - 1) & ~((align) - 1))
86 
87 #define DB_ALIGN_BELOW(offset, align) \
88  ((offset) & ~((align) - 1))
89 
90 #define DB_WASTED_ALIGN(offset, align) \
91  (DB_ALIGN((offset), (align)) - (offset))
92 
93 #define DB_ATT_ALIGN(offset) \
94  (((offset) + (INT_ALIGNMENT) - 1) & ~((INT_ALIGNMENT) - 1))
95 
96 /*
97  * Macros related to memory allocation
98  */
99 
100 #define MEM_REGION_INIT_MARK '\0' /* Set this to allocated areas */
101 #define MEM_REGION_SCRAMBLE_MARK '\01' /* Set this to allocated areas */
102 #define MEM_REGION_GUARD_MARK '\02' /* Set this as a memory guard to detect over/under runs */
103 
104 #if defined (CUBRID_DEBUG)
105 extern void db_scramble (void *region, int size);
106 #define MEM_REGION_INIT(region, size) \
107  memset((region), MEM_REGION_SCRAMBLE_MARK, (size))
108 #define MEM_REGION_SCRAMBLE(region, size) \
109  memset (region, MEM_REGION_SCRAMBLE_MARK, size)
110 #else /* CUBRID_DEBUG */
111 #define MEM_REGION_INIT(region, size) \
112  memset((region), MEM_REGION_INIT_MARK, (size))
113 #define MEM_REGION_SCRAMBLE(region, size)
114 #endif /* CUBRID_DEBUG */
115 
116 #if defined(NDEBUG)
117 #define db_private_free_and_init(thrd, ptr) \
118  do { \
119  if ((ptr)) { \
120  db_private_free ((thrd), (ptr)); \
121  (ptr) = NULL; \
122  } \
123  } while (0)
124 
125 #define free_and_init(ptr) \
126  do { \
127  if ((ptr)) { \
128  free ((void*) (ptr)); \
129  (ptr) = NULL; \
130  } \
131  } while (0)
132 
133 #define os_free_and_init(ptr) \
134  do { \
135  if ((ptr)) { \
136  os_free((ptr)); \
137  (ptr) = NULL; \
138  } \
139  } while (0)
140 #else /* NDEBUG */
141 #define db_private_free_and_init(thrd, ptr) \
142  do { \
143  db_private_free ((thrd), (ptr)); \
144  (ptr) = NULL; \
145  } while (0)
146 
147 #define free_and_init(ptr) \
148  do { \
149  free ((void*) (ptr)); \
150  (ptr) = NULL; \
151  } while (0)
152 
153 #define os_free_and_init(ptr) \
154  do { \
155  os_free((ptr)); \
156  (ptr) = NULL; \
157  } while (0)
158 #endif /* NDEBUG */
159 
160 extern int ansisql_strcmp (const char *s, const char *t);
161 extern int ansisql_strcasecmp (const char *s, const char *t);
162 
163 #if !defined (SERVER_MODE)
164 
165 extern HL_HEAPID private_heap_id;
166 
167 #define os_malloc(size) (malloc (size))
168 #define os_free(ptr) (free (ptr))
169 #define os_realloc(ptr, size) (realloc ((ptr), (size)))
170 
171 #else /* SERVER_MODE */
172 
173 #if !defined(NDEBUG)
174 #define os_malloc(size) \
175  os_malloc_debug(size, true, __FILE__, __LINE__)
176 extern void *os_malloc_debug (size_t size, bool rc_track, const char *caller_file, int caller_line);
177 #define os_calloc(n, size) \
178  os_calloc_debug(n, size, true, __FILE__, __LINE__)
179 extern void *os_calloc_debug (size_t n, size_t size, bool rc_track, const char *caller_file, int caller_line);
180 #define os_free(ptr) \
181  os_free_debug(ptr, true, __FILE__, __LINE__)
182 extern void os_free_debug (void *ptr, bool rc_track, const char *caller_file, int caller_line);
183 #define os_realloc(ptr, size) (realloc ((ptr), (size)))
184 #else /* NDEBUG */
185 #define os_malloc(size) \
186  os_malloc_release(size, false)
187 extern void *os_malloc_release (size_t size, bool rc_track);
188 #define os_calloc(n, size) \
189  os_calloc_release(n, size, false)
190 extern void *os_calloc_release (size_t n, size_t size, bool rc_track);
191 #define os_free(ptr) \
192  os_free_release(ptr, false)
193 extern void os_free_release (void *ptr, bool rc_track);
194 #define os_realloc(ptr, size) (realloc ((ptr), (size)))
195 #endif /* NDEBUG */
196 
197 #endif /* SERVER_MODE */
198 
199 /*
200  * Return the assumed minimum alignment requirement for the requested
201  * size. Multiples of sizeof(double) are assumed to need double
202  * alignment, etc.
203  */
204 extern int db_alignment (int);
205 
206 /*
207  * Return the value of "n" to the next "alignment" boundary. "alignment"
208  * must be a power of 2.
209  */
210 extern int db_align_to (int n, int alignment);
211 
212 extern HL_HEAPID db_create_ostk_heap (int chunk_size);
213 extern void db_destroy_ostk_heap (HL_HEAPID heap_id);
214 
215 extern void *db_ostk_alloc (HL_HEAPID heap_id, size_t size);
216 #if defined(ENABLE_UNUSED_FUNCTION)
217 extern void db_ostk_free (HL_HEAPID heap_id, void *ptr);
218 #endif
219 
220 extern HL_HEAPID db_create_private_heap (void);
221 extern void db_clear_private_heap (THREAD_ENTRY * thread_p, HL_HEAPID heap_id);
222 extern HL_HEAPID db_change_private_heap (THREAD_ENTRY * thread_p, HL_HEAPID heap_id);
223 extern HL_HEAPID db_replace_private_heap (THREAD_ENTRY * thread_p);
224 extern void db_destroy_private_heap (THREAD_ENTRY * thread_p, HL_HEAPID heap_id);
225 
226 #if !defined(NDEBUG)
227 #define db_private_alloc(thrd, size) \
228  db_private_alloc_debug(thrd, size, true, __FILE__, __LINE__)
229 #define db_private_free(thrd, ptr) \
230  db_private_free_debug(thrd, ptr, true, __FILE__, __LINE__)
231 #define db_private_realloc(thrd, ptr, size) \
232  db_private_realloc_debug(thrd, ptr, size, true, __FILE__, __LINE__)
233 
234 #ifdef __cplusplus
235 extern "C"
236 {
237 #endif
238  extern void *db_private_alloc_debug (THREAD_ENTRY * thrd, size_t size, bool rc_track, const char *caller_file,
239  int caller_line);
240  extern void db_private_free_debug (THREAD_ENTRY * thrd, void *ptr, bool rc_track, const char *caller_file,
241  int caller_line);
242  extern void *db_private_realloc_debug (THREAD_ENTRY * thrd, void *ptr, size_t size, bool rc_track,
243  const char *caller_file, int caller_line);
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #else /* NDEBUG */
249 #define db_private_alloc(thrd, size) \
250  db_private_alloc_release(thrd, size, false)
251 #define db_private_free(thrd, ptr) \
252  db_private_free_release(thrd, ptr, false)
253 #define db_private_realloc(thrd, ptr, size) \
254  db_private_realloc_release(thrd, ptr, size, false)
255 
256 
257 #ifdef __cplusplus
258 extern "C"
259 {
260 #endif
261  extern void *db_private_alloc_release (THREAD_ENTRY * thrd, size_t size, bool rc_track);
262  extern void db_private_free_release (THREAD_ENTRY * thrd, void *ptr, bool rc_track);
263  extern void *db_private_realloc_release (THREAD_ENTRY * thrd, void *ptr, size_t size, bool rc_track);
264 #ifdef __cplusplus
265 }
266 #endif
267 #endif /* NDEBUG */
268 
269 #ifdef __cplusplus
270 extern "C"
271 {
272 #endif
273  extern char *db_private_strdup (THREAD_ENTRY * thrd, const char *s);
274 #ifdef __cplusplus
275 }
276 #endif
277 
278 /* for external package */
279 extern void *db_private_alloc_external (THREAD_ENTRY * thrd, size_t size);
280 extern void db_private_free_external (THREAD_ENTRY * thrd, void *ptr);
281 extern void *db_private_realloc_external (THREAD_ENTRY * thrd, void *ptr, size_t size);
282 
283 #if defined (SERVER_MODE)
284 extern HL_HEAPID db_private_set_heapid_to_thread (THREAD_ENTRY * thread_p, HL_HEAPID heap_id);
285 #endif // SERVER_MODE
286 
287 extern HL_HEAPID db_create_fixed_heap (int req_size, int recs_per_chunk);
288 extern void db_destroy_fixed_heap (HL_HEAPID heap_id);
289 extern void *db_fixed_alloc (HL_HEAPID heap_id, size_t size);
290 extern void db_fixed_free (HL_HEAPID heap_id, void *ptr);
291 
292 #if defined(SA_MODE)
293 typedef struct private_malloc_header_s PRIVATE_MALLOC_HEADER;
294 struct private_malloc_header_s
295 {
296  unsigned int magic;
297  int alloc_type;
298 };
299 
300 #define PRIVATE_MALLOC_HEADER_MAGIC 0xafdaafdaU
301 
302 enum
303 {
304  PRIVATE_ALLOC_TYPE_LEA = 1,
305  PRIVATE_ALLOC_TYPE_WS = 2
306 };
307 
308 #define PRIVATE_MALLOC_HEADER_ALIGNED_SIZE \
309  ((sizeof(PRIVATE_MALLOC_HEADER) + 7) & ~7)
310 
311 #define private_request_size(s) \
312  (PRIVATE_MALLOC_HEADER_ALIGNED_SIZE + (s))
313 
314 #define private_hl2user_ptr(ptr) \
315  (void *)((char *)(ptr) + PRIVATE_MALLOC_HEADER_ALIGNED_SIZE)
316 
317 #define private_user2hl_ptr(ptr) \
318  (PRIVATE_MALLOC_HEADER *)((char *)(ptr) - PRIVATE_MALLOC_HEADER_ALIGNED_SIZE)
319 #endif /* SA_MODE */
320 
321 #endif /* _MEMORY_ALLOC_H_ */
int db_align_to(int n, int alignment)
Definition: memory_alloc.c:224
void * db_private_realloc_debug(THREAD_ENTRY *thrd, void *ptr, size_t size, bool rc_track, const char *caller_file, int caller_line)
Definition: memory_alloc.c:555
HL_HEAPID db_replace_private_heap(THREAD_ENTRY *thread_p)
Definition: memory_alloc.c:359
void * db_fixed_alloc(HL_HEAPID heap_id, size_t size)
Definition: fixed_alloc.c:92
void * db_private_alloc_external(THREAD_ENTRY *thrd, size_t size)
Definition: memory_alloc.c:804
HL_HEAPID db_create_fixed_heap(int req_size, int recs_per_chunk)
Definition: fixed_alloc.c:64
HL_HEAPID db_change_private_heap(THREAD_ENTRY *thread_p, HL_HEAPID heap_id)
Definition: memory_alloc.c:337
void * db_ostk_alloc(HL_HEAPID heap_id, size_t size)
Definition: memory_alloc.c:262
void db_fixed_free(HL_HEAPID heap_id, void *ptr)
Definition: fixed_alloc.c:109
int ansisql_strcasecmp(const char *s, const char *t)
Definition: memory_alloc.c:134
void THREAD_ENTRY
void db_destroy_fixed_heap(HL_HEAPID heap_id)
Definition: fixed_alloc.c:80
void db_destroy_private_heap(THREAD_ENTRY *thread_p, HL_HEAPID heap_id)
Definition: memory_alloc.c:388
void db_clear_private_heap(THREAD_ENTRY *thread_p, HL_HEAPID heap_id)
Definition: memory_alloc.c:314
HL_HEAPID db_create_ostk_heap(int chunk_size)
Definition: memory_alloc.c:239
HL_HEAPID db_create_private_heap(void)
Definition: memory_alloc.c:294
void db_destroy_ostk_heap(HL_HEAPID heap_id)
Definition: memory_alloc.c:250
int ansisql_strcmp(const char *s, const char *t)
Definition: memory_alloc.c:79
int db_alignment(int)
Definition: memory_alloc.c:192
HL_HEAPID private_heap_id
Definition: memory_alloc.c:55
void db_private_free_debug(THREAD_ENTRY *thrd, void *ptr, bool rc_track, const char *caller_file, int caller_line)
Definition: memory_alloc.c:724
void * db_private_realloc_external(THREAD_ENTRY *thrd, void *ptr, size_t size)
Definition: memory_alloc.c:826
char * db_private_strdup(THREAD_ENTRY *thrd, const char *s)
Definition: memory_alloc.c:675
void db_private_free_external(THREAD_ENTRY *thrd, void *ptr)
Definition: memory_alloc.c:815
void * db_private_alloc_debug(THREAD_ENTRY *thrd, size_t size, bool rc_track, const char *caller_file, int caller_line)
Definition: memory_alloc.c:433