auto classes and finalizers

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Fri Apr 14 08:50:03 PDT 2006


Bruno Medeiros wrote:
> Sean Kelly wrote:
>> Bruno Medeiros wrote:
>>>
>>> What we want is an allocator that allocates memory that is not to be 
>>> claimed by the GC (but which is to be scanned by the GC). It's 
>>> behavior is exactly like the allocator of 
>>> http://www.digitalmars.com/d/memory.html#newdelete but it should come 
>>> with the language and be available for all types. With usage like:
>>>
>>>   class LinkedList {
>>>     ...
>>>     Add(Object obj) {
>>>       Node node = mnew Node(blabla);
>>>       ...
>>>     }
>>>
>>> Thus, when the destructor is called upon a LinkedList, either 
>>> explicitly, or by the GC, the Node references will always be valid. 
>>> One has to be careful now, as mnew'ed object are effectively under 
>>> manual memory management, and so every mnew must have a corresponding 
>>> delete, lest there be dangling pointer ou memory leaks. Nonetheless 
>>> it seems to be only sane solution to this problem.
>>
>> This does seem to be the most reasonable method.  In fact, it could be 
>> done now without the addition of new keywords by adding two new GC 
>> functions: release and reclaim (bad names, but they're all I could 
>> think of).  'release' would tell the GC not to automatically finalize 
>> or delete the memory block, as you've suggested above, and 'reclaim' 
>> would transfer ownership back to the GC.  It's more error prone than 
>> I'd like, but also perhaps the most reasonable.
>>
> 
> Hum, indeed.
> 

Then again, with a proper allocator (mmnew) there is room for more 
optimization. I doubt one would want (or that it would be good) to 
change the management ownership of an instance during it's lifetime. 
Rather, it should be set right from the start (when allocated).

Also, I've realized just now, that with templates one can get a pretty 
close solution, with something like:
   mmnew!(Foobar)
The shortcoming is you won't be able to use non-default constructors in 
that call.

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list