FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
December 01, 1995

Smart Pointers for C++ Garbage Collection

(Page 2 of 6)
December 1995/Smart Pointers for C++ Garbage Collection/Listing 1

Listing 1 Portable interface to garbage collection classes

////////////////////////////////////////////////////////////
// gc.h
//
// Smart pointers to garbage collected objects.
#ifndef GC_H
#define GC_H

#include "gc_imp.h"

// Allocator for garbage collected objects.
template<class T> struct gc {
   static void* allocate(size_t n) throw(bad_alloc) {
      void* p= ::operator new(gc_data::min()+n);
      return &(new(p) gc_value<T>())->data;
   }
   static void deallocate(T*) throw() {}
};

// Smart pointers to collectable objects.
template<class X> struct gc_ptr : gc_link {
   gc_ptr() : gc_link() {}
   gc_ptr(X* p) : gc_link(p) {}
   gc_ptr(const gc_ptr& r) : gc_link(r) {}
   gc_ptr& operator=(const gc_ptr& r) {
      mark_or_copy(r);
      return *this;
   }
   gc_ptr& operator=(X* p) {
      set_ptr(p);
      return *this;
   }
   operator X*()          const { return (X*)ptr; }
   X* operator->()        const { return (X*)ptr; }
   X& operator[](size_t i)      { return *((X*)ptr+i); }
   X operator[](size_t i) const { return *((X*)ptr+i); }
};

// Do garbage full or partial collection.
void gc_all();
void gc_min();
void gc_handler() throw(bad_alloc);

#endif

/* End of File */
Previous Page | 1 | 2 | 3 | 4 | 5 | 6 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK