CUBRID Engine  latest
api_util.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  * api_util.h -
21  */
22 
23 #ifndef _API_UTIL_H_
24 #define _API_UTIL_H_
25 
26 #include <pthread.h>
27 #include <stdio.h>
28 
29 /* doubly linked list header */
30 typedef struct dlisth_s dlisth;
31 struct dlisth_s
32 {
35 };
36 
37 /* dlisth_map() hook function type definition */
38 typedef int (*dlist_map_func) (dlisth * h, void *arg, int *cont);
39 
40 extern int dlisth_map (dlisth * h, dlist_map_func func, void *arg);
41 
42 #define dlisth_init(h) \
43 do { \
44  dlisth *__h = (h); \
45  (__h)->next = (__h)->prev = (__h); \
46 } while (0)
47 
48 #define dlisth_is_empty(h) ((h)->next == (h) && (h)->prev == (h))
49 
50 #define dlisth_delete(h_) \
51 do { \
52  dlisth *__h = (h_); \
53  (__h)->next->prev = (__h)->prev; \
54  (__h)->prev->next = (__h)->next; \
55  (__h)->next = (__h)->prev = (__h); \
56 } while(0)
57 
58 #define dlisth_insert_before(ih, bh) \
59 do { \
60  dlisth *__ih = (ih); \
61  dlisth *__bh = (bh); \
62  (__ih)->next = (__bh); \
63  (__ih)->prev = (__bh)->prev; \
64  (__bh)->prev->next = (__ih); \
65  (__bh)->prev = (__ih); \
66 } while (0)
67 
68 #define dlisth_insert_after(ih, bh) \
69 do { \
70  dlisth *__ih = (ih); \
71  dlisth *__bh = (bh); \
72  (__ih)->prev = (__bh); \
73  (__ih)->next = (__bh)->next; \
74  (__bh)->next->prev = (__ih); \
75  (__bh)->next = (__ih); \
76 } while (0)
77 
78 
79 /* generic static hash table */
80 typedef struct hash_table_s hash_table;
81 /* key compare function */
82 typedef int (*ht_comparef) (void *key1, void *key2, int *r);
83 /* key hash function */
84 typedef int (*ht_hashf) (void *key, unsigned int *rv);
85 /* key function : get pointer to the key from element */
86 typedef int (*ht_keyf) (void *elem, void **rk);
87 /* element destroy function */
88 typedef void (*ht_destroyf) (void *elem);
89 
91 extern void hash_destroy (hash_table * ht, ht_destroyf dtor);
92 extern int hash_lookup (hash_table * ht, void *key, void **elem);
93 extern int hash_insert (hash_table * ht, void *elem);
94 extern int hash_delete (hash_table * ht, void *key, void **elem);
95 
96 /* misc. */
97 #define API_MUTEX pthread_mutex_t
98 #define API_MUTEX_INIT(m) pthread_mutex_init(m,NULL)
99 #define API_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
100 #define API_LOCK(m) pthread_mutex_lock(m)
101 #define API_UNLOCK(m) pthread_mutex_unlock(m)
102 #define API_TRYLOCK(m) pthread_mutex_trylock(m)
103 #define API_MUTEX_DESTROY(m) pthread_mutex_destroy(m)
104 #define API_ONCE_TYPE pthread_once_t
105 #define API_ONCE_INIT PTHREAD_ONCE_INIT
106 #define API_ONCE_FUNC(t,r) pthread_once(t,r)
107 
108 /* malloc/calloc/free hook functions for memory check */
109 #if 1
110 #define API_CALLOC(n,s) api_calloc((n),(s),__FILE__,__LINE__)
111 #define API_MALLOC(s) api_malloc((s),__FILE__,__LINE__)
112 #define API_FREE(p) api_free((p), __FILE__, __LINE__)
113 extern void *api_calloc (size_t nmemb, size_t size, const char *file, int line);
114 extern void *api_malloc (size_t size, const char *file, int line);
115 extern void api_free (void *ptr, const char *file, int line);
116 extern int api_check_memory (FILE * fp);
117 
118 /* api_malloc_dhook_flag_set is global flag used to enable malloc debug */
119 extern int api_malloc_dhook_flag_set;
120 #else
121 #define API_CALLOC(n,s) calloc(n,s)
122 #define API_MALLOC(s) malloc(s)
123 #define API_FREE(p) free(p)
124 #endif
125 
126 #endif /* _API_UTIL_H_ */
dlisth * prev
Definition: api_util.h:34
dlisth * next
Definition: api_util.h:33
void hash_destroy(hash_table *ht, ht_destroyf dtor)
Definition: api_util.c:198
ht_hashf hashf
Definition: api_util.c:52
int(* ht_hashf)(void *key, unsigned int *rv)
Definition: api_util.h:84
ht_keyf keyf
Definition: api_util.c:53
int hash_lookup(hash_table *ht, void *key, void **elem)
Definition: api_util.c:233
int(* ht_comparef)(void *key1, void *key2, int *r)
Definition: api_util.h:82
int hash_new(int bucket_sz, ht_hashf hashf, ht_keyf keyf, ht_comparef comparef, hash_table **ht)
Definition: api_util.c:166
int api_check_memory(FILE *fp)
Definition: api_util.c:458
int dlisth_map(dlisth *h, dlist_map_func func, void *arg)
Definition: api_util.c:138
static int rv
Definition: area_alloc.c:52
int hash_insert(hash_table *ht, void *elem)
Definition: api_util.c:275
int(* ht_keyf)(void *elem, void **rk)
Definition: api_util.h:86
void * api_calloc(size_t nmemb, size_t size, const char *file, int line)
Definition: api_util.c:366
int(* dlist_map_func)(dlisth *h, void *arg, int *cont)
Definition: api_util.h:38
void api_free(void *ptr, const char *file, int line)
Definition: api_util.c:431
int bucket_sz
Definition: api_util.c:51
int api_malloc_dhook_flag_set
Definition: api_util.c:79
int hash_delete(hash_table *ht, void *key, void **elem)
Definition: api_util.c:318
void(* ht_destroyf)(void *elem)
Definition: api_util.h:88
void * api_malloc(size_t size, const char *file, int line)
Definition: api_util.c:399
ht_comparef comparef
Definition: api_util.c:54