[beast/devel: 21/77] BIRNET: remove all utility classes from birnetutils



commit 199e8594f005d4e5bb33596b85b15ee4248842bb
Author: Tim Janik <timj gnu org>
Date:   Fri Mar 29 01:52:19 2013 +0000

    BIRNET: remove all utility classes from birnetutils

 birnet/birnetutils.cc |  280 +------------------------------------------------
 birnet/birnetutils.hh |  253 ++-------------------------------------------
 2 files changed, 9 insertions(+), 524 deletions(-)
---
diff --git a/birnet/birnetutils.cc b/birnet/birnetutils.cc
index cbf8ca1..cb9825b 100644
--- a/birnet/birnetutils.cc
+++ b/birnet/birnetutils.cc
@@ -223,12 +223,8 @@ BIRNET_STARTUP_ASSERT (DBL_EPSILON  <= 1E-9);
 BIRNET_STATIC_ASSERT (LDBL_MIN     <= 1E-37);
 BIRNET_STATIC_ASSERT (LDBL_MAX     >= 1E+37);
 BIRNET_STATIC_ASSERT (LDBL_EPSILON <= 1E-9);
+
 /* --- assertions/warnings/errors --- */
-void
-raise_sigtrap ()
-{
-  raise (SIGTRAP);
-}
 static void
 stderr_print (bool        bail_out,
               const char *prefix,
@@ -321,29 +317,6 @@ birnet_runtime_problemv (char        ewran_tag,
       abort();
     }
 }
