1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> |
---|
2 | |
---|
3 | <HTML> |
---|
4 | |
---|
5 | <HEAD> |
---|
6 | |
---|
7 | <TITLE>Counted Body Techniques</TITLE> |
---|
8 | |
---|
9 | <META NAME="GENERATOR" CONTENT="Microsoft FrontPage 4.0"> |
---|
10 | |
---|
11 | <META NAME="Author" CONTENT="Kevlin Henney"> |
---|
12 | |
---|
13 | <META NAME="KeyWords" CONTENT="C++, Reference Counting, Advanced Techniques, Smart Pointers, Patterns"> |
---|
14 | |
---|
15 | </HEAD> |
---|
16 | |
---|
17 | <BODY bgcolor="#FFFFFF" text="#000000"> |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | <H1 ALIGN=CENTER><I><FONT SIZE=+4>Counted Body Techniques</FONT></I></H1> |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | <CENTER><P><B><FONT SIZE=+1><a href="../people/kevlin_henney.htm">Kevlin Henney</a><BR> |
---|
26 | |
---|
27 | </FONT>(<A HREF="mailto:kevlin@acm.org">kevlin@acm.org</A>, <a HREF="mailto:khenney@qatraining.com">khenney@qatraining.com</a>)</B></P></CENTER> |
---|
28 | |
---|
29 | |
---|
30 | |
---|
31 | <UL> |
---|
32 | |
---|
33 | <P>Reference counting techniques? Nothing new, you might think. Every good |
---|
34 | |
---|
35 | C++ text that takes you to an intermediate or advanced level will introduce |
---|
36 | |
---|
37 | the concept. It has been explored with such thoroughness in the past that |
---|
38 | |
---|
39 | you might be forgiven for thinking that everything that can be said has |
---|
40 | |
---|
41 | been said. Well, let's start from first principles and see if we can unearth |
---|
42 | |
---|
43 | something new....</P> |
---|
44 | |
---|
45 | </UL> |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | <HR WIDTH="100%"> |
---|
51 | <H2>And then there were none...</H2> |
---|
52 | |
---|
53 | |
---|
54 | <UL> |
---|
55 | |
---|
56 | <P>The principle behind reference counting is to keep a running usage count |
---|
57 | |
---|
58 | of an object so that when it falls to zero we know the object is unused. |
---|
59 | |
---|
60 | This is normally used to simplify the memory management for dynamically |
---|
61 | |
---|
62 | allocated objects: keep a count of the number of references held to that |
---|
63 | |
---|
64 | object and, on zero, delete the object.</P> |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | <P>How to keep a track of the number of users of an object? Well, normal |
---|
69 | |
---|
70 | pointers are quite dumb, and so an extra level of indirection is required |
---|
71 | |
---|
72 | to manage the count. This is essentially the P<FONT SIZE=-1>ROXY</FONT> |
---|
73 | |
---|
74 | pattern described in <I>Design Patterns</I> [Gamma, Helm, Johnson & |
---|
75 | |
---|
76 | Vlissides, Addison-Wesley, <FONT SIZE=-1>ISBN </FONT>0-201-63361-2]. The |
---|
77 | |
---|
78 | intent is given as</P> |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | <UL> |
---|
83 | |
---|
84 | <P><I>Provide a surrogate or placeholder for another object to control |
---|
85 | |
---|
86 | access to it.</I></P> |
---|
87 | |
---|
88 | </UL> |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | <P>Coplien [<I>Advanced C++ Programming Styles and Idioms</I>, Addison-Wesley, |
---|
93 | |
---|
94 | <FONT SIZE=-1>ISBN </FONT>0-201-56365-7] defines a set of idioms related |
---|
95 | |
---|
96 | to this essential separation of a handle and a body part. The <I>Taligent |
---|
97 | |
---|
98 | Guide to Designing Programs </I>[Addison-Wesley, <FONT SIZE=-1>ISBN </FONT>0-201-40888-0] |
---|
99 | |
---|
100 | identifies a number of specific categories for proxies (aka surrogates). |
---|
101 | |
---|
102 | Broadly speaking they fall into two general categories:</P> |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | <UL> |
---|
107 | |
---|
108 | <LI><I>Hidden</I>: The handle is the object of interest, hiding the body |
---|
109 | |
---|
110 | itself. The functionality of the handle is obtained by delegation to the |
---|
111 | |
---|
112 | body, and the user of the handle is unaware of the body. Reference counted |
---|
113 | |
---|
114 | strings offer a transparent optimisation. The body is shared between copies |
---|
115 | |
---|
116 | of a string until such a time as a change is needed, at which point a copy |
---|
117 | |
---|
118 | is made. Such a C<FONT SIZE=-1>OPY</FONT> <FONT SIZE=-1>ON</FONT> W<FONT SIZE=-1>RITE</FONT> |
---|
119 | |
---|
120 | pattern (a specialisation of L<FONT SIZE=-1>AZY</FONT> E<FONT SIZE=-1>VALUATION</FONT>) |
---|
121 | |
---|
122 | requires the use of a hidden reference counted body.</LI> |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | <LI><I>Explicit</I>: Here the body is of interest and the handle merely |
---|
127 | |
---|
128 | provides intelligence for its access and housekeeping. In C++ this is often |
---|
129 | |
---|
130 | implemented as the S<FONT SIZE=-1>MART</FONT> P<FONT SIZE=-1>OINTER</FONT> |
---|
131 | |
---|
132 | idiom. One such application is that of reference counted smart pointers |
---|
133 | |
---|
134 | that collaborate to keep a count of an object, deleting it when the count |
---|
135 | |
---|
136 | falls to zero.</LI> |
---|
137 | |
---|
138 | </UL> |
---|
139 | |
---|
140 | </UL> |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | <HR WIDTH="100%"> |
---|
146 | <H2>Attached vs detached</H2> |
---|
147 | |
---|
148 | |
---|
149 | <UL> |
---|
150 | |
---|
151 | <P>For reference counted smart pointers there are two places the count |
---|
152 | |
---|
153 | can exist, resulting in two different patterns, both outlined in <I>Software |
---|
154 | |
---|
155 | Patterns</I> [Coplien, SIGS, <FONT SIZE=-1>ISBN </FONT>0-884842-50-X]:</P> |
---|
156 | |
---|
157 | |
---|
158 | |
---|
159 | <UL> |
---|
160 | |
---|
161 | <LI>C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT> or A<FONT SIZE=-1>TTACHED</FONT> |
---|
162 | |
---|
163 | C<FONT SIZE=-1>OUNTED</FONT> H<FONT SIZE=-1>ANDLE</FONT>/B<FONT SIZE=-1>ODY</FONT> |
---|
164 | |
---|
165 | places the count within the object being counted. The benefits are that |
---|
166 | |
---|
167 | countability is a part of the object being counted, and that reference |
---|
168 | |
---|
169 | counting does not require an additional object. The drawbacks are clearly |
---|
170 | |
---|
171 | that this is intrusive, and that the space for the reference count is wasted |
---|
172 | |
---|
173 | when the object is not heap based. Therefore the reference counting ties |
---|
174 | |
---|
175 | you to a particular implementation and style of use.</LI> |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | <LI>D<FONT SIZE=-1>ETACHED</FONT> C<FONT SIZE=-1>OUNTED</FONT> H<FONT SIZE=-1>ANDLE</FONT>/B<FONT SIZE=-1>ODY</FONT> |
---|
180 | |
---|
181 | places the count outside the object being counted, such that they are handled |
---|
182 | |
---|
183 | together. The clear benefit of this is that this technique is completely |
---|
184 | |
---|
185 | unintrusive, with all of the intelligence and support apparatus in the |
---|
186 | |
---|
187 | smart pointer, and therefore can be used on classes created independently |
---|
188 | |
---|
189 | of the reference counted pointer. The main disadvantage is that frequent |
---|
190 | |
---|
191 | use of this can lead to a proliferation of small objects, i.e. the counter, |
---|
192 | |
---|
193 | being created on the heap.</LI> |
---|
194 | |
---|
195 | </UL> |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | <P>Even with this simple analysis, it seems that the D<FONT SIZE=-1>ETACHED</FONT> |
---|
200 | |
---|
201 | C<FONT SIZE=-1>OUNTED</FONT> H<FONT SIZE=-1>ANDLE</FONT>/B<FONT SIZE=-1>ODY</FONT> |
---|
202 | |
---|
203 | approach is ahead. Indeed, with the increasing use of templates this is |
---|
204 | |
---|
205 | often the favourite, and is the principle behind the common - but not standard |
---|
206 | |
---|
207 | - <TT><FONT SIZE=+1>counted_ptr</FONT></TT>. |
---|
208 | <I>[The Boost name is <a href="../libs/smart_ptr/shared_ptr.htm"><TT><FONT SIZE=+1>shared_ptr</FONT></TT></a> |
---|
209 | |
---|
210 | rather than <TT><FONT SIZE=+1>counted_ptr</FONT></TT>.]</I></P> |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | <P>A common implementation of C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT> |
---|
215 | |
---|
216 | is to provide the counting mechanism in a base class that the counted type |
---|
217 | |
---|
218 | is derived from. Either that, or the reference counting mechanism is provided |
---|
219 | |
---|
220 | anew for each class that needs it. Both of these approaches are unsatisfactory |
---|
221 | |
---|
222 | because they are quite closed, coupling a class into a particular framework. |
---|
223 | |
---|
224 | Added to this the non-cohesiveness of having the count lying dormant in |
---|
225 | |
---|
226 | a non-counted object, and you get the feeling that excepting its use in |
---|
227 | |
---|
228 | widespread object models such as COM and CORBA the C<FONT SIZE=-1>OUNTED</FONT> |
---|
229 | |
---|
230 | B<FONT SIZE=-1>ODY</FONT> approach is perhaps only of use in specialised |
---|
231 | |
---|
232 | situations.</P> |
---|
233 | |
---|
234 | </UL> |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | <HR WIDTH="100%"> |
---|
239 | <H2>A requirements based approach</H2> |
---|
240 | |
---|
241 | |
---|
242 | <UL> |
---|
243 | |
---|
244 | <P>It is the question of openness that convinced me to revisit the problems |
---|
245 | |
---|
246 | with the C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT> idiom. |
---|
247 | |
---|
248 | Yes, there is a certain degree of intrusion expected when using this idiom, |
---|
249 | |
---|
250 | but is there anyway to minimise this and decouple the choice of counting |
---|
251 | |
---|
252 | mechanism from the smart pointer type used?</P> |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | <P>In recent years the most instructive body of code and specification |
---|
257 | |
---|
258 | for constructing open general purpose components has been the Stepanov |
---|
259 | |
---|
260 | and Lee's STL (Standard Template Library), now part of the C++ standard |
---|
261 | |
---|
262 | library. The STL approach makes extensive use of compile time polymorphism |
---|
263 | |
---|
264 | based on well defined operational requirements for types. For instance, |
---|
265 | |
---|
266 | each container, contained and iterator type is defined by the operations |
---|
267 | |
---|
268 | that should be performable on an object of that type, often with annotations |
---|
269 | |
---|
270 | describing additional constraints. Compile time polymorphism, as its name |
---|
271 | |
---|
272 | suggests, resolves functions at compile time based on function name and |
---|
273 | |
---|
274 | argument usage, i.e. overloading. This is less intrusive, although less |
---|
275 | |
---|
276 | easily diagnosed if incorrect, than runtime poymorphism that is based on |
---|
277 | |
---|
278 | types, names and function signatures.</P> |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | <P>This requirements based approach can be applied to reference counting. |
---|
283 | |
---|
284 | The operations we need for a type to be <I>Countable</I> are loosely:</P> |
---|
285 | |
---|
286 | |
---|
287 | |
---|
288 | <UL> |
---|
289 | |
---|
290 | <LI>An <TT><FONT SIZE=+1>acquire</FONT></TT> operation that registers interest |
---|
291 | |
---|
292 | in a <I>Countable </I>object.</LI> |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | <LI>A <TT><FONT SIZE=+1>release</FONT></TT> operation unregisters interest |
---|
297 | |
---|
298 | in a <I>Countable </I>object.</LI> |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | <LI>An <TT><FONT SIZE=+1>acquired</FONT></TT> query that returns whether |
---|
303 | |
---|
304 | or not a <I>Countable </I>object is currently acquired.</LI> |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | <LI>A <TT><FONT SIZE=+1>dispose</FONT></TT> operation that is responsible |
---|
309 | |
---|
310 | for disposing of an object that is no longer acquired.</LI> |
---|
311 | |
---|
312 | </UL> |
---|
313 | |
---|
314 | |
---|
315 | |
---|
316 | <P>Note that the count is deduced as a part of the abstract state of this |
---|
317 | |
---|
318 | type, and is not mentioned or defined in any other way. The openness of |
---|
319 | |
---|
320 | this approach derives in part from the use of global functions, meaning |
---|
321 | |
---|
322 | that no particular member functions are implied; a perfect way to wrap |
---|
323 | |
---|
324 | up an existing counted body class without modifying the class itself. The |
---|
325 | |
---|
326 | other aspect to the openness comes from a more precise specification of |
---|
327 | |
---|
328 | the operations.</P> |
---|
329 | |
---|
330 | |
---|
331 | |
---|
332 | <P>For a type to be <I>Countable</I> it must satisfy the following requirements, |
---|
333 | |
---|
334 | where <TT><FONT SIZE=+1>ptr</FONT></TT> is a non-null pointer to a single |
---|
335 | |
---|
336 | object (i.e. not an array) of the type, and <I><TT><FONT SIZE=+1>#function</FONT></TT></I> |
---|
337 | |
---|
338 | indicates number of calls to <TT><FONT SIZE=+1><I>function(</I>ptr<I>)</I></FONT></TT>:</P> |
---|
339 | |
---|
340 | |
---|
341 | |
---|
342 | <CENTER><TABLE BORDER=1 CELLSPACING=2 CELLPADDING=2 > |
---|
343 | |
---|
344 | <TR> |
---|
345 | |
---|
346 | <TD><I>Expression</I></TD> |
---|
347 | |
---|
348 | |
---|
349 | |
---|
350 | <TD><I>Return type</I></TD> |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | <TD><I>Semantics and notes</I></TD> |
---|
355 | |
---|
356 | </TR> |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | <TR> |
---|
361 | |
---|
362 | <TD><TT><FONT SIZE=+1>acquire(ptr)</FONT></TT></TD> |
---|
363 | |
---|
364 | |
---|
365 | |
---|
366 | <TD>no requirement</TD> |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | <TD><I>post</I>: <TT><FONT SIZE=+1>acquired(ptr)</FONT></TT></TD> |
---|
371 | |
---|
372 | </TR> |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | <TR> |
---|
377 | |
---|
378 | <TD><TT><FONT SIZE=+1>release(ptr)</FONT></TT></TD> |
---|
379 | |
---|
380 | |
---|
381 | |
---|
382 | <TD>no requirement</TD> |
---|
383 | |
---|
384 | |
---|
385 | |
---|
386 | <TD><I>pre</I>: <TT><FONT SIZE=+1>acquired(ptr)<BR> |
---|
387 | |
---|
388 | </FONT></TT><I>post</I>: <TT><FONT SIZE=+1>acquired(ptr) == #acquire - |
---|
389 | |
---|
390 | #release</FONT></TT></TD> |
---|
391 | |
---|
392 | </TR> |
---|
393 | |
---|
394 | |
---|
395 | |
---|
396 | <TR> |
---|
397 | |
---|
398 | <TD><TT><FONT SIZE=+1>acquired(ptr)</FONT></TT></TD> |
---|
399 | |
---|
400 | |
---|
401 | |
---|
402 | <TD>convertible to <TT><FONT SIZE=+1>bool</FONT></TT></TD> |
---|
403 | |
---|
404 | |
---|
405 | |
---|
406 | <TD><I>return</I>: <TT><FONT SIZE=+1>#acquire > #release</FONT></TT></TD> |
---|
407 | |
---|
408 | </TR> |
---|
409 | |
---|
410 | |
---|
411 | |
---|
412 | <TR> |
---|
413 | |
---|
414 | <TD><TT><FONT SIZE=+1>dispose(ptr, ptr)</FONT></TT></TD> |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | <TD>no requirement</TD> |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | <TD><I>pre</I>: <TT><FONT SIZE=+1>!acquired(ptr)<BR> |
---|
423 | |
---|
424 | </FONT></TT><I>post</I>: <TT><FONT SIZE=+1>*ptr</FONT></TT> no longer usable</TD> |
---|
425 | |
---|
426 | </TR> |
---|
427 | |
---|
428 | </TABLE></CENTER> |
---|
429 | |
---|
430 | |
---|
431 | |
---|
432 | <P>Note that the two arguments to <TT><FONT SIZE=+1>dispose</FONT></TT> |
---|
433 | |
---|
434 | are to support selection of the appropriate type safe version of the function |
---|
435 | |
---|
436 | to be called. In the general case the intent is that the first argument |
---|
437 | |
---|
438 | determines the type to be deleted, and would typically be templated, while |
---|
439 | |
---|
440 | the second selects which template to use, e.g. by conforming to a specific |
---|
441 | |
---|
442 | base class.</P> |
---|
443 | |
---|
444 | |
---|
445 | |
---|
446 | <P>In addition the following requirements must also be satisfied, where |
---|
447 | |
---|
448 | <TT><FONT SIZE=+1>null</FONT></TT> is a null pointer to the <I>Countable</I> |
---|
449 | |
---|
450 | type:</P> |
---|
451 | |
---|
452 | |
---|
453 | |
---|
454 | <CENTER><TABLE BORDER=1 > |
---|
455 | |
---|
456 | <TR> |
---|
457 | |
---|
458 | <TD><I>Expression</I></TD> |
---|
459 | |
---|
460 | |
---|
461 | |
---|
462 | <TD><I>Return type</I></TD> |
---|
463 | |
---|
464 | |
---|
465 | |
---|
466 | <TD><I>Semantics and notes</I></TD> |
---|
467 | |
---|
468 | </TR> |
---|
469 | |
---|
470 | |
---|
471 | |
---|
472 | <TR> |
---|
473 | |
---|
474 | <TD><TT><FONT SIZE=+1>acquire(null)</FONT></TT></TD> |
---|
475 | |
---|
476 | |
---|
477 | |
---|
478 | <TD>no requirement</TD> |
---|
479 | |
---|
480 | |
---|
481 | |
---|
482 | <TD><I>action</I>: none</TD> |
---|
483 | |
---|
484 | </TR> |
---|
485 | |
---|
486 | |
---|
487 | |
---|
488 | <TR> |
---|
489 | |
---|
490 | <TD><TT><FONT SIZE=+1>release(null)</FONT></TT></TD> |
---|
491 | |
---|
492 | |
---|
493 | |
---|
494 | <TD>no requirement</TD> |
---|
495 | |
---|
496 | |
---|
497 | |
---|
498 | <TD><I>action</I>: none</TD> |
---|
499 | |
---|
500 | </TR> |
---|
501 | |
---|
502 | |
---|
503 | |
---|
504 | <TR> |
---|
505 | |
---|
506 | <TD><TT><FONT SIZE=+1>acquired(null)</FONT></TT></TD> |
---|
507 | |
---|
508 | |
---|
509 | |
---|
510 | <TD>convertible to <TT><FONT SIZE=+1>bool</FONT></TT></TD> |
---|
511 | |
---|
512 | |
---|
513 | |
---|
514 | <TD><I>return</I>: <TT><FONT SIZE=+1>false</FONT></TT></TD> |
---|
515 | |
---|
516 | </TR> |
---|
517 | |
---|
518 | |
---|
519 | |
---|
520 | <TR> |
---|
521 | |
---|
522 | <TD><TT><FONT SIZE=+1>dispose(null, null)</FONT></TT></TD> |
---|
523 | |
---|
524 | |
---|
525 | |
---|
526 | <TD>no requirement</TD> |
---|
527 | |
---|
528 | |
---|
529 | |
---|
530 | <TD><I>action</I>: none</TD> |
---|
531 | |
---|
532 | </TR> |
---|
533 | |
---|
534 | </TABLE></CENTER> |
---|
535 | |
---|
536 | |
---|
537 | |
---|
538 | <P>Note that there are no requirements on these functions in terms of exceptions |
---|
539 | |
---|
540 | thrown or not thrown, except that if exceptions are thrown the functions |
---|
541 | |
---|
542 | themselves should be exception safe.</P> |
---|
543 | |
---|
544 | </UL> |
---|
545 | |
---|
546 | |
---|
547 | |
---|
548 | <HR WIDTH="100%"> |
---|
549 | <H2>Getting smart</H2> |
---|
550 | |
---|
551 | |
---|
552 | <UL> |
---|
553 | |
---|
554 | <P>Given the <I>Countable</I> requirements for a type, it is possible to |
---|
555 | |
---|
556 | define a generic smart pointer type that uses them for reference counting:</P> |
---|
557 | |
---|
558 | |
---|
559 | |
---|
560 | <UL> |
---|
561 | <PRE><TT>template<typename countable_type> |
---|
562 | class countable_ptr |
---|
563 | { |
---|
564 | public: // construction and destruction |
---|
565 | |
---|
566 | explicit countable_ptr(countable_type *); |
---|
567 | countable_ptr(const countable_ptr &); |
---|
568 | ~countable_ptr(); |
---|
569 | |
---|
570 | public: // access |
---|
571 | |
---|
572 | countable_type *operator->() const; |
---|
573 | countable_type &operator*() const; |
---|
574 | countable_type *get() const; |
---|
575 | |
---|
576 | public: // modification |
---|
577 | |
---|
578 | countable_ptr &clear(); |
---|
579 | countable_ptr &assign(countable_type *); |
---|
580 | countable_ptr &assign(const countable_ptr &); |
---|
581 | countable_ptr &operator=(const countable_ptr &); |
---|
582 | |
---|
583 | private: // representation |
---|
584 | |
---|
585 | countable_type *body; |
---|
586 | |
---|
587 | }; |
---|
588 | </TT></PRE> |
---|
589 | |
---|
590 | </UL> |
---|
591 | |
---|
592 | |
---|
593 | |
---|
594 | <P>The interface to this class has been kept intentionally simple, e.g. |
---|
595 | |
---|
596 | member templates and <TT><FONT SIZE=+1>throw</FONT></TT> specs have been |
---|
597 | |
---|
598 | omitted, for exposition. The majority of the functions are quite simple |
---|
599 | |
---|
600 | in implementation, relying very much on the <TT><FONT SIZE=+1>assign</FONT></TT> |
---|
601 | |
---|
602 | member as a keystone function:</P> |
---|
603 | |
---|
604 | |
---|
605 | |
---|
606 | <UL> |
---|
607 | |
---|
608 | <PRE><TT>template<typename countable_type> |
---|
609 | countable_ptr<countable_type>::countable_ptr(countable_type *initial) |
---|
610 | : body(initial) |
---|
611 | { |
---|
612 | acquire(body); |
---|
613 | } |
---|
614 | |
---|
615 | template<typename countable_type> |
---|
616 | countable_ptr<countable_type>::countable_ptr(const countable_ptr &other) |
---|
617 | : body(other.body) |
---|
618 | { |
---|
619 | acquire(body); |
---|
620 | } |
---|
621 | |
---|
622 | template<typename countable_type> |
---|
623 | countable_ptr<countable_type>::~countable_ptr() |
---|
624 | { |
---|
625 | clear(); |
---|
626 | } |
---|
627 | |
---|
628 | template<typename countable_type> |
---|
629 | countable_type *countable_ptr<countable_type>::operator->() const |
---|
630 | { |
---|
631 | return body; |
---|
632 | } |
---|
633 | |
---|
634 | template<typename countable_type> |
---|
635 | countable_type &countable_ptr<countable_type>::operator*() const |
---|
636 | { |
---|
637 | return *body; |
---|
638 | } |
---|
639 | |
---|
640 | template<typename countable_type> |
---|
641 | countable_type *countable_ptr<countable_type>::get() const |
---|
642 | { |
---|
643 | return body; |
---|
644 | } |
---|
645 | |
---|
646 | template<typename countable_type> |
---|
647 | countable_ptr<countable_type> &countable_ptr<countable_type>::clear() |
---|
648 | { |
---|
649 | return assign(0); |
---|
650 | } |
---|
651 | |
---|
652 | template<typename countable_type> |
---|
653 | countable_ptr<countable_type> &countable_ptr<countable_type>::assign(countable_type *rhs) |
---|
654 | { |
---|
655 | // set to rhs (uses Copy Before Release idiom which is self assignment safe) |
---|
656 | acquire(rhs); |
---|
657 | countable_type *old_body = body; |
---|
658 | body = rhs; |
---|
659 | |
---|
660 | // tidy up |
---|
661 | release(old_body); |
---|
662 | if(!acquired(old_body)) |
---|
663 | { |
---|
664 | dispose(old_body, old_body); |
---|
665 | } |
---|
666 | |
---|
667 | return *this; |
---|
668 | } |
---|
669 | |
---|
670 | template<typename countable_type> |
---|
671 | countable_ptr<countable_type> &countable_ptr<countable_type>::assign(const countable_ptr &rhs) |
---|
672 | { |
---|
673 | return assign(rhs.body); |
---|
674 | } |
---|
675 | |
---|
676 | template<typename countable_type> |
---|
677 | countable_ptr<countable_type> &countable_ptr<countable_type>::operator=(const countable_ptr &rhs) |
---|
678 | { |
---|
679 | return assign(rhs); |
---|
680 | } |
---|
681 | </TT></PRE> |
---|
682 | |
---|
683 | </UL> |
---|
684 | |
---|
685 | </UL> |
---|
686 | |
---|
687 | |
---|
688 | |
---|
689 | <HR WIDTH="100%"> |
---|
690 | <H2>Public accountability</H2> |
---|
691 | |
---|
692 | |
---|
693 | <UL> |
---|
694 | |
---|
695 | <P>Conformance to the requirements means that a type can be used with <TT><FONT SIZE=+1>countable_ptr</FONT></TT>. |
---|
696 | |
---|
697 | Here is an implementation mix-in class (<I>mix-imp</I>) that confers countability |
---|
698 | |
---|
699 | on its derived classes through member functions. This class can be used |
---|
700 | |
---|
701 | as a class adaptor:</P> |
---|
702 | |
---|
703 | |
---|
704 | |
---|
705 | <UL> |
---|
706 | |
---|
707 | <PRE><TT>class countability |
---|
708 | { |
---|
709 | public: // manipulation |
---|
710 | |
---|
711 | void acquire() const; |
---|
712 | void release() const; |
---|
713 | size_t acquired() const; |
---|
714 | |
---|
715 | protected: // construction and destruction |
---|
716 | |
---|
717 | countability(); |
---|
718 | ~countability(); |
---|
719 | |
---|
720 | private: // representation |
---|
721 | |
---|
722 | mutable size_t count; |
---|
723 | |
---|
724 | private: // prevention |
---|
725 | |
---|
726 | countability(const countability &); |
---|
727 | countability &operator=(const countability &); |
---|
728 | |
---|
729 | }; |
---|
730 | </TT></PRE> |
---|
731 | |
---|
732 | </UL> |
---|
733 | |
---|
734 | |
---|
735 | |
---|
736 | <P>Notice that the manipulation functions are <TT><FONT SIZE=+1>const</FONT></TT> |
---|
737 | |
---|
738 | and that the <TT><FONT SIZE=+1>count</FONT></TT> member itself is <TT><FONT SIZE=+1>mutable</FONT></TT>. |
---|
739 | |
---|
740 | This is because countability is not a part of an object's abstract state: |
---|
741 | |
---|
742 | memory management does not depend on the <TT><FONT SIZE=+1>const</FONT></TT>-ness |
---|
743 | |
---|
744 | or otherwise of an object. I won't include the definitions of the member |
---|
745 | |
---|
746 | functions here as you can probably guess them: increment, decrement and |
---|
747 | |
---|
748 | return the current count, respectively for the manipulation functions. |
---|
749 | |
---|
750 | In a multithreaded environment you should ensure that such read and write |
---|
751 | |
---|
752 | operations are atomic.</P> |
---|
753 | |
---|
754 | |
---|
755 | |
---|
756 | <P>So how do we make this class <I>Countable</I>? A simple set of forwarding |
---|
757 | |
---|
758 | functions does the job:</P> |
---|
759 | |
---|
760 | |
---|
761 | |
---|
762 | <UL> |
---|
763 | |
---|
764 | <PRE><TT>void acquire(const countability *ptr) |
---|
765 | { |
---|
766 | if(ptr) |
---|
767 | { |
---|
768 | ptr->acquire(); |
---|
769 | } |
---|
770 | } |
---|
771 | |
---|
772 | void release(const countability *ptr) |
---|
773 | { |
---|
774 | if(ptr) |
---|
775 | { |
---|
776 | ptr->release(); |
---|
777 | } |
---|
778 | } |
---|
779 | |
---|
780 | size_t acquired(const countability *ptr) |
---|
781 | { |
---|
782 | return ptr ? ptr->acquired() : 0; |
---|
783 | } |
---|
784 | |
---|
785 | template<class countability_derived> |
---|
786 | void dispose(const countability_derived *ptr, const countability *) |
---|
787 | { |
---|
788 | delete ptr; |
---|
789 | } |
---|
790 | </TT></PRE> |
---|
791 | |
---|
792 | </UL> |
---|
793 | |
---|
794 | |
---|
795 | |
---|
796 | <P>Any type that now derives from <TT><FONT SIZE=+1>countability</FONT></TT> |
---|
797 | |
---|
798 | may now be used with <TT><FONT SIZE=+1>countable_ptr</FONT></TT>:</P> |
---|
799 | |
---|
800 | |
---|
801 | |
---|
802 | <UL> |
---|
803 | |
---|
804 | <PRE><TT>class example : public countability |
---|
805 | { |
---|
806 | ... |
---|
807 | }; |
---|
808 | |
---|
809 | void simple() |
---|
810 | { |
---|
811 | countable_ptr<example> ptr(new example); |
---|
812 | countable_ptr<example> qtr(ptr); |
---|
813 | ptr.clear(); // set ptr to point to null |
---|
814 | } // allocated object deleted when qtr destructs |
---|
815 | </TT></PRE> |
---|
816 | </UL> |
---|
817 | </UL> |
---|
818 | |
---|
819 | |
---|
820 | |
---|
821 | |
---|
822 | <HR WIDTH="100%"> |
---|
823 | <H2>Runtime mixin</H2> |
---|
824 | |
---|
825 | |
---|
826 | <UL> |
---|
827 | |
---|
828 | <P>The challenge is to apply C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT> |
---|
829 | |
---|
830 | in a non-intrusive fashion, such that there is no overhead when an object |
---|
831 | |
---|
832 | is not counted. What we would like to do is confer this capability on a |
---|
833 | |
---|
834 | per object rather than on a per class basis. Effectively we are after <I>Countability |
---|
835 | |
---|
836 | </I>on any object, i.e. anything pointed to by a <TT><FONT SIZE=+1>void |
---|
837 | |
---|
838 | *</FONT></TT>! It goes without saying that <TT><FONT SIZE=+1>void</FONT></TT> |
---|
839 | |
---|
840 | is perhaps the least committed of any type.</P> |
---|
841 | |
---|
842 | |
---|
843 | |
---|
844 | <P>The forces to resolve on this are quite interesting, to say the least. |
---|
845 | |
---|
846 | Interesting, but not insurmountable. Given that the class of a runtime |
---|
847 | |
---|
848 | object cannot change dynamically in any well defined manner, and the layout |
---|
849 | |
---|
850 | of the object must be fixed, we have to find a new place and time to add |
---|
851 | |
---|
852 | the counting state. The fact that this must be added only on heap creation |
---|
853 | |
---|
854 | suggests the following solution:</P> |
---|
855 | |
---|
856 | |
---|
857 | |
---|
858 | <UL> |
---|
859 | |
---|
860 | <PRE><TT>struct countable_new; |
---|
861 | extern const countable_new countable; |
---|
862 | |
---|
863 | void *operator new(size_t, const countable_new &); |
---|
864 | void operator delete(void *, const countable_new &);</TT></PRE> |
---|
865 | </UL> |
---|
866 | |
---|
867 | |
---|
868 | |
---|
869 | <P>We have overloaded <TT><FONT SIZE=+1>operator new</FONT></TT> with a |
---|
870 | |
---|
871 | dummy argument to distinguish it from the regular global <TT><FONT SIZE=+1>operator |
---|
872 | |
---|
873 | new</FONT></TT>. This is comparable to the use of the <TT><FONT SIZE=+1>std::nothrow_t</FONT></TT> |
---|
874 | |
---|
875 | type and <TT><FONT SIZE=+1>std::nothrow</FONT></TT> object in the standard |
---|
876 | |
---|
877 | library. The placement <TT><FONT SIZE=+1>operator delete</FONT></TT> is |
---|
878 | |
---|
879 | there to perform any tidy up in the event of failed construction. Note |
---|
880 | |
---|
881 | that this is not yet supported on all that many compilers.</P> |
---|
882 | |
---|
883 | |
---|
884 | |
---|
885 | <P>The result of a <TT><FONT SIZE=+1>new</FONT></TT> expression using <TT><FONT SIZE=+1>countable</FONT></TT> |
---|
886 | |
---|
887 | is an object allocated on the heap that has a header block that holds the |
---|
888 | |
---|
889 | count, i.e. we have extended the object by prefixing it. We can provide |
---|
890 | |
---|
891 | a couple of features in an anonymous namespace (not shown) in the implementation |
---|
892 | |
---|
893 | file for for supporting the count and its access from a raw pointer:</P> |
---|
894 | |
---|
895 | |
---|
896 | |
---|
897 | <UL> |
---|
898 | |
---|
899 | <PRE><TT>struct count |
---|
900 | { |
---|
901 | size_t value; |
---|
902 | }; |
---|
903 | |
---|
904 | count *header(const void *ptr) |
---|
905 | { |
---|
906 | return const_cast<count *>(static_cast<const count *>(ptr) - 1); |
---|
907 | } |
---|
908 | </TT></PRE> |
---|
909 | |
---|
910 | </UL> |
---|
911 | |
---|
912 | |
---|
913 | |
---|
914 | <P>An important constraint to observe here is the alignment of <TT><FONT SIZE=+1>count</FONT></TT> |
---|
915 | |
---|
916 | should be such that it is suitably aligned for any type. For the definition |
---|
917 | |
---|
918 | shown this will be the case on almost all platforms. However, you may need |
---|
919 | |
---|
920 | to add a padding member for those that don't, e.g. using an anonymous <TT><FONT SIZE=+1>union</FONT></TT> |
---|
921 | |
---|
922 | to coalign <TT><FONT SIZE=+1>count</FONT></TT> and the most aligned type. |
---|
923 | |
---|
924 | Unfortunately, there is no portable way of specifying this such that the |
---|
925 | |
---|
926 | minimum alignment is also observed - this is a common problem when specifying |
---|
927 | |
---|
928 | your own allocators that do not directly use the results of either <TT><FONT SIZE=+1>new</FONT></TT> |
---|
929 | |
---|
930 | or <TT><FONT SIZE=+1>malloc</FONT></TT>.</P> |
---|
931 | |
---|
932 | |
---|
933 | |
---|
934 | <P>Again, note that the count is not considered to be a part of the logical |
---|
935 | |
---|
936 | state of the object, and hence the conversion from <TT><FONT SIZE=+1>const</FONT></TT> |
---|
937 | |
---|
938 | to non-<TT><FONT SIZE=+1>const</FONT></TT> - <TT><FONT SIZE=+1>count</FONT></TT> |
---|
939 | |
---|
940 | is in effect a <TT><FONT SIZE=+1>mutable</FONT></TT> type.</P> |
---|
941 | |
---|
942 | |
---|
943 | |
---|
944 | <P>The allocator functions themselves are fairly straightforward:</P> |
---|
945 | |
---|
946 | |
---|
947 | |
---|
948 | <UL> |
---|
949 | |
---|
950 | <PRE><TT>void *operator new(size_t size, const countable_new &) |
---|
951 | { |
---|
952 | count *allocated = static_cast<count *>(::operator new(sizeof(count) + size)); |
---|
953 | *allocated = count(); // initialise the header |
---|
954 | return allocated + 1; // adjust result to point to the body |
---|
955 | } |
---|
956 | |
---|
957 | void operator delete(void *ptr, const countable_new &) |
---|
958 | { |
---|
959 | ::operator delete(header(ptr)); |
---|
960 | } |
---|
961 | </TT></PRE> |
---|
962 | |
---|
963 | </UL> |
---|
964 | |
---|
965 | |
---|
966 | |
---|
967 | <P>Given a correctly allocated header, we now need the <I>Countable </I>functions |
---|
968 | |
---|
969 | to operate on <TT><FONT SIZE=+1>const void *</FONT></TT> to complete the |
---|
970 | |
---|
971 | picture:</P> |
---|
972 | |
---|
973 | |
---|
974 | |
---|
975 | <UL> |
---|
976 | |
---|
977 | <PRE><TT>void acquire(const void *ptr) |
---|
978 | { |
---|
979 | if(ptr) |
---|
980 | { |
---|
981 | ++header(ptr)->value; |
---|
982 | } |
---|
983 | } |
---|
984 | |
---|
985 | void release(const void *ptr) |
---|
986 | { |
---|
987 | if(ptr) |
---|
988 | { |
---|
989 | --header(ptr)->value; |
---|
990 | } |
---|
991 | } |
---|
992 | |
---|
993 | size_t acquired(const void *ptr) |
---|
994 | { |
---|
995 | return ptr ? header(ptr)->value : 0; |
---|
996 | } |
---|
997 | |
---|
998 | template<typename countable_type> |
---|
999 | void dispose(const countable_type *ptr, const void *) |
---|
1000 | { |
---|
1001 | ptr->~countable_type(); |
---|
1002 | operator delete(const_cast<countable_type *>(ptr), countable); |
---|
1003 | } |
---|
1004 | </TT></PRE> |
---|
1005 | |
---|
1006 | </UL> |
---|
1007 | |
---|
1008 | |
---|
1009 | |
---|
1010 | <P>The most complex of these is the <TT><FONT SIZE=+1>dispose</FONT></TT> |
---|
1011 | |
---|
1012 | function that must ensure that the correct type is destructed and also |
---|
1013 | |
---|
1014 | that the memory is collected from the correct offset. It uses the value |
---|
1015 | |
---|
1016 | and type of first argument to perform this correctly, and the second argument |
---|
1017 | |
---|
1018 | merely acts as a strategy selector, i.e. the use of <TT><FONT SIZE=+1>const |
---|
1019 | |
---|
1020 | void *</FONT></TT> distinguishes it from the earlier dispose shown for |
---|
1021 | |
---|
1022 | <TT><FONT SIZE=+1>const countability *</FONT></TT>.</P> |
---|
1023 | |
---|
1024 | </UL> |
---|
1025 | |
---|
1026 | |
---|
1027 | |
---|
1028 | |
---|
1029 | <HR WIDTH="100%"> |
---|
1030 | <H2>Getting smarter</H2> |
---|
1031 | |
---|
1032 | |
---|
1033 | |
---|
1034 | <UL> |
---|
1035 | |
---|
1036 | <P>Now that we have a way of adding countability at creation for objects |
---|
1037 | |
---|
1038 | of any type, what extra is needed to make this work with the <TT><FONT SIZE=+1>countable_ptr</FONT></TT> |
---|
1039 | |
---|
1040 | we defined earlier? Good news: nothing!</P> |
---|
1041 | |
---|
1042 | |
---|
1043 | |
---|
1044 | <UL> |
---|
1045 | |
---|
1046 | <PRE><TT>class example |
---|
1047 | { |
---|
1048 | ... |
---|
1049 | }; |
---|
1050 | |
---|
1051 | void simple() |
---|
1052 | { |
---|
1053 | countable_ptr<example> ptr(new(countable) example); |
---|
1054 | countable_ptr<example> qtr(ptr); |
---|
1055 | ptr.clear(); // set ptr to point to null |
---|
1056 | } // allocated object deleted when qtr destructs |
---|
1057 | </TT></PRE> |
---|
1058 | |
---|
1059 | </UL> |
---|
1060 | |
---|
1061 | |
---|
1062 | |
---|
1063 | <P>The <TT><FONT SIZE=+1>new(countable)</FONT></TT> expression defines |
---|
1064 | |
---|
1065 | a different policy for allocation and deallocation and, in common with |
---|
1066 | |
---|
1067 | other allocators, any attempt to mix your allocation policies, e.g. call |
---|
1068 | |
---|
1069 | <TT><FONT SIZE=+1>delete</FONT></TT> on an object allocated with <TT><FONT SIZE=+1>new(countable)</FONT></TT>, |
---|
1070 | |
---|
1071 | results in undefined behaviour. This is similar to what happens when you |
---|
1072 | |
---|
1073 | mix <TT><FONT SIZE=+1>new[]</FONT></TT> with <TT><FONT SIZE=+1>delete</FONT></TT> |
---|
1074 | |
---|
1075 | or <TT><FONT SIZE=+1>malloc</FONT></TT> with <TT><FONT SIZE=+1>delete</FONT></TT>. |
---|
1076 | |
---|
1077 | The whole point of <I>Countable </I>conformance is that <I>Countable </I>objects |
---|
1078 | |
---|
1079 | are used with <TT><FONT SIZE=+1>countable_ptr</FONT></TT>, and this ensures |
---|
1080 | |
---|
1081 | the correct use.</P> |
---|
1082 | |
---|
1083 | |
---|
1084 | |
---|
1085 | <P>However, accidents will happen, and inevitably you may forget to allocate |
---|
1086 | |
---|
1087 | using <TT><FONT SIZE=+1>new(countable)</FONT></TT> and instead use <TT><FONT SIZE=+1>new</FONT></TT>. |
---|
1088 | |
---|
1089 | This error and others can be detected in most cases by extending the code |
---|
1090 | |
---|
1091 | shown here to add a check member to the <TT><FONT SIZE=+1>count</FONT></TT>, |
---|
1092 | |
---|
1093 | validating the check on every access. A benefit of ensuring clear separation |
---|
1094 | |
---|
1095 | between header and implementation source files means that you can introduce |
---|
1096 | |
---|
1097 | a checking version of this allocator without having to recompile your code.</P> |
---|
1098 | |
---|
1099 | </UL> |
---|
1100 | |
---|
1101 | |
---|
1102 | |
---|
1103 | |
---|
1104 | <HR WIDTH="100%"> |
---|
1105 | <H2>Conclusion</H2> |
---|
1106 | |
---|
1107 | |
---|
1108 | |
---|
1109 | <UL> |
---|
1110 | |
---|
1111 | <P>There are two key concepts that this article has introduced:</P> |
---|
1112 | |
---|
1113 | |
---|
1114 | |
---|
1115 | <UL> |
---|
1116 | |
---|
1117 | <LI>The use of a generic requirements based approach to simplify and adapt |
---|
1118 | |
---|
1119 | the use of the C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT> pattern.</LI> |
---|
1120 | |
---|
1121 | |
---|
1122 | |
---|
1123 | <LI>The ability, through control of allocation, to dynamically and non-intrusively |
---|
1124 | |
---|
1125 | add capabilities to fixed types using the R<FONT SIZE=-1>UNTIME</FONT> |
---|
1126 | |
---|
1127 | M<FONT SIZE=-1>IXIN</FONT> pattern.</LI> |
---|
1128 | |
---|
1129 | </UL> |
---|
1130 | |
---|
1131 | |
---|
1132 | |
---|
1133 | <P>The application of the two together gives rise to a new variant of the |
---|
1134 | |
---|
1135 | essential C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT> pattern, |
---|
1136 | |
---|
1137 | U<FONT SIZE=-1>NINTRUSIVE</FONT> C<FONT SIZE=-1>OUNTED</FONT> B<FONT SIZE=-1>ODY</FONT>. |
---|
1138 | |
---|
1139 | You can take this theme even further and contrive a simple garbage collection |
---|
1140 | |
---|
1141 | system for C++.</P> |
---|
1142 | |
---|
1143 | |
---|
1144 | |
---|
1145 | <P>The complete code for <TT><FONT SIZE=+1>countable_ptr</FONT></TT>, <TT><FONT SIZE=+1>countability</FONT></TT>, |
---|
1146 | |
---|
1147 | and the <TT><FONT SIZE=+1>countable new</FONT></TT> is also available.</P> |
---|
1148 | |
---|
1149 | </UL> |
---|
1150 | |
---|
1151 | |
---|
1152 | |
---|
1153 | <DIV ALIGN=right><P> |
---|
1154 | |
---|
1155 | <HR WIDTH="100%"><FONT SIZE=-1><I>First published in </I><a href="http://www.accu.org/c++sig/public/Overload.html">Overload</a> <I>25, |
---|
1156 | |
---|
1157 | April 1998, ISSN 1354-3172<BR> |
---|
1158 | |
---|
1159 | © Copyright Kevlin Henney, 1998, 1999</I></FONT></DIV> |
---|
1160 | |
---|
1161 | |
---|
1162 | |
---|
1163 | </BODY> |
---|
1164 | |
---|
1165 | </HTML> |
---|
1166 | |
---|