Zero  0.1.0
w_base.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-MT -- Multi-threaded port of the SHORE storage manager
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 /*<std-header orig-src='shore' incl-file-exclusion='W_BASE_H'>
25 
26  $Id: w_base.h,v 1.82 2010/12/08 17:37:37 nhall Exp $
27 
28 SHORE -- Scalable Heterogeneous Object REpository
29 
30 Copyright (c) 1994-99 Computer Sciences Department, University of
31  Wisconsin -- Madison
32 All Rights Reserved.
33 
34 Permission to use, copy, modify and distribute this software and its
35 documentation is hereby granted, provided that both the copyright
36 notice and this permission notice appear in all copies of the
37 software, derivative works or modified versions, and any portions
38 thereof, and that both notices appear in supporting documentation.
39 
40 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
41 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
42 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
43 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
44 
45 This software was developed with support by the Advanced Research
46 Project Agency, ARPA order number 018 (formerly 8230), monitored by
47 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
48 Further funding for this work was provided by DARPA through
49 Rome Research Laboratory Contract No. F30602-97-2-0247.
50 
51 */
52 
53 #ifndef __W_BASE_H
54 #define __W_BASE_H
55 
56 #include "w_defines.h"
57 
58 /* -- do not edit anything above this line -- </std-header>*/
59 
66 /*******************************************************/
67 /* get configuration definitions from config/shore.def */
68 /*
69  * WARNING: if ON and OFF are defined, we must turn them off asap
70  * because ON and OFF are re-definedelsewhere as enums
71  */
72 #ifdef ON
73 #undef ON
74 #endif
75 
76 #ifdef OFF
77 #undef OFF
78 #endif
79 /* end configuration definitions */
80 /*******************************************************/
81 
82 #ifndef __W_WORKAROUND_H
83 
84 #include "w_workaround.h"
85 
86 #endif // __W_WORKAROUND_H
87 
88 #define CAST(t, o) ((t)(o))
89 #define W_UNUSED(x)
90 
91 #if W_DEBUG_LEVEL > 0
92 #define W_IFDEBUG1(x) x
93 #define W_IFNDEBUG1(x)
94 #else
95 #define W_IFDEBUG1(x)
96 #define W_IFNDEBUG1(x) x
97 #endif
98 
99 #if W_DEBUG_LEVEL > 1
100 #define W_IFDEBUG2(x) x
101 #define W_IFNDEBUG2(x)
102 #else
103 #define W_IFDEBUG2(x)
104 #define W_IFNDEBUG2(x) x
105 #endif
106 
107 #if W_DEBUG_LEVEL > 2
108 #define W_IFDEBUG3(x) x
109 #define W_IFNDEBUG3(x)
110 #else
111 #define W_IFDEBUG3(x)
112 #define W_IFNDEBUG3(x) x
113 #endif
114 
115 #if W_DEBUG_LEVEL > 3
116 #define W_IFDEBUG4(x) x
117 #define W_IFNDEBUG4(x)
118 #else
119 #define W_IFDEBUG4(x)
120 #define W_IFNDEBUG4(x) x
121 #endif
122 
123 #if W_DEBUG_LEVEL > 4
124 #define W_IFDEBUG5(x) x
125 #define W_IFNDEBUG5(x)
126 #else
127 #define W_IFDEBUG5(x)
128 #define W_IFNDEBUG5(x) x
129 #endif
130 
131 #define W_IFDEBUG9(x)
132 #define W_IFNDEBUG9(x) x
133 
135 #undef W_IFDEBUG
136 #undef W_IFNDEBUG
137 #if W_DEBUG_LEVEL == 1
138 #define W_IFDEBUG(x) W_IFDEBUG1(x)
139 #define W_IFNDEBUG(x) W_IFNDEBUG1(x)
140 #endif
141 
142 #if W_DEBUG_LEVEL == 2
143 #define W_IFDEBUG(x) W_IFDEBUG2(x)
144 #define W_IFNDEBUG(x) W_IFNDEBUG2(x)
145 #endif
146 
147 #if W_DEBUG_LEVEL == 3
148 #define W_IFDEBUG(x) W_IFDEBUG3(x)
149 #define W_IFNDEBUG(x) W_IFNDEBUG3(x)
150 #endif
151 
152 #if W_DEBUG_LEVEL == 4
153 #define W_IFDEBUG(x) W_IFDEBUG4(x)
154 #define W_IFNDEBUG(x) W_IFNDEBUG4(x)
155 #endif
156 
157 #ifndef W_IFDEBUG
158 #define W_IFDEBUG(x)
159 #endif
160 #ifndef W_IFNDEBUG
161 #define W_IFNDEBUG(x) x
162 #endif
163 
165 
166 #ifdef W_TRACE
167 #define W_IFTRACE(x) x
168 #define W_IFNTRACE(x)
169 #else
170 #define W_IFTRACE(x)
171 #define W_IFNTRACE(x) x
172 #endif
173 
175 #define w_assert0(x) do { \
176  if (!(x)) w_base_t::assert_failed(#x, __FILE__, __LINE__); \
177 } while(0)
178 
179 #define w_assert0_msg(x, msg) \
180 do { \
181  if(!(x)) { \
182  std::stringstream s; \
183  s << #x ; \
184  s << " (detail: " << msg << ")"; \
185  w_base_t::assert_failed(s.str().c_str(), __FILE__, __LINE__); \
186  } \
187 }while(0) \
188 
189 #ifndef W_DEBUG_LEVEL
190 #define W_DEBUG_LEVEL 0
191 #endif
192 
194 #if W_DEBUG_LEVEL >= 1
195 #define w_assert1(x) w_assert0(x)
196 #else
197 //#define w_assert1(x) /**/
198 #define w_assert1(x) if (false) { (void)(x); }
199 #endif
200 
202 #if W_DEBUG_LEVEL >= 2
203 #define w_assert2(x) w_assert1(x)
204 #else
205 //#define w_assert2(x) /**/
206 #define w_assert2(x) if (false) { (void)(x); }
207 #endif
208 
210 #if W_DEBUG_LEVEL >= 3
211 #define w_assert3(x) w_assert1(x)
212 #else
213 //#define w_assert3(x) /**/
214 #define w_assert3(x) if (false) { (void)(x); }
215 #endif
216 
218 #if W_DEBUG_LEVEL >= 4
219 #define w_assert4(x) w_assert1(x)
220 #else
221 //#define w_assert4(x) /**/
222 #define w_assert4(x) if (false) { (void)(x); }
223 #endif
224 
226 #if W_DEBUG_LEVEL >= 5
227 #define w_assert5(x) w_assert1(x)
228 #else
229 //#define w_assert5(x) /**/
230 #define w_assert5(x) if (false) { (void)(x); }
231 #endif
232 
233 /*
234  * The whole idea here is to gradually move assert3's, which have
235  * not been established to be useful in an mt-environment, to anoter
236  * assert level.
237  * First: make them 9. Then gradually move them to level 2->5, based
238  * on the cost and frequency of usefulness.
239  * Make them 2 if you want them for a 'normal' debug system.
240 */
242 //#define w_assert9(x) /**/
243 #define w_assert9(x) if (false) { (void)(x); }
244 
252 #define W_ENUM(x) ((int)(x))
253 
261 #define W_ADDR(x) ((void *)(x))
262 
263 class w_rc_t;
264 
266 class w_base_t {
267 public:
268  /*
269  * shorthands
270  */
271  typedef unsigned char u_char;
272 
273  typedef unsigned short u_short;
274 
275  typedef unsigned long u_long;
276  // typedef w_rc_t rc_t;
277 
278  /*
279  * For statistics that are always 64-bit numbers
280  */
281  typedef uint64_t large_stat_t;
282 
283  /*
284  * For statistics that are 64-bit numbers
285  * only when #defined LARGEFILE_AWARE
286  */
287 // ARCH_LP64 and LARGEFILE_AWARE are determined by configure
288 // and set isn config/shore-config.h
289 #if defined(LARGEFILE_AWARE) || defined(ARCH_LP64)
290 
291  typedef int64_t base_stat_t;
292 
293  typedef double base_float_t;
294 
295 #else
296  typedef int32_t base_stat_t;
297  typedef float base_float_t;
298 #endif
299 
300  typedef float f4_t;
301 
302  typedef double f8_t;
303 
304  static const int8_t int1_max, int1_min;
305 
306  static const int16_t int2_max, int2_min;
307 
308  static const int32_t int4_max, int4_min;
309 
310  static const int64_t int8_max, int8_min;
311 
312  static const uint8_t uint1_max, uint1_min;
313 
314  static const uint16_t uint2_max, uint2_min;
315 
316  static const uint32_t uint4_max, uint4_min;
317 
318  static const uint64_t uint8_max, uint8_min;
319 
320  /*
321  * miscellaneous
322  */
323 
325 #define alignonarg(a) (((ptrdiff_t)(a))-1)
326 #define alignon(p, a) (((ptrdiff_t)((ptrdiff_t)(p) + alignonarg(a))) & ~alignonarg(a))
328 
329  /*
330  * turned into a macro for the purpose of folding
331  * static uint32_t align(uint32_t sz);
332  *
333  * Align to 8-byte boundary.
334  * We now support *only* 8-byte alignment of records
335  */
336 # ifndef ALIGN_BYTE
337 # define ALIGNON 0x8
338 # define ALIGNON1 (ALIGNON-1)
339 # define ALIGN_BYTE(sz) ((size_t)((sz + ALIGNON1) & ~ALIGNON1))
340 # endif /* ALIGN_BYTE */
341 
342  static bool is_aligned(size_t sz);
343 
344  static bool is_aligned(const void* s);
345 
346  static bool is_big_endian();
347 
348  static bool is_little_endian();
349 
361  static int64_t strtoi8(const char*, char** end = 0, int base = 0);
362 
369  static uint64_t strtou8(const char*, char** end = 0, int base = 0);
370 
371  static bool is_finite(const f8_t x);
372 
373  static bool is_infinite(const f8_t x);
374 
375  static bool is_nan(const f8_t x);
376 
377  static bool is_infinite_or_nan(const f8_t x);
378 
379  /*
380  * Endian conversions that don't require any non-shore headers.
381  * These may not be inlined, but that is the portability tradeoff.
382  * w_ prefix due to typical macro problems with the names.
383  * Why not use overloaded args? Great idea, but unintentional
384  * conversions could be a big problem with this stuff.
385  * Used by w_opaque.
386  */
387  static uint16_t w_ntohs(uint16_t);
388 
389  static uint16_t w_htons(uint16_t);
390 
391  static uint32_t w_ntohl(uint32_t);
392 
393  static uint32_t w_htonl(uint32_t);
394 
396  static void assert_failed(
397  const char* desc,
398  const char* file,
399  uint32_t line);
400 
402  static void abort();
403 
407  enum CompareOp {
408  badOp = 0x0,
409  eqOp = 0x1,
410  gtOp = 0x2,
411  geOp = 0x3,
412  ltOp = 0x4,
413  leOp = 0x5,
414  /* for internal use only: */
415  NegInf = 0x100,
421  PosInf = 0x400,
427  };
428 };
429 
430 /*--------------------------------------------------------------*
431  * w_base_t::is_aligned() *
432  *--------------------------------------------------------------*/
433 inline bool
435  return (ALIGN_BYTE(sz) == sz);
436 }
437 
438 inline bool
439 w_base_t::is_aligned(const void* s) {
440  /* XXX works OK if there is a size mismatch because we are looking
441  at the *low* bits */
442  return is_aligned((ptrdiff_t)(s));
443 }
444 
445 /*--------------------------------------------------------------*
446  * w_base_t::is_big_endian() *
447  *--------------------------------------------------------------*/
448 inline bool w_base_t::is_big_endian() {
449 #ifdef WORDS_BIGENDIAN
450  return true;
451 #else
452  return false;
453 #endif
454 }
455 
456 /*--------------------------------------------------------------*
457  * w_base_t::is_little_endian() *
458  *--------------------------------------------------------------*/
459 inline bool
461  return !is_big_endian();
462 }
463 
466 class w_vbase_t : public w_base_t {
467 public:
468  w_vbase_t() {};
469 
470  virtual ~w_vbase_t() {};
471 };
472 
473 #include "w_fill.h"
474 #include "w_error.h"
475 #include "w_rc.h"
476 
477 template<bool B>
479 
484 template<>
485 struct CompileTimeAssertion<true> {
486  void reference() {}
487 };
488 
506 template<typename T>
509  CompileTimeAssertion<sizeof(long) == 8> assert_8byte_long;
510  CompileTimeAssertion<sizeof(long) >= sizeof(T)> assert_long_holds_T;
511  }
512 };
513 
514 #if W_DEBUG_LEVEL > 4
515 #define ASSERT_FITS_IN_LONGLONG(T) { \
516  CompileTimeAssertion<sizeof(int64_t) >= sizeof(T)> assert__##T##__fits_in_longlong; \
517  assert__##T##__fits_in_longlong.reference(); \
518  }
519 #define ASSERT_FITS_IN_POINTER(T) { \
520  CompileTimeAssertion<sizeof(void*) >= sizeof(T)> assert__##T##__fits_in_pointer; \
521  assert__##T##__fits_in_pointer.reference(); \
522  }
523 #else
524 
525 #define ASSERT_FITS_IN_POINTER(T)
526 #define ASSERT_FITS_IN_LONGLONG(T)
527 #endif
528 /*<std-footer incl-file-exclusion='W_BASE_H'> -- do not edit anything below this line -- */
529 
530 #endif // __W_BASE_H /*</std-footer>*/
Class that adds virtual destructor to w_base_t.
Definition: w_base.h:466
Definition: w_base.h:408
static uint32_t w_htonl(uint32_t)
Definition: w_base.cpp:261
static const int8_t int1_min
Definition: w_base.h:304
static void abort()
dump core
Definition: w_base.cpp:218
unsigned short u_short
Definition: w_base.h:273
Definition: w_base.h:417
static bool is_finite(const f8_t x)
Definition: w_base.cpp:183
static const uint32_t uint4_max
Definition: w_base.h:316
Definition: w_base.h:412
static const int32_t int4_min
Definition: w_base.h:308
static bool is_aligned(size_t sz)
Definition: w_base.h:434
static bool is_nan(const f8_t x)
Definition: w_base.cpp:201
CompareOp
Comparison Operators.
Definition: w_base.h:407
Definition: w_base.h:416
Definition: w_base.h:423
float f4_t
Definition: w_base.h:300
w_vbase_t()
Definition: w_base.h:468
Definition: w_base.h:421
Definition: w_base.h:411
Compile-time assertion trick.
Definition: w_base.h:507
static const uint32_t uint4_min
Definition: w_base.h:316
static const int64_t int8_min
Definition: w_base.h:310
int32_t base_stat_t
Definition: w_base.h:296
Definition: w_base.h:266
Definition: w_base.h:418
compile_time_assert()
Definition: w_base.h:508
Definition: w_base.h:415
static bool is_infinite_or_nan(const f8_t x)
Definition: w_base.cpp:212
Definition: w_base.h:424
Definition: w_base.h:425
static const uint64_t uint8_min
Definition: w_base.h:318
virtual ~w_vbase_t()
Definition: w_base.h:470
static const int32_t int4_max
Definition: w_base.h:308
Definition: w_base.h:410
Definition: w_base.h:413
float base_float_t
Definition: w_base.h:297
Return code for most functions and methods.
Definition: w_rc.h:87
static uint32_t w_ntohl(uint32_t)
Definition: w_base.cpp:257
void reference()
Definition: w_base.h:486
Definition: w_base.h:422
static const int8_t int1_max
Definition: w_base.h:304
static const int16_t int2_min
Definition: w_base.h:306
#define ALIGN_BYTE(sz)
Definition: w_base.h:339
static void assert_failed(const char *desc, const char *file, uint32_t line)
print a message and abort
Definition: w_base.cpp:124
unsigned long u_long
Definition: w_base.h:275
static const uint16_t uint2_min
Definition: w_base.h:314
unsigned char u_char
Definition: w_base.h:271
Definition: w_base.h:419
static const uint8_t uint1_min
Definition: w_base.h:312
static bool is_infinite(const f8_t x)
Definition: w_base.cpp:190
Definition: w_base.h:409
static bool is_big_endian()
Definition: w_base.h:448
static int64_t strtoi8(const char *, char **end=0, int base=0)
Convert string to 8-byte integer.
Definition: w_base.cpp:159
static uint16_t w_ntohs(uint16_t)
Definition: w_base.cpp:249
double f8_t
Definition: w_base.h:302
static bool is_little_endian()
Definition: w_base.h:460
static const uint16_t uint2_max
Definition: w_base.h:314
uint64_t large_stat_t
Definition: w_base.h:281
static uint64_t strtou8(const char *, char **end=0, int base=0)
Convert string to 8-byte unsigned integer.
Definition: w_base.cpp:172
static const uint8_t uint1_max
Definition: w_base.h:312
static uint16_t w_htons(uint16_t)
Definition: w_base.cpp:253
static const int16_t int2_max
Definition: w_base.h:306
#define T
Definition: w_okvl_inl.h:45
Definition: w_base.h:426
Definition: w_base.h:420
static const uint64_t uint8_max
Definition: w_base.h:318
static const int64_t int8_max
Definition: w_base.h:310
Definition: w_base.h:478