Utils/Daemon: Add warning when the scheduler has registered events at a fork()
[senf.git] / Packets / PacketImpl.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief PacketImpl inline non-template implementation */
25
26 // Custom includes
27 #include "../Utils/senfassert.hh"
28 // #include "PacketInterpreter.hh"
29
30 #define prefix_ inline
31 ///////////////////////////////cci.p///////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::detail::AnnotationIndexerBase
35
36 prefix_ std::vector<bool> & senf::detail::AnnotationIndexerBase::small()
37 {
38     static std::vector<bool> smalls;
39     return smalls;
40 }
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::detail::AnnotationP
44
45 prefix_ senf::detail::AnnotationP::~AnnotationP()
46 {}
47
48 // Memory management:
49 //
50 // * The PacketImpl destructor will *explicitly* clean-up the interpreters_ list by removing
51 //   each element from the list and deleting it if it's (intrusive) refcount is 0
52 // * The PacketInterpreters use safe hooks -> they know wether they are part of a list or not
53 // * PacketHandle has an intrusive_ptr to PacketInterpreterBase. The intrusive_ptr_add_ref
54 //   will refcount both the PacketImpl as well as the PacketInterpreterBase
55 // * intrusive_ptr_remove will only delete the object if it's not in a container
56 // * removing an object from the list will decrement the PacketImpl refcount accordingly
57 // * inserting an object into the list will incroment the PacketImpl refcount accordingly
58 // * each PacketInterpreterBase instance holds a *raw* pointer to the PacketImpl
59 //
60 // The following operations change refcounts:
61 //
62 // * intrusive_ptr_add_ref(PacketInterpreterBase *);
63 // * intrusive_ptr_remove(PacketInterpreterBase *);
64 // * PacketImpl::appendInterpreter();
65 // * PacketImpl::prependInterpreter();
66 // * PacketImpl::truncateInterpreters();
67 //
68 // The last three also modify the impl_ member accordingly by calling
69 // PacketInterpreterBase::assign/release
70
71 ///////////////////////////////////////////////////////////////////////////
72 // senf::detail::PacketImpl
73
74 prefix_ senf::detail::PacketImpl::PacketImpl()
75     : refcount_(0), annotations_(AnnotationIndexerBase::maxAnnotations, 0)
76 {}
77
78 prefix_ senf::detail::PacketImpl::PacketImpl(size_type size, byte initValue)
79     : refcount_(0), data_(size,initValue), annotations_(AnnotationIndexerBase::maxAnnotations, 0)
80 {}
81
82 // rerference/memory management
83
84 prefix_ void senf::detail::PacketImpl::add_ref(refcount_t n)
85 {
86     refcount_ += n;
87 }
88
89 prefix_ senf::detail::PacketImpl::refcount_t senf::detail::PacketImpl::refcount()
90     const
91 {
92     return refcount_;
93 }
94
95 // Interpreter chain
96
97 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::first()
98 {
99     return interpreters_.empty() ? 0 : & interpreters_.front();
100 }
101
102 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::last()
103 {
104     return interpreters_.empty() ? 0 : & interpreters_.back();
105 }
106
107 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::next(PacketInterpreterBase * p)
108 {
109     interpreter_list::iterator i (interpreter_list::current(*p));
110     return (++i == interpreters_.end()) ? 0 : &*i;
111 }
112
113 prefix_ senf::PacketInterpreterBase * senf::detail::PacketImpl::prev(PacketInterpreterBase * p)
114 {
115     interpreter_list::iterator i (interpreter_list::current(*p));
116     return (i == interpreters_.begin()) ? 0 : &*(--i);
117 }
118
119 prefix_ void senf::detail::PacketImpl::truncateInterpreters(PacketInterpreterBase * p)
120 {
121     Guard guard (this);
122     eraseInterpreters(interpreter_list::current(*p),interpreters_.end());
123 }
124
125 prefix_ void senf::detail::PacketImpl::truncateInterpretersBackwards(PacketInterpreterBase * p)
126 {
127     Guard guard (this);
128     eraseInterpreters(interpreters_.begin(),boost::next(interpreter_list::current(*p)));
129 }
130
131 // Data container
132
133 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::begin()
134 {
135     return data_.begin();
136 }
137
138 prefix_ senf::detail::PacketImpl::iterator senf::detail::PacketImpl::end()
139 {
140     return data_.end();
141 }
142
143 prefix_ senf::detail::PacketImpl::size_type senf::detail::PacketImpl::size()
144 {
145     return data_.size();
146 }
147
148 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, byte v)
149 {
150     difference_type ix (std::distance(begin(),pos));
151     data_.insert(pos,v);
152     updateIterators(self,ix,1);
153 }
154
155 prefix_ void senf::detail::PacketImpl::insert(PacketData * self, iterator pos, size_type n,
156                                               byte v)
157 {
158     difference_type ix (std::distance(begin(),pos));
159     data_.insert(pos,n,v);
160     updateIterators(self,ix,n);
161 }
162
163 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator pos)
164 {
165     difference_type ix (std::distance(begin(),pos));
166     data_.erase(pos);
167     updateIterators(self,ix,-1);
168 }
169
170 prefix_ void senf::detail::PacketImpl::erase(PacketData * self, iterator first, iterator last)
171 {
172     difference_type ix (std::distance(begin(),first));
173     difference_type delta (std::distance(first,last));
174     data_.erase(first,last);
175     updateIterators(self,ix,-delta);
176 }
177
178 ///////////////////////////////////////////////////////////////////////////
179 // senf::detail::PacketImpl::Guard
180
181 prefix_ senf::detail::PacketImpl::Guard::Guard(PacketImpl * impl)
182     : p (impl)
183 {
184     p->add_ref();
185 }
186
187 prefix_ senf::detail::PacketImpl::Guard::~Guard()
188 {
189     p->release();
190 }
191
192 ///////////////////////////////cci.e///////////////////////////////////////
193 #undef prefix_
194
195 \f
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u test"
203 // comment-column: 40
204 // End: