00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 00002 * 00003 * $Id: vtkContainersAllocator.h,v 1.1 2005/07/04 06:29:44 xpxqx Exp $ 00004 * 00005 * Copyright (c) 2005 Sean McInerney 00006 * All rights reserved. 00007 * 00008 * See Copyright.txt or http://vtkcontainers.sourceforge.net/Copyright.html 00009 * for details. 00010 * 00011 * This software is distributed WITHOUT ANY WARRANTY; without even 00012 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00013 * PURPOSE. See the above copyright notice for more information. 00014 * 00015 */ 00016 #ifndef VTK_CONTAINERS_ALLOCATOR_H_ 00017 # define VTK_CONTAINERS_ALLOCATOR_H_ 00018 # include <limits> 00019 # ifdef VTK_CONTAINERS_SMALL_OBJECT_ALLOCATOR 00020 # include "vtkContainersAllocatorBase.h" 00021 # endif 00022 00023 template <class T> 00024 class vtkContainersAllocator 00025 # ifdef VTK_CONTAINERS_SMALL_OBJECT_ALLOCATOR 00026 : public vtkContainersAllocatorBase 00027 # endif 00028 { 00029 public: 00030 // type definitions 00031 typedef size_t size_type; 00032 typedef ptrdiff_t difference_type; 00033 typedef T* pointer; 00034 typedef const T* const_pointer; 00035 typedef T& reference; 00036 typedef const T& const_reference; 00037 typedef T value_type; 00038 00039 // rebind allocator to type U 00040 template <class U> 00041 struct rebind 00042 { 00043 typedef vtkContainersAllocator<U> other; 00044 }; 00045 00046 // return address of values 00047 pointer address (reference value) const 00048 { return &value; } 00049 const_pointer address (const_reference value) const 00050 { return &value; } 00051 00052 /* Constructors and destructor 00053 * -nothing to do because allocator has no state 00054 */ 00055 vtkContainersAllocator (void) throw() {} 00056 vtkContainersAllocator (const vtkContainersAllocator&) throw() {} 00057 template <class U> 00058 vtkContainersAllocator (const vtkContainersAllocator<U>&) throw() {} 00059 ~vtkContainersAllocator() throw() {} 00060 00061 // return maximum number of elements that can be allocated 00062 size_type max_size (void) const throw() 00063 { return std::numeric_limits<size_t>::max() / sizeof(T); } 00064 00065 // allocate but don't initialize num elements of type T 00066 pointer allocate (size_type num, const void* = 0) 00067 { 00068 # ifdef VTK_CONTAINERS_SMALL_OBJECT_ALLOCATOR 00069 return (pointer)_allocate(num*sizeof(T)); 00070 # else 00071 return (pointer)(::operator new(num*sizeof(T))); 00072 # endif 00073 } 00074 00075 // initialize elements of allocated storage p with value 00076 // (initialize memory with placement new) 00077 void construct (pointer p, const T& value) 00078 { new((void *)p)T(value); } 00079 00080 // destroy elements of initialized storage p 00081 // (destroy objects by calling their destructor) 00082 void destroy (pointer p) 00083 { p->~T(); } 00084 00085 // deallocate storage p of deleted items 00086 void deallocate (pointer p, size_type num) 00087 { 00088 # ifdef VTK_CONTAINERS_SMALL_OBJECT_ALLOCATOR 00089 _deallocate((void*)p, num*sizeof(T)); 00090 # else 00091 ::operator delete((void*)p); 00092 # endif 00093 } 00094 }; 00095 00096 // return that all specializations of this allocator are interchangable 00097 template <class T1, class T2> 00098 bool operator== (const vtkContainersAllocator<T1>&, 00099 const vtkContainersAllocator<T2>&) throw() 00100 { return true; } 00101 00102 template <class T1, class T2> 00103 bool operator!= (const vtkContainersAllocator<T1>&, 00104 const vtkContainersAllocator<T2>&) throw() 00105 { return false; } 00106 00107 #endif /* VTK_CONTAINERS_ALLOCATOR_H_ */ 00108 /* 00109 * End of: $Id: vtkContainersAllocator.h,v 1.1 2005/07/04 06:29:44 xpxqx Exp $. 00110 * 00111 */