00001 /* This file is part of Polyora, a multi-target tracking library. 00002 Copyright (C) 2010 Julien Pilet 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (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 along 00015 with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 To contact the author of this program, please send an e-mail to: 00018 julien.pilet(at)calodox.org 00019 */ 00020 #ifndef PREALLOCATED_H 00021 #define PREALLOCATED_H 00022 00023 template <class T, unsigned N> 00024 class pre_allocated_alloc { 00025 public: 00026 // type definitions 00027 typedef T value_type; 00028 typedef T* pointer; 00029 typedef const T* const_pointer; 00030 typedef T& reference; 00031 typedef const T& const_reference; 00032 typedef std::size_t size_type; 00033 typedef std::ptrdiff_t difference_type; 00034 00035 unsigned char _memory[N]; 00036 unsigned next; 00037 00038 // rebind allocator to type U 00039 template <class U> 00040 struct rebind { 00041 typedef pre_allocated_alloc<U, N> other; 00042 }; 00043 00044 // return address of values 00045 pointer address (reference value) const { 00046 return &value; 00047 } 00048 const_pointer address (const_reference value) const { 00049 return &value; 00050 } 00051 00052 pre_allocated_alloc () throw() { 00053 next=0; 00054 } 00055 pre_allocated_alloc (const pre_allocated_alloc&) throw() { 00056 next=0; 00057 } 00058 00059 template <class U> 00060 pre_allocated_alloc (const pre_allocated_alloc<U,N>&) throw() { 00061 next=0; 00062 } 00063 00064 // return maximum number of elements that can be allocated 00065 size_type max_size () const throw() { 00066 return (N-next) / sizeof(T); 00067 } 00068 00069 // allocate but don't initialize num elements of type T 00070 pointer allocate (size_type num, const void* = 0) { 00071 unsigned size = num*sizeof(T); 00072 unsigned end = next+size; 00073 if (end>N) 00074 std::__throw_bad_alloc(); 00075 next = end; 00076 00077 return (pointer)(_memory + next); 00078 } 00079 00080 // initialize elements of allocated storage p with value value 00081 void construct (pointer p, const T& value) { 00082 // initialize memory with placement new 00083 new((void*)p)T(value); 00084 } 00085 00086 // destroy elements of initialized storage p 00087 void destroy (pointer p) { 00088 // destroy objects by calling their destructor 00089 p->~T(); 00090 } 00091 00092 // deallocate storage p of deleted elements 00093 void deallocate (pointer , size_type ) { 00094 } 00095 }; 00096 00097 #endif 00098