home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12327 < prev    next >
Encoding:
Text File  |  1992-08-13  |  7.0 KB  |  149 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.mentorg.com!bcannard
  3. From: bcannard@hppcb36.mentorg.com (Bob Cannard @ PCB x5565)
  4. Subject: Re: Garbage Collection for C++
  5. Originator: bcannard@hppcb36
  6. Sender: news@news.mentorg.com (News User)
  7. Message-ID: <1992Aug14.021547.15215@news.mentorg.com>
  8. Date: Fri, 14 Aug 1992 02:15:47 GMT
  9. References: <1992Aug6.014619.2111@ucc.su.OZ.AU> <DAVEG.92Aug13025629@synaptx.synaptics.com>
  10. Nntp-Posting-Host: hppcb36.mentorg.com
  11. Organization: Mentor Graphics 
  12. Keywords: 
  13. Followup-To: 
  14. Lines: 133
  15.  
  16.  
  17. In article <DAVEG.92Aug13025629@synaptx.synaptics.com>, daveg@synaptics.com (Dave Gillespie) writes:
  18. |> Couldn't we get away with having a garbage collector that didn't
  19. |> need pointers to be declared `GC'?  The only thing that would need
  20. |> special `GC' treatment would be "new":  There would be a special
  21. |> "GCnew" or something which would allocate an object with the
  22. |> property that it went away when it was no longer referenced.  Note
  23. |> that it would be safe for an implementation to allocate *all*
  24. |> objects as GC-able, but it would be a good idea at least in the
  25. |> transition period to provide both traditional "new" and "GCnew".
  26.  
  27. I can't see that this is viable given the basic constraints on C++: among
  28. other things, those who don't want GC shouldn't have to suffer the hits.
  29. Marking classes or pointers (as appropriate, depending on the GC
  30. implementation) means the compiler knows about any extra treatment it has
  31. to give to those pointers/objects, and can leave it out for "ordinary"
  32. code.
  33.  
  34. In particular, in my own application - which will eventually be quite a big
  35. program - I only want GC for certain classes, where the problem of determining
  36. when an object must be deleted has no sensible solution other than GC. Yet
  37. performance (both run time and memory use) carries a high premium, so I can't
  38. afford to have GC everywhere.
  39.  
  40. |> Some Lisp systems, such as Kyoto Common Lisp, work this way.  Its
  41. |> garbage collector scans the whole stack as "raw memory," looking
  42. |> for 32-bit words that could be pointers to GC-able objects.  It
  43. |> allocates GC-able objects in such a way that it is relatively
  44. |> easy to tell if a given memory address points to a GC-able object
  45. |> or not.
  46.  
  47. Exactly what I mean. If the GC has to scan the whole stack (and all the
  48. statics and globals while we're at it), it's doing too much work. The
  49. reference-count solution is more efficient than that, because the need
  50. for GC is restricted to a small but important part of the program. The
  51. idea of languages like C++ is to give the compiler the information it
  52. needs to get the best performance out of the resulting code.
  53.  
  54. |> This may sound slow, but actually I've found KCL's garbage
  55. |> collector to be quite fast.
  56.  
  57. Quite fast compared to what? In previous projects I've been involved with
  58. (using C rather than C++) "malloc" and "free" were found to be too slow,
  59. and were replaced by custom ones. I want this GC to clip along as fast as
  60. possible.
  61.  
  62. |> The vital question is, can a scheme like this be made to work on
  63. |> all architectures on which C++ could reasonably be expected to be
  64. |> used?
  65.  
  66. If it can be made to work on one common architecture, it can probably be
  67. tweaked for all. The vital question is, would it be useable?
  68.  
  69. |> I think you'd have to live with no compaction
  70. |> at all:  Whereas it is fairly benign to mark an object because
  71. |> an integer happened to hold its address, you wouldn't want to adjust
  72. |> that integer when you moved the object!  You'd need special GC
  73. |> pointers if you wanted compaction, and those would probably not
  74. |> be practical.
  75.  
  76. This is why special GC pointers have been proposed in the first place.
  77. If GC is in use, the programmer must accept the "no clever tricks"
  78. constraint, which includes not passing pointers around disguised as
  79. integers. And once that constraint is accepted, there may be no good
  80. reason for not having compaction. My earlier post on this subject
  81. contained some suggestions that would make the combination of GC and
  82. existing libraries practical.
  83.  
  84. |> If you instead require *classes* to be declared GC, then there is no
  85. |> point in declaring pointers to be GC as well since a pointer is GC
  86. |> if, and only if, the pointed-to type is a GC class.  The problem with
  87. |> this is, suppose you have a GC class which contains a string (i.e.,
  88. |> a pointer to an old-fashioned non-GC'd array of chars).  Suppose you
  89. |> no longer have any pointers a certain object of this class, but you
  90. |> still do have a pointer to its component string.  You lose, if the
  91. |> destructor for the class deletes the string (which it pretty much has
  92. |> to do).  So a GC class could only safely contain pointers to other
  93. |> GC classes, which rules out strings unless you also have GC arrays
  94. |> of predefined types, which sounds too awkward to work.  Oh, well.
  95.  
  96. What you have described is a programming error. If a collectable object
  97. contains such an old-style pointer, it is the programmer's responsibility
  98. to ensure that the object will not be deleted as long as it is needed.
  99. This in no way prevents us from having old style pointers in GC objects:
  100. it's no different from the existing situation where a class contains a
  101. pointer to a string.
  102.  
  103. |>   GC objects can be deleted, in which case any remaining pointers
  104. |>   to the object become invalid and must not be used to dereference
  105. |>   an object.
  106.  
  107. This would accord with existing C++ philosophy, but I don't see how
  108. you could make a GC that's immune to dangling pointers. Anyone out
  109. there know otherwise?
  110.  
  111. |> What if a pointer to a GC-able object is passed to foreign (non-C++)
  112. |> code, which stashes it somewhere the C++ collector doesn't know to
  113. |> look?  (All garbage-collecting languages have this problem, and as
  114. |> far as I know all they can do is shrug and warn programmers not to
  115. |> let go of the last C++ reference to an object if they know there
  116. |> might be non-C++ references still lurking around.)
  117.  
  118. Hence the proposal for tagging GC-able objects, and having something
  119. comparable to "const" which declares "I will not do any clever tricks
  120. with this pointer, including converting it to an integer, performing
  121. pointer arithmetic on it, storing it, etc". This only needs a change
  122. to the C++ declarations of library functions, not to the functions
  123. themselves. IMO this makes it feasible to mix GC and libraries.
  124.  
  125. |> What if you have virtual base classes, and there are only references
  126. |> to the base part of an object?  The derived part of the object has
  127. |> a pointer to the base part but not the other way around, so the
  128. |> derived part might be collected in this case.  Can this ever be a
  129. |> problem?
  130.  
  131. I don't think so, but I don't know the mechanics of inheritance (and in
  132. particular MI) well enough to say. Comments from those who know the
  133. dirty details of inheritance welcome!
  134.  
  135. |> Some food for thought,
  136. |>                                 -- Dave
  137. |> --
  138. |> Dave Gillespie
  139. |>   daveg@synaptics.com, uunet!synaptx!daveg
  140. |>   or: daveg@csvax.cs.caltech.edu
  141.  
  142. Much appreciated!
  143.  
  144. Bob.
  145. -- 
  146. bob_cannard@mentorg.com         "Human beings? ... Well, I suppose they are a
  147.                                  form of life, even if they are unspeakable"
  148. Exprssed opinions are not necessarily those of Mentor Graphics Corporation.
  149.