source: to-imperative/trunk/runtime/pxx_common.hh @ 153

Last change on this file since 153 was 153, checked in by orlov, 18 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.3 KB
Line 
1// $Source$
2// $Revision: 153 $
3// $Date: 2002-09-13 22:28:31 +0000 (Fri, 13 Sep 2002) $
4
5#ifndef __pxx_common_hh__
6#define __pxx_common_hh__
7
8#include <unistd.h>
9#include <stdint.h>
10#include <stdio.h>
11
12namespace pxx
13{
14
15#ifndef DEBUG
16#define DEBUG 0
17#endif
18
19#ifndef PARANOIA
20#define PARANOIA 0
21#endif
22
23#define null (0)
24
25#define FATAL \
26  fprintf( \
27    stderr,"Fatal error in %s\nat %s:%d\n", \
28    __PRETTY_FUNCTION__, __FILE__, __LINE__ \
29  ), \
30  pxx::fatal
31
32extern void (*terminate) (int _error_code);
33extern void fatal (char* _fmt, ...);
34
35#define ROUNDS_SIZE (sizeof(size_t) * 8)
36
37extern size_t page_size;
38extern size_t orders [256];
39extern size_t rounds [ROUNDS_SIZE];
40
41static inline
42size_t get_order( size_t _size )
43{
44  /*if( _size > 0 )*/ _size--;
45#if 1
46  size_t b;
47  int n = -8;
48  do {
49    b = _size & 0xFF;
50    _size >>= 8;
51    n += 8;
52  } while( _size );
53  return orders[b] + n;
54#else
55  size_t n = 0;
56  if (_size & 0xFFFF0000) { _size &= 0xFFFF0000; n += 16; }
57  if (_size & 0xFF00FF00) { _size &= 0xFF00FF00; n += 8; }
58  if (_size & 0xF0F0F0F0) { _size &= 0xF0F0F0F0; n += 4; }
59  if (_size & 0xCCCCCCCC) { _size &= 0xCCCCCCCC; n += 2; }
60  if (_size & 0xAAAAAAAA) n += 1;
61  return n + 1;
62#endif
63}
64
65static inline
66bool aligned( void* _ptr, uintptr_t _align )
67{
68  return ((_align - 1) & (uintptr_t)_ptr) == 0;
69}
70
71template <typename typeT>
72inline typeT* ptr_add( typeT* _ptr, size_t _value )
73{
74  return (typeT*)((char*)_ptr + _value);
75}
76
77template <typename typeT>
78inline typeT* ptr_sub( typeT* _ptr, size_t _value )
79{
80  return (typeT*)((char*)_ptr - _value);
81}
82
83template <typename typeT1, typename typeT2>
84inline ssize_t sub_ptr( typeT1* _ptr1, typeT2* _ptr2 )
85{
86  return (char*)_ptr1 - (char*)_ptr2;
87}
88
89#if 0
90static inline
91void* ptr_align( void* _ptr, uintptr_t _align )
92{
93  return (void*)(((uintptr_t)_ptr) & ~(_align - 1));
94}
95
96static inline
97size_t size_align (size_t _size, size_t _align)
98{
99  _align--;
100  return (_size + _align) & (~(_align));
101}
102
103inline void* ptr_and (void* _p1, void* _p2)
104{
105  return (void*)((uintptr_t)_p1 & (uintptr_t)_p2);
106}
107
108inline void* ptr_or (void* _p1, void* _p2)
109{
110  return (void*)((uintptr_t)_p1 | (uintptr_t)_p2);
111}
112
113inline void* ptr_xor (void* _p1, void* _p2)
114{
115  return (void*)((uintptr_t)_p1 ^ (uintptr_t)_p2);
116}
117
118inline void* ptr_diff (void* _p1, void* _p2)
119{
120  uintptr_t diff = (uintptr_t)_p1 ^ (uintptr_t)_p2;
121  return (void*)(((diff ^ (diff - 1)) >> 1) + 1);
122}
123#endif
124
125}
126
127#endif // __pxx_common_hh__
Note: See TracBrowser for help on using the repository browser.