-/* --- VirtualTypeid --- */
-VirtualTypeid::~VirtualTypeid ()
-{ /* virtual destructor ensures vtable */ }
-String
-VirtualTypeid::typeid_name ()
-{
-  return typeid (*this).name();
-}
-String
-VirtualTypeid::typeid_pretty_name ()
-{
-  return cxx_demangle (typeid (*this).name());
-}
-String
-VirtualTypeid::cxx_demangle (const char *mangled_identifier)
-{
-  int status = 0;
-  char *malloced_result = abi::__cxa_demangle (mangled_identifier, NULL, NULL, &status);
-  String result = malloced_result && !status ? malloced_result : mangled_identifier;
-  if (malloced_result)
-    free (malloced_result);
-  return result;
-}
 
 /* --- file utils --- */
 /**
@@ -525,228 +498,7 @@ equals (const String &file1,
           st1.st_rdev == st2.st_rdev);
 }
 } // Path
-/* --- Deletable --- */
-Deletable::~Deletable ()
-{
-  invoke_deletion_hooks();
-}
-/**
- * @param deletable     possible Deletable* handle
- * @return              TRUE if the hook was added
- *
- * Adds the deletion hook to @a deletable if it is non NULL.
- * The deletion hook is asserted to be so far uninstalled.
- * This function is MT-safe and may be called from any thread.
- */
-bool
-Deletable::DeletionHook::deletable_add_hook (Deletable *deletable)
-{
-  if (deletable)
-    {
-      deletable->add_deletion_hook (this);
-      return true;
-    }
-  return false;
-}
-/**
- * @param deletable     possible Deletable* handle
- * @return              TRUE if the hook was removed
- *
- * Removes the deletion hook from @a deletable if it is non NULL.
- * The deletion hook is asserted to be installed on @a deletable.
- * This function is MT-safe and may be called from any thread.
- */
-bool
-Deletable::DeletionHook::deletable_remove_hook (Deletable *deletable)
-{
-  if (deletable)
-    {
-      deletable->remove_deletion_hook (this);
-      return true;
-    }
-  return false;
-}
-Deletable::DeletionHook::~DeletionHook ()
-{
-  if (this->next || this->prev)
-    g_error ("%s: hook is being destroyed but not unlinked: %p", G_STRFUNC, this);
-}
-#if 0
-static struct {
-  Mutex                                         mutex;
-  std::map<Deletable*,Deletable::DeletionHook*> dmap;
-} deletable_maps[19]; /* use prime size for hashing, sum up to roughly 1k (use 83 for 4k) */
-#endif
-#define DELETABLE_MAP_HASH      (19)    /* use prime size for hashing, sum up to roughly 1k (use 83 for 4k) 
*/
-struct DeletableMap {
-  Rapicorn::Mutex                               mutex;
-  std::map<Deletable*,Deletable::DeletionHook*> dmap;
-};
-static Rapicorn::Atomic<DeletableMap*> deletable_maps = NULL;
-static inline void
-auto_init_deletable_maps (void)
-{
-  if (UNLIKELY (deletable_maps == NULL))
-    {
-      DeletableMap *dmaps = new DeletableMap[DELETABLE_MAP_HASH];
-      if (!deletable_maps.cas ((DeletableMap*) NULL, dmaps))
-        delete dmaps;
-    }
-}
-/**
- * @param hook  valid deletion hook
- *
- * Add an uninstalled deletion hook to the deletable.
- * This function is MT-safe and may be called from any thread.
- */
-void
-Deletable::add_deletion_hook (DeletionHook *hook)
-{
-  auto_init_deletable_maps();
-  uint32 hashv = ((gsize) (void*) this) % DELETABLE_MAP_HASH;
-  deletable_maps[hashv].mutex.lock();
-  BIRNET_ASSERT (hook);
-  BIRNET_ASSERT (!hook->next);
-  BIRNET_ASSERT (!hook->prev);
-  std::map<Deletable*,DeletionHook*>::iterator it;
-  it = deletable_maps[hashv].dmap.find (this);
-  if (it != deletable_maps[hashv].dmap.end() && it->second)
-    {
-      hook->prev = it->second->prev;
-      hook->next = it->second;
-      hook->prev->next = hook;
-      hook->next->prev = hook;
-      it->second = hook;
-    }
-  else if (it != deletable_maps[hashv].dmap.end())
-    it->second = hook->prev = hook->next = hook;
-  else
-    deletable_maps[hashv].dmap[this] = hook->prev = hook->next = hook;
-  deletable_maps[hashv].mutex.unlock();
-  hook->monitoring_deletable (*this);
-  //g_printerr ("DELETABLE-ADD(%p,%p)\n", this, hook);
-}
-/**
- * @param hook  valid deletion hook
- *
- * Remove a previously added deletion hook.
- * This function is MT-safe and may be called from any thread.
- */
-void
-Deletable::remove_deletion_hook (DeletionHook *hook)
-{
-  auto_init_deletable_maps();
-  uint32 hashv = ((gsize) (void*) this) % DELETABLE_MAP_HASH;
-  deletable_maps[hashv].mutex.lock();
-  BIRNET_ASSERT (hook);
-  BIRNET_ASSERT (hook->next && hook->prev);
-  hook->next->prev = hook->prev;
-  hook->prev->next = hook->next;
-  std::map<Deletable*,DeletionHook*>::iterator it;
-  it = deletable_maps[hashv].dmap.find (this);
-  BIRNET_ASSERT (it != deletable_maps[hashv].dmap.end());
-  if (it->second == hook)
-    it->second = hook->next != hook ? hook->next : NULL;
-  hook->prev = NULL;
-  hook->next = NULL;
-  deletable_maps[hashv].mutex.unlock();
-  //g_printerr ("DELETABLE-REM(%p,%p)\n", this, hook);
-}
-/**
- * Invoke all deletion hooks installed on this deletable.
- */
-void
-Deletable::invoke_deletion_hooks()
-{
-  auto_init_deletable_maps();
-  uint32 hashv = ((gsize) (void*) this) % DELETABLE_MAP_HASH;
-  while (TRUE)
-    {
-      /* lookup hook list */
-      deletable_maps[hashv].mutex.lock();
-      std::map<Deletable*,DeletionHook*>::iterator it;
-      DeletionHook *hooks;
-      it = deletable_maps[hashv].dmap.find (this);
-      if (it != deletable_maps[hashv].dmap.end())
-        {
-          hooks = it->second;
-          deletable_maps[hashv].dmap.erase (it);
-        }
-      else
-        hooks = NULL;
-      deletable_maps[hashv].mutex.unlock();
-      /* we're done if all hooks have been procesed */
-      if (!hooks)
-        break;
-      /* process hooks */
-      while (hooks)
-        {
-          DeletionHook *hook = hooks;
-          hook->next->prev = hook->prev;
-          hook->prev->next = hook->next;
-          hooks = hook->next != hook ? hook->next : NULL;
-          hook->prev = hook->next = NULL;
-          //g_printerr ("DELETABLE-DISMISS(%p,%p)\n", this, hook);
-          hook->dismiss_deletable();
-        }
-    }
-}
 
