source: to-imperative/trunk/libp++/pxx_common.hh @ 2032

Last change on this file since 2032 was 2032, checked in by orlov, 15 years ago
  • Removed definition for SIZE_MAX. It's now defined in <limits.h> under Windows.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1//
2// Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>
3//
4// libp++ is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// libp++ is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with libp++; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17//
18// $Source$
19// $Revision: 2032 $
20// $Date: 2006-07-19 17:51:26 +0000 (Wed, 19 Jul 2006) $
21// Author: Andrey Slepuhin <pooh@msu.ru>
22
23#ifndef __pxx_common_hh__
24#define __pxx_common_hh__
25
26//
27// This is needed to allow the use of macros for integer constant
28//  initialization.
29#define __STDC_CONSTANT_MACROS
30#define __STDC_LIMIT_MACROS
31#define __STDC_FORMAT_MACROS
32
33#ifndef WINDOWS
34#include <inttypes.h>
35#include <unistd.h>    // ssize_t in FreeBSD
36#else
37#include <stddef.h>    // for integer types like intptr_t and size_t
38#include <malloc.h>    // for _alloca()
39#endif
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <errno.h>
44
45#ifdef WINDOWS
46#define WSTRPREFIX  L
47#else
48#define WSTRPREFIX  L""
49#endif
50
51#ifdef WINDOWS
52#define snprintf             _snprintf
53#define alloca               _alloca
54#define __PRETTY_FUNCTION__  __FUNCTION__
55#define sleep                ::Sleep
56
57#ifdef  _WIN64
58typedef __int64    ssize_t;
59#else
60typedef _W64 int   ssize_t;
61#endif
62
63#ifdef  _WIN64
64#define __PRIPTR_PREFIX      "l"
65#else
66#define __PRIPTR_PREFIX
67#endif
68
69/* Macros for printing `intptr_t' and `uintptr_t'.  */
70#define PRIdPTR        __PRIPTR_PREFIX "d"
71#define PRIiPTR        __PRIPTR_PREFIX "i"
72#define PRIoPTR        __PRIPTR_PREFIX "o"
73#define PRIuPTR        __PRIPTR_PREFIX "u"
74#define PRIxPTR        __PRIPTR_PREFIX "x"
75#define PRIXPTR        __PRIPTR_PREFIX "X"
76
77typedef          __int8         int8_t;
78typedef          __int16        int16_t;
79typedef          __int32        int32_t;
80typedef          __int64        int64_t;
81
82typedef unsigned __int8         uint8_t;
83typedef unsigned __int16        uint16_t;
84typedef unsigned __int32        uint32_t;
85typedef unsigned __int64        uint64_t;
86
87/* Values to test for integral types holding `void *' pointer.  */
88#if _WIN64
89# define INTPTR_MIN            (-9223372036854775807L-1)
90# define INTPTR_MAX            (9223372036854775807L)
91# define UINTPTR_MAX           (18446744073709551615UL)
92#else
93# define INTPTR_MIN            (-2147483647-1)
94# define INTPTR_MAX            (2147483647)
95# define UINTPTR_MAX           (4294967295U)
96#endif
97
98#if __WIN64
99# define __INT64_C(c)  c ## L
100# define __UINT64_C(c) c ## UL
101#else
102# define __INT64_C(c)  c ## LL
103# define __UINT64_C(c) c ## ULL
104#endif
105
106#define UINT32_MAX             _UI32_MAX
107#define UINT64_MAX             _UI64_MAX
108
109//#define NOGDI
110
111#endif // WINDOWS
112
113
114namespace pxx
115{
116
117#ifndef DEBUG
118#define DEBUG 0
119#endif
120
121#ifndef PARANOIA
122#define PARANOIA 0
123#endif
124
125//
126// Define macros for uintptr_t type.
127#if UINTPTR_MAX == UINT32_MAX
128#define UINTPTR_C(x) UINT32_C(x)
129#elif UINTPTR_MAX == UINT64_MAX
130#define UINTPTR_C(x) UINT64_C(x)
131#else
132#error "Unsupported pointer size"
133#endif
134
135//
136// Useful macros.
137#define null (0)
138#define self (*this)
139#define zref(_type) (*((_type*)(0)))
140
141//
142// A macro to use in a case of fatal errors. Prints a message and source file
143// information (function name, file name and line number).
144#define FATAL \
145  fprintf( \
146    stderr,"Fatal error in %s\nat %s:%d\n", \
147    __PRETTY_FUNCTION__, __FILE__, __LINE__ \
148  ), \
149  pxx::fatal
150
151#define PMETHOD \
152  fprintf(stderr, "%p: %s\n", this, __PRETTY_FUNCTION__)
153
154#define TRY(a) \
155  try { \
156    a; \
157  } catch (pxx::SysError const& err) { \
158    FATAL("System error code = %d", err.get_code()); \
159  }
160
161#define NO_COPY_CTOR(cls) \
162  cls (cls const&) ;
163
164#define NO_ASSIGN(cls) \
165  cls& operator = (cls const&) ;
166
167extern unsigned debug_level ;
168
169//
170// A function to be called when fatal error occured. By default exit is called.
171extern void (*terminate) (int _error_code) ;
172//
173// A function to print fatal error messages.
174extern void fatal (char* _fmt, ...) ;
175//
176// A function to print debug messages.
177extern void dprint (unsigned _level, char* _fmt, ...) ;
178//
179// A memory page size. Should be initialized at runtime due to architectures
180// with a variable page size.
181extern size_t& page_size ;
182//
183// An array containing ceilings of binary logarythms of small integers.
184extern size_t (&orders)[256] ;
185//
186// An array containing exponents of 2.
187extern size_t (&exps_of_two)[sizeof(size_t) * 8] ;
188
189//
190// This function returns a ceiling of a binary logarythm of its argument.
191inline size_t get_order (size_t _size) ;
192//
193// This function checks whether a pointer is properly aligned. The second
194// argument should be an exponent of 2.
195inline bool ptr_is_aligned (void const* _ptr, uintptr_t _align) ;
196//
197// This function aligns a pointer, rounding it to a smaller value.
198inline void* ptr_align (void const* _ptr, uintptr_t _align) ;
199//
200// This function aligns an integer size, rounding it to a higher value.
201inline size_t size_align (size_t _size, size_t _align) ;
202//
203// These functions implement bit operations on pointer values.
204inline void* ptr_and (void const* _p1, void const* _p2) ;
205inline void* ptr_or (void const* _p1, void const* _p2) ;
206inline void* ptr_xor (void const* _p1, void const* _p2) ;
207//
208// This function finds the least significant bit by which two pointers differ
209// and returns an integer where this bit is set.
210inline uintptr_t ptr_diff (void const* _p1, void const* _p2) ;
211//
212// Pointer arithmetic.
213template <typename type_t>
214  inline type_t* ptr_add_offset (type_t* _ptr, ssize_t _value) ;
215template <typename type_t>
216  inline type_t* ptr_sub_offset (type_t* _ptr, ssize_t _value) ;
217template <typename type1_t, typename type2_t>
218  inline ssize_t ptr_sub (type1_t* _ptr1, type2_t* _ptr2) ;
219
220//
221// We provide our own maximum and minimum macros to avoid naming conflicts
222#define pxx_max(a,b) ((a) > (b) ? (a) : (b))
223#define pxx_min(a,b) ((a) < (b) ? (a) : (b))
224
225///
226/// This enumeration type used as argument of constructors of some data types
227/// may indicate that constructor argument is static constant data that
228/// shouldn't be copied.
229typedef enum
230{
231  const_data,
232  non_const_data
233} const_t ;
234
235typedef enum
236{
237  mode_read = 1,
238  mode_write = 2,
239  mode_append = mode_read | 4,
240  mode_read_write = mode_read | mode_write
241} io_mode_t ;
242
243//class Init ;
244//extern Init* __pinit ;
245//static Init* __static_pinit = __pinit ;
246
247}
248
249#endif // __pxx_common_hh__
Note: See TracBrowser for help on using the repository browser.