Remove definition of builtin function
[platform/upstream/db4.git] / build_windows / dbstl_common.h
1 #ifndef _DB_STL_COMMON_H
2 #define _DB_STL_COMMON_H
3
4 #ifdef DBSTL_DEBUG_LEAK
5 #include "vld.h"
6 #endif
7
8 #include <assert.h>
9
10 #include "db_cxx.h"
11
12 // In release builds, the native assert will be disabled so we
13 // can't use it in dbstl in cases where we rely on the expression being
14 // evaluated to change the state of the application.
15 //
16 #if !defined(DEBUG) && !defined(_DEBUG)
17 #undef dbstl_assert
18 #define dbstl_assert(expression)
19 #else
20 #undef dbstl_assert
21 #define dbstl_assert(expression) do {                   \
22         if (!(expression)) {                            \
23                 FailedAssertionException ex(__FILE__, __LINE__, #expression);\
24                 throw ex; } } while (0)
25 #endif
26
27 #ifndef SIZE_T_MAX
28 // The max value for size_t variables, one fourth of 2 powers 32.
29 #define SIZE_T_MAX 1073741824
30 #endif
31
32 #if defined( DB_WIN32) || defined(_WIN32)
33 #include <windows.h>
34 #include <tchar.h>
35 #else
36 #define TCHAR char
37 #define _T(e) (e)
38 #define _ftprintf fprintf
39 #define _snprintf snprintf
40 #define _tcschr strchr
41 #define _tcscmp strcmp
42 #define _tcscpy strcpy
43 #define _tcslen strlen
44 #define _tgetopt getopt
45 #define _tmain main
46 #define _tprintf printf
47 #define _ttoi atoi
48 #endif
49
50 // Macro for HAVE_WSTRING (detected by configure)
51 #define HAVE_WSTRING    1
52
53 // Thread local storage modifier declaration.
54 #define TLS_DECL_MODIFIER       __declspec(thread)
55 #define TLS_DEFN_MODIFIER       __declspec(thread)
56
57 #if !defined(TLS_DECL_MODIFIER) && !defined(HAVE_PTHREAD_TLS)
58 #error "No appropriate TLS modifier defined."
59 #endif
60
61 //////////////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////////////
63 //
64 // C++ compiler portability control macro definitions.
65 // If a C++ compiler does not support the following capabilities, disabling
66 // these flags will remove usage of the feature from DB STL.
67 // Where possible a DB STL has implemented work-arounds for the missing
68 // functionality.
69 //
70 #define HAVE_EXPLICIT_KEYWORD                   1
71 #define HAVE_NAMESPACE                          1
72 #define HAVE_TYPENAME                           1
73
74 // Platform specific compiler capability configuration.
75 #ifdef WIN32
76 #define CLS_SCOPE(clstmpl_name)
77 #else
78
79 // C++ standard: It is not possible to define a full specialized version of
80 // a member function of a class template inside the class body. It needs to
81 // be defined outside the class template, and must be defined in the namespace
82 // scope.
83 #define CLS_SCOPE(clstmpl_name) clstmpl_name::
84 #define NO_IN_CLASS_FULL_SPECIALIZATION  1
85 #define NO_MEMBER_FUNCTION_PARTIAL_SPECIALIZATION 1
86 #endif
87
88 #if HAVE_NAMESPACE
89 #define START_NS(nsname) namespace nsname {
90 #define END_NS }
91 #else
92 #define START_NS(nsname) struct nsname {
93 #define END_NS };
94 #endif
95
96 #if HAVE_EXPLICIT_KEYWORD
97 #define EXPLICIT explicit
98 #else
99 #define EXPLICIT
100 #endif
101
102 #if HAVE_TYPENAME
103 #define Typename typename
104 #else
105 #define Typename class
106 #endif
107
108 //////////////////////////////////////////////////////////////////////////
109 // End of compiler portability control macro definitions.
110 ////////////////////////////////////////////////////////////////////////
111
112 //////////////////////////////////////////////////////////////////////////
113 /////////////////////////////////////////////////////////////////////////
114 //
115 // Iterator status macro definitions.
116 //
117 #define INVALID_ITERATOR_POSITION -1 // Iterator goes out of valid range.
118 #define INVALID_ITERATOR_CURSOR -2 // The iterator's dbc cursor is invalid.
119 #define ITERATOR_DUP_ERROR -3 // Failed to duplicate a cursor.
120
121 // Current cursor's key or data dbt has no data.
122 #define INVALID_KEY_DATA -4
123 #define EMPTY_DBT_DATA -5 // Current cursor's pointed data dbt has no data.
124 #define ITERATOR_AT_END -6
125 #define CURSOR_NOT_OPEN -7
126
127 ///////////////////////////////////////////////////////////////////////
128 // End of iterator status macro definitions.
129 //////////////////////////////////////////////////////////////////////
130
131 //////////////////////////////////////////////////////////////////////
132 //////////////////////////////////////////////////////////////////////
133 //
134 // Helper macros definitions.
135 //
136 // Use BDBOP and BDBOP2 to wrap Berkeley DB calls. The macros validate the 
137 // return value. On failure, the wrappers clean up, and generate the
138 // expected exception.
139 //
140 #define BDBOP(bdb_call, ret) do {                                       \
141         if ((ret = (bdb_call)) != 0) throw_bdb_exception(#bdb_call, ret);\
142         } while(0)
143 #define BDBOP2(bdb_call, ret, cleanup) do {                             \
144         if ((ret = (bdb_call)) != 0) { (cleanup);                       \
145                 throw_bdb_exception(#bdb_call, ret);}                   \
146         } while (0)
147 // Do not throw the exception if bdb_call returned a specified error number.
148 #define BDBOP3(bdb_call, ret, exception, cleanup) do {                  \
149         if (((ret = (bdb_call)) != 0) && (ret & exception) == 0) {      \
150                 (cleanup); throw_bdb_exception(#bdb_call, ret);}        \
151         } while (0)
152
153 #define THROW(exception_type, arg_list) do {            \
154         exception_type ex arg_list; throw ex; } while (0)
155
156 #define THROW0(exception_type)  do {                    \
157         exception_type ex; throw ex; } while (0)
158
159 #define INVALID_INDEX ((index_type)-1)
160 #define INVALID_DLEN ((u_int32_t)-1)
161
162 #define DBSTL_MAX_DATA_BUF_LEN 1024 * 4096
163 #define DBSTL_MAX_KEY_BUF_LEN 1024 * 4096
164 #define DBSTL_MAX_MTX_ENV_MUTEX 4096 * 4
165 #define DBSTL_BULK_BUF_SIZE 256 * 1024
166
167 #define COMPARE_CHECK(obj) if (this == &obj) return true;
168 #define ASSIGNMENT_PREDCOND(obj) if (this == &obj) return obj;
169 //////////////////////////////////////////////////////////////////
170 // End of helper macro definitions.
171 //////////////////////////////////////////////////////////////////
172
173 //////////////////////////////////////////////////////////////////
174 //////////////////////////////////////////////////////////////////
175 //
176 // Public global function declarations.
177 // These functions are open/public functionalities of dbstl for
178 // dbstl users to call.
179 //
180 START_NS(dbstl)
181 // _exported is a macro we employ from db_cxx.h of Berkeley DB C++
182 // API. If we want to export the symbols it decorates on Windows,
183 // we must define the macro "DB_CREATE_DLL", as is defined in dbstl
184 // project property.
185 /// \defgroup dbstl_global_functions dbstl global public functions
186 //@{
187
188 /// \name Functions to close database/environments.
189 /// Normally you don't have to close any database 
190 /// or environment handles, they will be closed automatically.
191 /// Though you still have the following API to close them.
192 //@{ 
193 /// Close pdb regardless of reference count. You must make sure pdb
194 /// is not used by others before calling this method.
195 /// You can close the underlying database of a container and assign 
196 /// another database with right configurations to it, if the configuration
197 /// is not suitable for the container, there will be an 
198 /// InvalidArgumentException type of exception thrown.
199 /// You can't use the container after you called close_db and before setting 
200 /// another valid database handle to the container via 
201 /// db_container::set_db_handle() function.
202 /// \param pdb The database handle to close.
203 _exported void close_db(Db *pdb);
204
205 /// Close all open database handles regardless of reference count.
206 /// You can't use any container after you called close_all_dbs and 
207 /// before setting another valid database handle to the 
208 /// container via db_container::set_db_handle() function.
209 /// \sa close_db(Db *);
210 _exported void close_all_dbs();
211
212 /// \brief Close specified database environment handle regardless of reference 
213 /// count. 
214 ///
215 /// Make sure the environment is not used by any other databases.
216 /// \param pdbenv The database environment handle to close.
217 _exported void close_db_env(DbEnv *pdbenv);
218
219 /// \brief Close all open database environment handles regardless of
220 /// reference count.
221 ///
222 /// You can't use the container after you called close_db and before setting
223 /// another valid database handle to the container via 
224 /// db_container::set_db_handle() function. \sa close_db_env(DbEnv *);
225 _exported void close_all_db_envs();
226 //@}
227
228 /// \name Transaction control global functions.
229 /// dbstl transaction API. You should call these API rather than DB C/C++
230 /// API to use Berkeley DB transaction features.
231 //@{ 
232 /// Begin a new transaction from the specified environment "env". 
233 /// This function is called by dbstl user to begin an external transaction.
234 /// The "flags" parameter is passed to DbEnv::txn_begin(). 
235 /// If a transaction created from 
236 /// the same database environment already exists and is unresolved,
237 /// the new transaction is started as a child transaction of that transaction,
238 /// and thus you can't specify the parent transaction.
239 /// \param env The environment to start a transaction from.
240 /// \param flags It is set to DbEnv::txn_begin() function.
241 /// \return The newly created transaction.
242 ///
243 _exported DbTxn* begin_txn(u_int32_t flags, DbEnv *env);
244
245 /// Commit current transaction opened in the environment "env".
246 /// This function is called by user to commit an external explicit transaction.
247 /// \param env The environment whose current transaction is to be committed.
248 /// \param flags It is set to DbTxn::commit() funcion.
249 /// \sa commit_txn(DbEnv *, DbTxn *, u_int32_t);
250 ///
251 _exported void commit_txn(DbEnv *env, u_int32_t flags = 0);
252
253 /// Commit a specified transaction and all its child transactions.
254 /// \param env The environment where txn is started from.
255 /// \param txn The transaction to commit, can be a parent transaction of a 
256 /// nested transaction group, all un-aborted child transactions of 
257 /// it will be committed. 
258 /// \param flags It is passed to each DbTxn::commit() call.
259 /// \sa commit_txn(DbEnv *, u_int32_t);
260 _exported void commit_txn(DbEnv *env, DbTxn *txn, u_int32_t flags = 0);
261
262 /// Abort current transaction of environment "env". This function is called by
263 /// dbstl user to abort an outside explicit transaction.
264 /// \param env The environment whose current transaction is to be aborted.
265 /// \sa abort_txn(DbEnv *, DbTxn *);
266 _exported void abort_txn(DbEnv *env);
267
268 /// Abort specified transaction "txn" and all its child transactions. 
269 /// That is, "txn" can be a parent transaction of a nested transaction group.
270 /// \param env The environment where txn is started from.
271 /// \param txn The transaction to abort, can be a parent transaction of a 
272 /// nested transaction group, all child transactions of it will be aborted.
273 /// \sa abort_txn(DbEnv *);
274 ///
275 _exported void abort_txn(DbEnv *env, DbTxn *txn);
276
277 /// Get current transaction of environment "env".
278 /// \param env The environment whose current transaction we want to get.
279 /// \return Current transaction of env.
280 _exported DbTxn* current_txn(DbEnv *env);
281
282 /// Set environment env's current transaction handle to be newtxn. The original
283 /// transaction handle returned without aborting or commiting. This function
284 /// is used for users to use one transaction among multiple threads.
285 /// \param env The environment whose current transaction to replace.
286 /// \param newtxn The new transaction to be as the current transaction of env.
287 /// \return The old current transaction of env. It is not resolved.
288 _exported DbTxn* set_current_txn_handle(DbEnv *env, DbTxn *newtxn);
289 //@} 
290
291 /// \name Functions to open and register database/environment handles.
292 //@{ 
293 /// Register a Db handle "pdb1". This handle and handles opened in it will be
294 /// closed by ResourceManager, so application code must not try to close or
295 /// delete it. Users can do enough configuration before opening the Db then
296 /// register it via this function.
297 /// All database handles should be registered via this function in each 
298 /// thread using the handle. The only exception is the database handle opened
299 /// by dbstl::open_db should not be registered in the thread of the 
300 /// dbstl::open_db call.
301 /// \param pdb1 The database handle to register into dbstl for current thread.
302 ///
303 _exported void register_db(Db *pdb1);
304
305 /// Register a DbEnv handle env1, this handle and handles opened in it will be
306 /// closed by ResourceManager. Application code must not try to close or delete
307 /// it. Users can do enough config before opening the DbEnv and then register
308 /// it via this function.
309 /// All environment handles should be registered via this function in each 
310 /// thread using the handle. The only exception is the environment handle 
311 /// opened by dbstl::open_db_env should not be registered in the thread of 
312 /// the dbstl::open_db_env call.
313 /// \param env1 The environment to register into dbstl for current thread.
314 ///
315 _exported void register_db_env(DbEnv *env1);
316
317 /// Helper function to open a database and register it into dbstl for the 
318 /// calling thread.
319 /// Users still need to register it in any other thread using it if it
320 /// is shared by multiple threads, via register_db() function.
321 /// Users don't need to delete or free the memory of the returned object, 
322 /// dbstl will take care of that.
323 /// When you don't use dbstl::open_db() but explicitly call DB C++ API to
324 /// open a database, you must new the Db object, rather than create it 
325 /// on stack, and you must delete the Db object by yourself.
326 /// \param penv The environment to open the database from.
327 /// \param cflags The create flags passed to Db class constructor.
328 /// \param filename The database file name, passed to Db::open.
329 /// \param dbname The database name, passed to Db::open.
330 /// \param dbtype The database type, passed to Db::open.
331 /// \param oflags The database open flags, passed to Db::open.
332 /// \param mode The database open mode, passed to Db::open.
333 /// \param txn The transaction to open the database from, passed to Db::open.
334 /// \param set_flags The flags to be set to the created database handle.
335 /// \return The opened database handle.
336 /// \sa register_db(Db *);
337 /// \sa open_db_env;
338 ///
339 _exported Db* open_db (DbEnv *penv, const char *filename, DBTYPE dbtype,
340     u_int32_t oflags, u_int32_t set_flags, int mode = 0644, DbTxn *txn = NULL,
341     u_int32_t cflags = 0, const char* dbname = NULL);
342
343 /// Helper function to open an environment and register it into dbstl for the
344 /// calling thread. Users still need to register it in any other thread if it
345 /// is shared by multiple threads, via register_db_env() function above.
346 /// Users don't need to delete or free the memory of the returned object, 
347 /// dbstl will take care of that.
348 /// 
349 /// When you don't use dbstl::open_env() but explicitly call DB C++ API to
350 /// open an environment, you must new the DbEnv object, rather than create it
351 /// on stack, and you must delete the DbEnv object by yourself.
352 /// \param env_home Environment home directory, it must exist. Passed to 
353 /// DbEnv::open.
354 /// \param cflags DbEnv constructor creation flags, passed to DbEnv::DbEnv.
355 /// \param set_flags Flags to set to the created environment before opening it.
356 /// \param oflags Environment open flags, passed to DbEnv::open.
357 /// \param mode Environment region files mode, passed to DbEnv::open.
358 /// \param cachesize Environment cache size, by default 4M bytes.
359 /// \return The opened database environment handle.
360 /// \sa register_db_env(DbEnv *); 
361 /// \sa open_db;
362 ///
363 _exported DbEnv* open_env(const char *env_home, u_int32_t set_flags,
364     u_int32_t oflags = DB_CREATE | DB_INIT_MPOOL,
365     u_int32_t cachesize = 4 * 1024 * 1024,
366     int mode = 0644,
367     u_int32_t cflags = 0/* Flags for DbEnv constructor. */);
368 //@}
369
370 /// @name Mutex API based on Berkeley DB mutex.
371 /// These functions are in-process mutex support which uses Berkeley DB 
372 /// mutex mechanisms. You can call these functions to do portable 
373 /// synchronization for your code.
374 //@{
375 /// Allocate a Berkeley DB mutex.
376 /// \return Berkeley DB mutex handle.
377 _exported db_mutex_t alloc_mutex();
378 /// Lock a mutex, wait if it is held by another thread.
379 /// \param mtx The mutex handle to lock.
380 /// \return 0 if succeed, non-zero otherwise, call db_strerror to get message.
381 _exported int lock_mutex(db_mutex_t mtx);
382 /// Unlock a mutex, and return immediately.
383 /// \param mtx The mutex handle to unlock.
384 /// \return 0 if succeed, non-zero otherwise, call db_strerror to get message.
385 _exported int unlock_mutex(db_mutex_t mtx);
386 /// Free a mutex, and return immediately.
387 /// \param mtx The mutex handle to free.
388 /// \return 0 if succeed, non-zero otherwise, call db_strerror to get message.
389 _exported void free_mutex(db_mutex_t mtx);
390 //@}
391
392 /// Close cursors opened in dbp1.
393 /// \param dbp1 The database handle whose active cursors to close.
394 /// \return The number of cursors closed by this call.
395 _exported size_t close_db_cursors(Db* dbp1);
396
397 /// \name Other global functions.
398 //@{
399 /// If there are multiple threads within a process that make use of dbstl, then
400 /// this function should be called in a single thread mutual exclusively before
401 /// any use of dbstl in a process; Otherwise, you don't need to call it, but
402 /// are allowed to call it anyway.
403 _exported void dbstl_startup();
404
405 /// This function releases any memory allocated in the heap by code of dbstl.
406 /// So you can only call dbstl_exit() right before the entire process exits.
407 /// It will release any memory allocated by dbstl that have to live during
408 /// the entire process lifetime.
409 _exported void dbstl_exit();
410
411 /// Operators to compare two Dbt objects.
412 /// \param d1 Dbt object to compare.
413 /// \param d2 Dbt object to compare.
414 _exported bool operator==(const Dbt&d1, const Dbt&d2);
415 /// Operators to compare two DBT objects.
416 /// \param d1 DBT object to compare.
417 /// \param d2 DBT object to compare.
418 _exported bool operator==(const DBT&d1, const DBT&d2);
419
420 /// If exisiting random temporary database name generation mechanism is still
421 /// causing name clashes, users can set this global suffix number which will
422 /// be append to each temporary database file name and incremented after each
423 /// append, and by default it is 0.
424 /// \param num Starting number to append to each temporary db file name.
425 _exported void set_global_dbfile_suffix_number(u_int32_t num);
426 //@}
427
428 //@} // dbstl_global_functions
429
430 // Internally used memory allocation functions, they will throw an exception
431 // of NotEnoughMemoryException if can't allocate memory.
432 _exported void * DbstlReAlloc(void *ptr, size_t size);
433 _exported void * DbstlMalloc(size_t size);
434
435 _exported u_int32_t hash_default(Db * /*dbp*/, const void *key, u_int32_t len);
436
437 // Default string manipulation callbacks.
438 _exported u_int32_t dbstl_strlen(const char *str);
439 _exported void dbstl_strcpy(char *dest, const char *src, size_t num);
440 _exported int dbstl_strncmp(const char *s1, const char *s2, size_t num);
441 _exported int dbstl_strcmp(const char *s1, const char *s2);
442 _exported int dbstl_wcscmp(const wchar_t *s1, const wchar_t *s2);
443 _exported int dbstl_wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t num);
444 _exported u_int32_t dbstl_wcslen(const wchar_t *str);
445 _exported void dbstl_wcscpy(wchar_t *dest, const wchar_t *src, size_t num);
446
447 END_NS
448
449 //////////////////////////////////////////////////////////////////
450 // End of public global function declarations.
451 //////////////////////////////////////////////////////////////////
452
453 #endif /* !_DB_STL_COMMON_H */