-/* --- DataList --- */
-DataList::NodeBase::~NodeBase ()
-{}
-void
-DataList::set_data (NodeBase *node)
-{
-  /* delete old node */
-  NodeBase *it = rip_data (node->key);
-  if (it)
-    delete it;
-  /* prepend node */
-  node->next = nodes;
-  nodes = node;
-}
-DataList::NodeBase*
-DataList::get_data (DataKey<void> *key) const
-{
-  NodeBase *it;
-  for (it = nodes; it; it = it->next)
-    if (it->key == key)
-      return it;
-  return NULL;
-}
-DataList::NodeBase*
-DataList::rip_data (DataKey<void> *key)
-{
-  NodeBase *last = NULL, *it;
-  for (it = nodes; it; last = it, it = last->next)
-    if (it->key == key)
-      {
-        /* unlink existing node */
-        if (last)
-          last->next = it->next;
-        else
-          nodes = it->next;
-        it->next = NULL;
-        return it;
-      }
-  return NULL;
-}
-void
-DataList::clear_like_destructor()
-{
-  while (nodes)
-    {
-      NodeBase *it = nodes;
-      nodes = it->next;
-      it->next = NULL;
-      delete it;
-    }
-}
-DataList::~DataList()
-{
-  clear_like_destructor();
-}
 /* --- url handling --- */
 bool
 url_test_show (const char *url)
@@ -1005,35 +757,7 @@ memset4 (guint32        *mem,
   BIRNET_STATIC_ASSERT (sizeof (wchar_t) == 4);
   wmemset ((wchar_t*) mem, filler, length);
 }
-/* --- memory utils --- */
-void*
-malloc_aligned (gsize    total_size,
-                gsize    alignment,
-                guint8 **free_pointer)
-{
-  const bool  alignment_power_of_2 = (alignment & (alignment - 1)) == 0;
-  const gsize cache_line_size = 64; // ensure that no false sharing will occur (at begin and end of data)
-  if (alignment_power_of_2)
-    {
-      // for power of 2 alignment, we guarantee also cache line alignment
-      alignment = std::max (alignment, cache_line_size);
-      uint8 *aligned_mem = (uint8 *) g_malloc (total_size + (alignment - 1) + (cache_line_size - 1));
-      *free_pointer = aligned_mem;
-      if ((ptrdiff_t) aligned_mem % alignment)
-        aligned_mem += alignment - (ptrdiff_t) aligned_mem % alignment;
-      return aligned_mem;
-    }
-  else
-    {
-      uint8 *aligned_mem = (uint8 *) g_malloc (total_size + (alignment - 1) + (cache_line_size - 1) * 2);
-      *free_pointer = aligned_mem;
-      if ((ptrdiff_t) aligned_mem % cache_line_size)
-        aligned_mem += cache_line_size - (ptrdiff_t) aligned_mem % cache_line_size;
-      if ((ptrdiff_t) aligned_mem % alignment)
-        aligned_mem += alignment - (ptrdiff_t) aligned_mem % alignment;
-      return aligned_mem;
-    }
-}
+
 /* --- zintern support --- */
 #include <zlib.h>
 /**
diff --git a/birnet/birnetutils.hh b/birnet/birnetutils.hh
index 8a3ffc3..b9f607a 100644
--- a/birnet/birnetutils.hh
+++ b/birnet/birnetutils.hh
@@ -18,30 +18,11 @@ using namespace Rapicorn;
 namespace Birnet {
 using namespace Rapicorn;
 
-/* --- short integer types --- */
-typedef BirnetUInt8   uint8;
-typedef BirnetUInt16  uint16;
-typedef BirnetUInt32  uint32;
-typedef BirnetUInt64  uint64;
-typedef BirnetInt8    int8;
-typedef BirnetInt16   int16;
-typedef BirnetInt32   int32;
-typedef BirnetInt64   int64;
-typedef BirnetUnichar unichar;
-/* --- convenient stdc++ types --- */
-typedef std::string String;
 using std::vector;
 using std::map;
 using std::min;
 using std::max;
