Blender V2.61 - r43446

MEM_Allocator.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Peter Schlaile <peter@schlaile.de> 2005
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 #ifndef MEM_ALLOCATOR_H
00029 #define MEM_ALLOCATOR_H
00030 
00031 #include <stddef.h>
00032 #include "guardedalloc/MEM_guardedalloc.h"
00033 #include "guardedalloc/MEM_sys_types.h"
00034 
00035 template<typename _Tp>
00036 struct MEM_Allocator
00037 {
00038     typedef size_t    size_type;
00039     typedef ptrdiff_t difference_type;
00040     typedef _Tp*       pointer;
00041     typedef const _Tp* const_pointer;
00042     typedef _Tp&       reference;
00043     typedef const _Tp& const_reference;
00044     typedef _Tp        value_type;
00045 
00046     template<typename _Tp1>
00047         struct rebind { 
00048         typedef MEM_Allocator<_Tp1> other; 
00049     };
00050 
00051     MEM_Allocator() throw() {}
00052     MEM_Allocator(const MEM_Allocator&) throw() {}
00053 
00054     template<typename _Tp1>
00055         MEM_Allocator(const MEM_Allocator<_Tp1>) throw() { }
00056 
00057     ~MEM_Allocator() throw() {}
00058 
00059     pointer address(reference __x) const { return &__x; }
00060 
00061     const_pointer address(const_reference __x) const { return &__x; }
00062 
00063     // NB: __n is permitted to be 0.  The C++ standard says nothing
00064     // about what the return value is when __n == 0.
00065     _Tp* allocate(size_type __n, const void* = 0) {
00066         _Tp* __ret = 0;
00067         if (__n)
00068             __ret = static_cast<_Tp*>(
00069                 MEM_mallocN(__n * sizeof(_Tp),
00070                         "STL MEM_Allocator"));
00071         return __ret;
00072     }
00073 
00074     // __p is not permitted to be a null pointer.
00075     void deallocate(pointer __p, size_type){ 
00076         MEM_freeN(__p);
00077     }
00078 
00079     size_type max_size() const throw() { 
00080         return size_t(-1) / sizeof(_Tp); 
00081     }
00082 
00083     void construct(pointer __p, const _Tp& __val) { 
00084         new(__p) _Tp(__val); 
00085     }
00086 
00087     void destroy(pointer __p) { 
00088         __p->~_Tp(); 
00089     }
00090 };
00091 
00092 #endif // MEM_ALLOCATOR_H