Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

BasicsAllocator.H

Go to the documentation of this file.
00001 // Copyright (C) 2003, Kenneth C. Schalk
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Sun Apr 17 22:55:47 EDT 2005 by ken@xorian.net        
00020 
00021 #ifndef _Basics_Allocator
00022 #define _Basics_Allocator
00023 
00024 #include <stddef.h>
00025 #include "Basics.H"
00026 
00027 namespace Basics
00028 {
00029   // This is an STL-style allocator that uses Basics::malloc and
00030   // Basics::free.  This makes it possible to use STL classes in a
00031   // program linked with the garbage collector.  At this time it does
00032   // not support using GC attributes (e.g. for telling the collector
00033   // that a block is pointer-free).
00034 
00035   template <class T>
00036   class Allocator {
00037   public:
00038     typedef size_t     size_type;
00039     typedef ptrdiff_t  difference_type;
00040     typedef T*       pointer;
00041     typedef const T* const_pointer;
00042     typedef T&       reference;
00043     typedef const T& const_reference;
00044     typedef T        value_type;
00045 
00046     template <class T1> struct rebind {
00047       typedef Allocator<T1> other;
00048     };
00049 
00050     Allocator()  {}
00051     template <class T1> Allocator(const Allocator<T1>&) throw() {}
00052     ~Allocator() throw() {}
00053 
00054     pointer address(reference x) const { return &x; }
00055     const_pointer address(const_reference x) const { return &x; }
00056 
00057     T* allocate(size_type n, const void* = 0) {
00058       return (T *) ::operator new(n*sizeof(T));
00059     }
00060 
00061     void deallocate(pointer p, size_type n)
00062     {
00063       ::operator delete((void *) p);
00064     }
00065 
00066     size_type max_size() const throw()
00067     { return size_t(-1) / sizeof(T); }
00068 
00069     void construct(pointer p, const T& __val) { new(p) T(__val); }
00070     void destroy(pointer p) { p->~T(); }
00071 
00072     bool  operator==(const Allocator<T>&) throw()
00073     {
00074       return true;
00075     }
00076   };
00077 }
00078 
00079 #endif

Generated on Mon May 8 00:48:28 2006 for Vesta by  doxygen 1.4.2