-class VirtualTypeid {
-protected:
-  virtual      ~VirtualTypeid      ();
-public:
-  String        typeid_name        ();
-  String        typeid_pretty_name ();
-  static String cxx_demangle       (const char *mangled_identifier);
-};
+
 /* --- implement assertion macros --- */
 #ifndef BIRNET__RUNTIME_PROBLEM
 #define BIRNET__RUNTIME_PROBLEM(ErrorWarningReturnAssertNotreach,domain,file,line,funcname,...) \
@@ -91,15 +72,6 @@ public:
   explicit InitHook (InitHookFunc _func,
                      int          _priority = 0);
 };
-/* --- assertions/warnings/errors --- */
-void    raise_sigtrap           ();
-#if (defined __i386__ || defined __x86_64__) && defined __GNUC__ && __GNUC__ >= 2
-//extern inline void BREAKPOINT() { __asm__ __volatile__ ("int $03"); }
-#elif defined __alpha__ && !defined __osf__ && defined __GNUC__ && __GNUC__ >= 2
-//extern inline void BREAKPOINT() { __asm__ __volatile__ ("bpt"); }
-#else   /* !__i386__ && !__alpha__ */
-//extern inline void BREAKPOINT() { raise_sigtrap(); }
-#endif  /* __i386__ */
 
 /* --- file/path functionality --- */
 namespace Path {
@@ -120,6 +92,7 @@ bool            check     (const String &file,
 bool            equals    (const String &file1,
                            const String &file2);
 } // Path
+
 /* --- url handling --- */
 void url_show                   (const char           *url);
 void url_show_with_cookie       (const char           *url,
@@ -129,26 +102,24 @@ bool url_test_show              (const char           *url);
 bool url_test_show_with_cookie  (const char          *url,
                                  const char           *url_title,
                                  const char           *cookie);
+
 /* --- cleanup registration --- */
 uint cleanup_add                (uint                  timeout_ms,
                                  void                (*destroy_data) (void*),
                                  void                 *data);
 void cleanup_force_handlers     (void);
+
 /* --- string utils --- */
 void memset4                   (uint32              *mem,
                                  uint32               filler,
                                  uint                 length);
-/* --- memory utils --- */
-void* malloc_aligned            (size_t                total_size,
-                                 size_t                alignment,
-                                 uint8               **free_pointer);
-/* --- C++ demangling --- */
-char*   cxx_demangle           (const char  *mangled_identifier); /* in birnetutilsxx.cc */
+
 /* --- zintern support --- */
 uint8*  zintern_decompress      (unsigned int          decompressed_size,
                                  const unsigned char  *cdata,
                                  unsigned int          cdata_size);
 void    zintern_free            (uint8                *dc_data);
+
 /* --- template errors --- */
 namespace TEMPLATE_ERROR {
 // to error out, call invalid_type<YourInvalidType>();
@@ -156,220 +127,10 @@ template<typename Type> void invalid_type () { bool force_compiler_error = void
 // to error out, derive from InvalidType<YourInvalidType>
 template<typename Type> class InvalidType;
 }
-/* --- Deletable --- */
-/**
- * Deletable is a virtual base class that can be derived from (usually with
- * public virtual) to ensure an object has a vtable and a virtual destructor.
- * Also, it allows deletion hooks to be called during the objects destructor,
- * by deriving from Birnet::Deletable::DeletionHook. No extra per-object space is
- * consumed to allow deletion hooks, which makes Deletable a suitable base
- * type for classes that may or may not need this feature (e.g. objects that
- * can but often aren't used for signal handler connections).
- */
-struct Deletable : public virtual VirtualTypeid {
-  /**
-   * DeletionHook is the base implementation class for hooks which are hooked
-   * up into the deletion phase of a Birnet::Deletable.
-   */
-  class DeletionHook {
-    DeletionHook    *prev;
-    DeletionHook    *next;
-    friend class Deletable;
-  protected:
-    virtual     ~DeletionHook          (); /* { if (deletable) deletable_remove_hook (deletable); deletable 
= NULL; } */
-    virtual void monitoring_deletable  (Deletable &deletable) = 0;
-    virtual void dismiss_deletable     () = 0;
-  public:
-    explicit     DeletionHook          () : prev (NULL), next (NULL) {}
-    bool         deletable_add_hook    (void      *any)              { return false; }
-    bool         deletable_add_hook    (Deletable *deletable);
-    bool         deletable_remove_hook (void      *any)              { return false; }
-    bool         deletable_remove_hook (Deletable *deletable);
-  };
-private:
-  void           add_deletion_hook     (DeletionHook *hook);
-  void           remove_deletion_hook  (DeletionHook *hook);
-protected:
-  void           invoke_deletion_hooks ();
-  virtual       ~Deletable             ();
-};
 
-/* --- ReferenceCountImpl --- */
 typedef Rapicorn::ReferenceCountable ReferenceCountImpl;
 
-/* --- generic named data --- */
-template<typename Type>
-class DataKey {
-private:
-  /*Copy*/        DataKey    (const DataKey&);
-  DataKey&        operator=  (const DataKey&);
-public:
-  /* explicit */  DataKey    ()                 { }
-  virtual Type    fallback   ()                 { Type d = Type(); return d; }
-  virtual void    destroy    (Type data)        { /* destruction hook */ }
-  virtual        ~DataKey    ()                 {}
-};
-class DataList {
-  class NodeBase {
-  protected:
-    NodeBase      *next;
-    DataKey<void> *key;
-    explicit       NodeBase (DataKey<void> *k) : next (NULL), key (k) {}
-    virtual       ~NodeBase ();
-    friend         class DataList;
-  };
-  template<typename T>
-  class Node : public NodeBase {
-    T data;
-  public:
-    T        get_data ()     { return data; }
-    T        swap     (T d)  { T result = data; data = d; return result; }
-    virtual ~Node()
-    {
-      if (key)
-        {
-          DataKey<T> *dkey = reinterpret_cast<DataKey<T>*> (key);
-          dkey->destroy (data);
-        }
-    }
-    explicit Node (DataKey<T> *k,
-                   T           d) :
-      NodeBase (reinterpret_cast<DataKey<void>*> (k)),
-      data (d)
-    {}
-  };
-  NodeBase *nodes;
-public:
-  DataList() :
-    nodes (NULL)
-  {}
-  template<typename T> void
-  set (DataKey<T> *key,
-       T           data)
-  {
-    Node<T> *node = new Node<T> (key, data);
-    set_data (node);
-  }
-  template<typename T> T
-  get (DataKey<T> *key) const
-  {
-    NodeBase *nb = get_data (reinterpret_cast<DataKey<void>*> (key));
-    if (nb)
-      {
-        Node<T> *node = reinterpret_cast<Node<T>*> (nb);
-        return node->get_data();
-      }
-    else
-      return key->fallback();
-  }
-  template<typename T> T
-  swap (DataKey<T> *key,
-        T           data)
-  {
-    NodeBase *nb = get_data (reinterpret_cast<DataKey<void>*> (key));
-    if (nb)
-      {
-        Node<T> *node = reinterpret_cast<Node<T>*> (nb);
-        return node->swap (data);
-      }
-    else
-      {
-        set (key, data);
-        return key->fallback();
-      }
-  }
-  template<typename T> T
-  swap (DataKey<T> *key)
-  {
-    NodeBase *nb = rip_data (reinterpret_cast<DataKey<void>*> (key));
-    if (nb)
-      {
-        Node<T> *node = reinterpret_cast<Node<T>*> (nb);
-        T d = node->get_data();
-        nb->key = NULL; // rip key to prevent data destruction
-        delete nb;
-        return d;
-      }
-    else
-      return key->fallback();
-  }
-  template<typename T> void
-  del (DataKey<T> *key)
-  {
-    NodeBase *nb = rip_data (reinterpret_cast<DataKey<void>*> (key));
-    if (nb)
-      delete nb;
-  }
-  void clear_like_destructor();
-  ~DataList();
-private:
-  void      set_data (NodeBase      *node);
-  NodeBase* get_data (DataKey<void> *key) const;
-  NodeBase* rip_data (DataKey<void> *key);
-};
-/* --- DataListContainer --- */
-class DataListContainer {
-  DataList data_list;
-public: /* generic data API */
-  template<typename Type> inline void set_data    (DataKey<Type> *key, Type data) { data_list.set (key, 
data); }
-  template<typename Type> inline Type get_data    (DataKey<Type> *key) const      { return data_list.get 
(key); }
-  template<typename Type> inline Type swap_data   (DataKey<Type> *key, Type data) { return data_list.swap 
(key, data); }
-  template<typename Type> inline Type swap_data   (DataKey<Type> *key)            { return data_list.swap 
(key); }
-  template<typename Type> inline void delete_data (DataKey<Type> *key)            { data_list.del (key); }
-};
-/* --- class to allocate aligned memory --- */
-template<class T, int ALIGN>
-class AlignedArray {
-  unsigned char *unaligned_mem;
-  T *data;
-  size_t n_elements;
-  void
-  allocate_aligned_data()
-  {
-    BIRNET_ASSERT ((ALIGN % sizeof (T)) == 0);
-    data = reinterpret_cast<T *> (malloc_aligned (n_elements * sizeof (T), ALIGN, &unaligned_mem));
-  }
-  /* no copy constructor and no assignment operator */
-  BIRNET_PRIVATE_CLASS_COPY (AlignedArray);
-public:
-  AlignedArray (const vector<T>& elements) :
-    n_elements (elements.size())
-  {
-    allocate_aligned_data();
-    for (size_t i = 0; i < n_elements; i++)
-      new (data + i) T (elements[i]);
-  }
-  AlignedArray (size_t n_elements) :
-    n_elements (n_elements)
-  {
-    allocate_aligned_data();
-    for (size_t i = 0; i < n_elements; i++)
-      new (data + i) T();
-  }
-  ~AlignedArray()
-  {
-    /* C++ destruction order: last allocated element is deleted first */
-    while (n_elements)
-      data[--n_elements].~T();
-    g_free (unaligned_mem);
-  }
-  T&
-  operator[] (size_t pos)
-  {
-    return data[pos];
-  }
-  const T&
-  operator[] (size_t pos) const
-  {
-    return data[pos];
-  }
-  size_t
-  size()
-  {
-    return n_elements;
-  }
-};
-
 } // Birnet
+
 #endif /* __BIRNET_UTILS_XX_HH__ */
 /* vim:set ts=8 sts=2 sw=2: */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]