Utils/Daemon: Add warning when the scheduler has registered events at a fork()
[senf.git] / Packets / PacketImpl.cc
index 8dfd75e..5b5e12f 100644 (file)
@@ -1,6 +1,8 @@
-// Copyright (C) 2007 
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+// $Id$
+//
+// Copyright (C) 2007
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
 //     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
+unsigned senf::detail::AnnotationIndexerBase::maxAnnotations (0);
+
 ///////////////////////////////////////////////////////////////////////////
 // senf::detail::PacketImpl
 
+prefix_ senf::detail::PacketImpl::~PacketImpl()
+{
+    // We increment refcount_ to ensure, release() won't call delete again
+    ++refcount_;
+    eraseInterpreters(interpreters_.begin(), interpreters_.end());
+    Annotations::const_iterator  i (annotations_.begin());
+    Annotations::const_iterator const i_end (annotations_.end());
+    std::vector<bool>::iterator small (AnnotationIndexerBase::small().begin());
+    for (; i != i_end; ++i, ++small)
+        if (! *small && *i)
+            delete *i;
+}
+
+// This function has a problem being inlined. Somehow, often when calling this, the size of the 
+// resulting inlined code would be huge. Need to further debug this.
+
+prefix_ void senf::detail::PacketImpl::release(refcount_t n)
+{
+    SENF_ASSERT(refcount_ >= n);
+    // uah ... we need to be extremely careful here. If refcount_ is n, we want to commit suicide,
+    // however the destructor will remove all PacketInterpreters from the list and will thereby
+    // decrement refcount -> only decrenebt refcount_ when *not* caling delete
+    if (refcount_ == n)
+        delete this;
+    else
+        refcount_ -= n;
+}
+
 // interpreter chain
 
 prefix_ void senf::detail::PacketImpl::appendInterpreter(PacketInterpreterBase * p)
@@ -57,7 +89,7 @@ prefix_ void senf::detail::PacketImpl::clear(PacketData * self)
         truncateInterpreters(n);
     iterator first (boost::next(begin(),self->begin_));
     data_.erase(first, boost::next(begin(),self->end_));
-    updateIterators(self,first,-self->size());
+    updateIterators(self,self->begin_,-self->size());
 }
 
 // private members
@@ -73,7 +105,7 @@ prefix_ void senf::detail::PacketImpl::eraseInterpreters(interpreter_list::itera
     }
 }
 
-prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, iterator pos,
+prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, difference_type pos,
                                                        difference_type n)
 {
     // I hate to change the PacketData representation from here, I would have preferred to let
@@ -101,7 +133,7 @@ prefix_ void senf::detail::PacketImpl::updateIterators(PacketData * self, iterat
     // c)
     interpreter_list::iterator const i_end (interpreters_.end());
     if (++i != i_end)
-        if (std::distance(begin(), pos) < difference_type(i->begin_))
+        if (pos <= difference_type(i->begin_))
             // pos is before the packet, it must then be before all futher packets ...
             for (; i != i_end; ++i) {
                 i->begin_ += n;