Eneboo - Documentación para desarrolladores
|
00001 /* -*- C++ -*- */ 00002 00003 /* 00004 00005 Heap Layers: An Extensible Memory Allocation Infrastructure 00006 00007 Copyright (C) 2000-2005 by Emery Berger 00008 http://www.cs.umass.edu/~emery 00009 emery@cs.umass.edu 00010 00011 This program is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 This program is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU General Public License for more details. 00020 00021 You should have received a copy of the GNU General Public License 00022 along with this program; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 */ 00026 00027 #ifndef _STLALLOCATOR_H_ 00028 #define _STLALLOCATOR_H_ 00029 00030 #include <cstdio> 00031 #include <new> 00032 #include <cstdlib> 00033 00034 #include <memory> // STL 00035 00036 using namespace std; 00037 00052 namespace HL { 00053 00054 template <class T, class Super> 00055 class STLAllocator : public Super { 00056 public: 00057 00058 typedef T value_type; 00059 typedef std::size_t size_type; 00060 typedef std::ptrdiff_t difference_type; 00061 typedef T * pointer; 00062 typedef const T * const_pointer; 00063 typedef T& reference; 00064 typedef const T& const_reference; 00065 00066 STLAllocator (void) {} 00067 virtual ~STLAllocator (void) {} 00068 00069 STLAllocator (const STLAllocator& s) 00070 : Super (s) 00071 {} 00072 00073 #if defined(_WIN32) 00074 char * _Charalloc (size_type n) { 00075 return (char *) allocate (n); 00076 } 00077 #endif 00078 00079 #if defined(__SUNPRO_CC) 00080 inline void * allocate (size_type n, 00081 const void * = 0) { 00082 if (n) { 00083 return reinterpret_cast<void *>(Super::malloc (sizeof(T) * n)); 00084 } else { 00085 return (void *) 0; 00086 } 00087 } 00088 #else 00089 inline pointer allocate (size_type n, 00090 const void * = 0) { 00091 if (n) { 00092 return reinterpret_cast<pointer>(Super::malloc (sizeof(T) * n)); 00093 } else { 00094 return 0; 00095 } 00096 } 00097 #endif 00098 00099 inline void deallocate (void * p, size_type) { 00100 Super::free (p); 00101 } 00102 00103 inline void deallocate (pointer p, size_type) { 00104 Super::free (p); 00105 } 00106 00107 pointer address (reference x) const { return &x; } 00108 const_pointer address (const_reference x) const { return &x; } 00109 00110 void construct (pointer p, const T& val) { new (p) T (val); } 00111 void destroy (pointer p) { p->~T(); } 00112 00114 size_type max_size(void) const 00115 { 00116 size_type n = (size_type)(-1); 00117 return (n > 0 ? n : (size_type)(n)); 00118 } 00119 00120 template <class U> STLAllocator( const STLAllocator<U, Super> &) {} 00121 template <class U> struct rebind { typedef STLAllocator<U,Super> other; }; 00122 00123 }; 00124 00125 template <typename T, class S> 00126 inline bool operator!=(const STLAllocator<T,S>& a, const STLAllocator<T,S>& b) { 00127 return (&a != &b); 00128 } 00129 00130 template <typename T, class S> 00131 inline bool operator==(const STLAllocator<T,S>& a, const STLAllocator<T,S>& b) { 00132 return (&a == &b); 00133 } 00134 00135 #if 0 00136 00137 #include <memory> // STL 00138 #include <cstdlib> 00139 00140 template <typename T, class Super> 00141 class STLAllocator : public std::allocator<T>, public Super { 00142 public: 00143 inline T * allocate (std::size_t n, 00144 const void * = 0) { 00145 if (n) { 00146 return reinterpret_cast<T *>(Super::malloc (sizeof(T) * n)); 00147 } else { 00148 return 0; 00149 } 00150 } 00151 00152 inline void deallocate (void * p, std::size_t) { 00153 Super::free (p); 00154 } 00155 00156 inline void deallocate (T * p, std::size_t) { 00157 Super::free (p); 00158 } 00159 00160 }; 00161 00162 #endif 00163 00164 } 00165 00166 00167 #endif