Changeset 254
- Timestamp:
- Nov 28, 2002, 4:51:54 PM (18 years ago)
- Location:
- to-imperative/trunk/libp++
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
to-imperative/trunk/libp++/pxx_common.cc
r115 r254 1 1 // 2 // Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>2 // Copyright (C) 2000, 2001, 2002 Andrey Slepuhin <pooh@msu.ru> 3 3 // 4 4 // libp++ is free software; you can redistribute it and/or modify … … 21 21 // Author: Andrey Slepuhin <pooh@msu.ru> 22 22 23 #include "pxx_common. hh"23 #include "pxx_common.ih" 24 24 25 #include < cstdarg>25 #include <stdarg.h> 26 26 #include <unistd.h> 27 27 28 28 namespace pxx 29 29 { 30 31 unsigned debug_level = 0 ; 30 32 31 33 void (*terminate) (int _error_code) = null ; … … 42 44 } 43 45 46 void dprint (unsigned _level, char* _fmt, ...) { 47 if ((debug_level & _level) != 0) { 48 va_list ap; 49 va_start(ap, _fmt); 50 vfprintf(stderr, _fmt, ap); 51 va_end(ap); 52 } 53 } 54 44 55 size_t page_size ; 45 56 size_t orders[256] ; 46 size_t rounds[sizeof(size_t) * 8] ;57 size_t exps_of_two[sizeof(size_t) * 8] ; 47 58 59 // 60 // Internal class used to initialize necessary data 48 61 class Init 49 62 { … … 59 72 unsigned s = i; 60 73 orders[i] = 0; 61 // if (s != 0) s--;62 74 while (s) { 63 75 s >>= 1; … … 66 78 orders[i]--; 67 79 } 68 for (i = 0; i < sizeof( rounds) / sizeof(rounds[0]); i++) {69 rounds[i] = 1 << i;80 for (i = 0; i < sizeof(exps_of_two) / sizeof(exps_of_two[0]); i++) { 81 exps_of_two[i] = 1 << i; 70 82 } 71 83 } -
to-imperative/trunk/libp++/pxx_common.hh
r109 r254 24 24 #define __pxx_common_hh__ 25 25 26 // 27 // This is needed to allow the use of macros for integer constant 28 // initialization. 29 #define __STDC_CONSTANT_MACROS 26 30 #include <inttypes.h> 31 27 32 #include <stdio.h> 28 33 #include <stdlib.h> … … 31 36 namespace pxx 32 37 { 33 34 #define __STDC_CONSTANT_MACROS35 38 36 39 #ifndef DEBUG … … 42 45 #endif 43 46 47 // 48 // Define macros for uintptr_t type. 44 49 #if UINTPTR_MAX == UINT32_MAX 45 50 #define UINTPTR_C(x) UINT32_C(x) … … 50 55 #endif 51 56 57 // 58 // Useful macros. 52 59 #define null (0) 53 54 60 #define self (*this) 55 56 typedef void* Ptr ;57 58 61 #define zref(_type) (*((_type*)(0))) 59 62 63 // 64 // A macro to use in a case of fatal errors. Prints a message and source file 65 // information (function name, file name and line number). 60 66 #define FATAL \ 61 67 fprintf( \ … … 72 78 } 73 79 80 extern unsigned debug_level ; 81 82 // 83 // A function to be called when fatal error occured. By default exit is called. 74 84 extern void (*terminate) (int _error_code) ; 85 // 86 // A function to print fatal error messages. 75 87 extern void fatal (char* _fmt, ...) ; 88 // 89 // A function to print debug messages. 90 extern void dprint (unsigned _level, char* _fmt, ...) ; 91 // 92 // A memory page size. Should be initialized at runtime due to architectures 93 // with a variable page size. 94 extern size_t page_size ; 95 // 96 // An array containing ceilings of binary logarythms of small integers. 97 extern size_t orders[256] ; 98 // 99 // An array containing exponents of 2. 100 extern size_t exps_of_two[sizeof(size_t) * 8] ; 76 101 77 extern size_t page_size ; 78 extern size_t orders [256] ; 79 extern size_t rounds [sizeof(size_t) * 8] ; 102 // 103 // This function returns a ceiling of a binary logarythm of its argument. 104 inline size_t get_order (size_t _size) ; 105 // 106 // This function checks whether a pointer is properly aligned. The second 107 // argument should be an exponent of 2. 108 inline bool ptr_is_aligned (void const* _ptr, uintptr_t _align) ; 109 // 110 // This function aligns a pointer, rounding it to a smaller value. 111 inline void* ptr_align (void const* _ptr, uintptr_t _align) ; 112 // 113 // This function aligns an integer size, rounding it to a smaller value. 114 inline size_t size_align (size_t _size, size_t _align) ; 115 // 116 // These functions implement bit operations on pointer values. 117 inline void* ptr_and (void const* _p1, void const* _p2) ; 118 inline void* ptr_or (void const* _p1, void const* _p2) ; 119 inline void* ptr_xor (void const* _p1, void const* _p2) ; 120 // 121 // This function finds the least significant bit by which two pointers differ 122 // and returns an integer where this bit is set. 123 inline uintptr_t ptr_diff (void const* _p1, void const* _p2) ; 124 // 125 // Pointer arithmetic. 126 template <typename type_t> 127 inline type_t* ptr_add_offset (type_t* _ptr, ssize_t _value) ; 128 template <typename type_t> 129 inline type_t* ptr_sub_offset (type_t* _ptr, ssize_t _value) ; 130 template <typename type1_t, typename type2_t> 131 inline ssize_t ptr_sub (type1_t* _ptr1, type2_t* _ptr2) ; 80 132 81 static inline 82 size_t get_order (size_t _size) 83 { 84 if (_size > 0) _size--; 85 #if 1 86 size_t b; 87 int n = -8; 88 do { 89 b = _size & size_t(0xFF); 90 _size >>= 8; 91 n += 8; 92 } while (_size != 0); 93 return orders [b] + n + 1; 94 #else 95 size_t n = 0; 96 if (_size & 0xFFFF0000) { _size &= 0xFFFF0000; n += 16; } 97 if (_size & 0xFF00FF00) { _size &= 0xFF00FF00; n += 8; } 98 if (_size & 0xF0F0F0F0) { _size &= 0xF0F0F0F0; n += 4; } 99 if (_size & 0xCCCCCCCC) { _size &= 0xCCCCCCCC; n += 2; } 100 if (_size & 0xAAAAAAAA) n += 1; 101 return n + 1; 102 #endif 103 } 104 105 static inline 106 bool aligned (void* _ptr, uintptr_t _align) 107 { 108 return ((_align - 1) & uintptr_t(_ptr)) == 0; 109 } 110 111 static inline 112 void* ptr_align (void* _ptr, uintptr_t _align) 113 { 114 return (void*)(((uintptr_t)_ptr) & (~(_align - 1))); 115 } 116 117 static inline 118 size_t size_align (size_t _size, size_t _align) 119 { 120 _align--; 121 return (_size + _align) & (~(_align)); 122 } 123 124 inline void* ptr_and (void* _p1, void* _p2) 125 { 126 return (void*)((uintptr_t)_p1 & (uintptr_t)_p2); 127 } 128 129 inline void* ptr_or (void* _p1, void* _p2) 130 { 131 return (void*)((uintptr_t)_p1 | (uintptr_t)_p2); 132 } 133 134 inline void* ptr_xor (void* _p1, void* _p2) 135 { 136 return (void*)((uintptr_t)_p1 ^ (uintptr_t)_p2); 137 } 138 139 inline void* ptr_diff (void* _p1, void* _p2) 140 { 141 uintptr_t diff = (uintptr_t)_p1 ^ (uintptr_t)_p2; 142 return (void*)(((diff ^ (diff - 1)) >> 1) + 1); 143 } 144 145 template <typename typeT> 146 inline typeT* ptr_add (typeT* _ptr, size_t _value) 147 { 148 return (typeT*)((char*)_ptr + _value); 149 } 150 151 template <typename typeT> 152 inline typeT* ptr_sub (typeT* _ptr, size_t _value) 153 { 154 return (typeT*)((char*)_ptr - _value); 155 } 156 157 template <typename typeT1, typename typeT2> 158 inline ssize_t sub_ptr (typeT1* _ptr1, typeT2* _ptr2) 159 { 160 return ((char*)_ptr1 - (char*)_ptr2); 161 } 162 133 // 134 // We provide our own maximum and minimum macros to avoid naming conflicts 163 135 #define pxx_max(a,b) ((a) > (b) ? (a) : (b)) 136 #define pxx_min(a,b) ((a) < (b) ? (a) : (b)) 164 137 165 138 }
Note: See TracChangeset
for help on using the changeset viewer.