Changeset 254


Ignore:
Timestamp:
Nov 28, 2002, 4:51:54 PM (18 years ago)
Author:
pooh
Message:
  • Code cleanup and comments.
  • Inline definitions moved to .ih file.
Location:
to-imperative/trunk/libp++
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/libp++/pxx_common.cc

    r115 r254  
    11//
    2 // Copyright (C) 2000, 2001 Andrey Slepuhin <pooh@msu.ru>
     2// Copyright (C) 2000, 2001, 2002 Andrey Slepuhin <pooh@msu.ru>
    33//
    44// libp++ is free software; you can redistribute it and/or modify
     
    2121// Author: Andrey Slepuhin <pooh@msu.ru>
    2222
    23 #include "pxx_common.hh"
     23#include "pxx_common.ih"
    2424
    25 #include <cstdarg>
     25#include <stdarg.h>
    2626#include <unistd.h>
    2727
    2828namespace pxx
    2929{
     30
     31unsigned debug_level = 0 ;
    3032
    3133void (*terminate) (int _error_code) = null ;
     
    4244}
    4345
     46void 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
    4455size_t page_size ;
    4556size_t orders[256] ;
    46 size_t rounds[sizeof(size_t) * 8] ;
     57size_t exps_of_two[sizeof(size_t) * 8] ;
    4758
     59//
     60// Internal class used to initialize necessary data
    4861class Init
    4962{
     
    5972      unsigned s = i;
    6073      orders[i] = 0;
    61 //      if (s != 0) s--;
    6274      while (s) {
    6375        s >>= 1;
     
    6678      orders[i]--;
    6779    }
    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;
    7082    }
    7183  }
  • to-imperative/trunk/libp++/pxx_common.hh

    r109 r254  
    2424#define __pxx_common_hh__
    2525
     26//
     27// This is needed to allow the use of macros for integer constant
     28//  initialization.
     29#define __STDC_CONSTANT_MACROS
    2630#include <inttypes.h>
     31
    2732#include <stdio.h>
    2833#include <stdlib.h>
     
    3136namespace pxx
    3237{
    33 
    34 #define __STDC_CONSTANT_MACROS
    3538
    3639#ifndef DEBUG
     
    4245#endif
    4346
     47//
     48// Define macros for uintptr_t type.
    4449#if UINTPTR_MAX == UINT32_MAX
    4550#define UINTPTR_C(x) UINT32_C(x)
     
    5055#endif
    5156
     57//
     58// Useful macros.
    5259#define null (0)
    53 
    5460#define self (*this)
    55 
    56 typedef void* Ptr ;
    57 
    5861#define zref(_type) (*((_type*)(0)))
    5962
     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).
    6066#define FATAL \
    6167  fprintf( \
     
    7278  }
    7379
     80extern unsigned debug_level ;
     81
     82//
     83// A function to be called when fatal error occured. By default exit is called.
    7484extern void (*terminate) (int _error_code) ;
     85//
     86// A function to print fatal error messages.
    7587extern void fatal (char* _fmt, ...) ;
     88//
     89// A function to print debug messages.
     90extern 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.
     94extern size_t page_size ;
     95//
     96// An array containing ceilings of binary logarythms of small integers.
     97extern size_t orders[256] ;
     98//
     99// An array containing exponents of 2.
     100extern size_t exps_of_two[sizeof(size_t) * 8] ;
    76101
    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.
     104inline 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.
     108inline bool ptr_is_aligned (void const* _ptr, uintptr_t _align) ;
     109//
     110// This function aligns a pointer, rounding it to a smaller value.
     111inline void* ptr_align (void const* _ptr, uintptr_t _align) ;
     112//
     113// This function aligns an integer size, rounding it to a smaller value.
     114inline size_t size_align (size_t _size, size_t _align) ;
     115//
     116// These functions implement bit operations on pointer values.
     117inline void* ptr_and (void const* _p1, void const* _p2) ;
     118inline void* ptr_or (void const* _p1, void const* _p2) ;
     119inline 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.
     123inline uintptr_t ptr_diff (void const* _p1, void const* _p2) ;
     124//
     125// Pointer arithmetic.
     126template <typename type_t>
     127  inline type_t* ptr_add_offset (type_t* _ptr, ssize_t _value) ;
     128template <typename type_t>
     129  inline type_t* ptr_sub_offset (type_t* _ptr, ssize_t _value) ;
     130template <typename type1_t, typename type2_t>
     131  inline ssize_t ptr_sub (type1_t* _ptr1, type2_t* _ptr2) ;
    80132
    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
    163135#define pxx_max(a,b) ((a) > (b) ? (a) : (b))
     136#define pxx_min(a,b) ((a) < (b) ? (a) : (b))
    164137
    165138}
Note: See TracChangeset for help on using the changeset viewer.