1 // Copyright (C) 2000 Stephen Cleary 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See 4 // accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #ifndef BOOST_SYS_ALLOCATOR_H 8 #define BOOST_SYS_ALLOCATOR_H 9 10 #ifdef _MSC_VER 11 #pragma warning(push) 12 #pragma warning(disable:4100) 13 #endif 14 15 // Symbols: malloc_allocator, new_delete_allocator 16 17 #include <cstddef> 18 #include <cstdlib> 19 #include <boost/limits.hpp> 20 #include <new> 21 22 template <typename T> 23 struct malloc_allocator 24 { 25 typedef T * pointer; 26 typedef const T * const_pointer; 27 typedef T & reference; 28 typedef const T & const_reference; 29 typedef T value_type; 30 31 typedef std::size_t size_type; 32 typedef std::ptrdiff_t difference_type; 33 34 template <typename U> 35 struct rebind 36 { 37 typedef malloc_allocator<U> other; 38 }; 39 addressmalloc_allocator40 static pointer address(reference r) { return &r; } addressmalloc_allocator41 static const_pointer address(const_reference r) { return &r; } allocatemalloc_allocator42 static pointer allocate(const size_type n, const void* = 0) 43 { 44 const pointer ret = (pointer) std::malloc(n * sizeof(T)); 45 if (ret == 0) 46 throw std::bad_alloc(); 47 return ret; 48 } deallocatemalloc_allocator49 static void deallocate(const pointer p, const size_type) 50 { std::free(p); } max_sizemalloc_allocator51 static size_type max_size() { return (std::numeric_limits<size_type>::max)(); } 52 operator ==malloc_allocator53 bool operator==(const malloc_allocator &) const { return true; } operator !=malloc_allocator54 bool operator!=(const malloc_allocator &) const { return false; } 55 malloc_allocatormalloc_allocator56 malloc_allocator() { } 57 template <typename U> malloc_allocatormalloc_allocator58 malloc_allocator(const malloc_allocator<U> &) { } 59 constructmalloc_allocator60 static void construct(const pointer p, const_reference t) 61 { new ((void *) p) T(t); } destroymalloc_allocator62 static void destroy(const pointer p) 63 { p->~T(); } 64 }; 65 66 template <typename T> 67 struct new_delete_allocator 68 { 69 typedef T * pointer; 70 typedef const T * const_pointer; 71 typedef T & reference; 72 typedef const T & const_reference; 73 typedef T value_type; 74 75 typedef std::size_t size_type; 76 typedef std::ptrdiff_t difference_type; 77 78 template <typename U> 79 struct rebind 80 { 81 typedef new_delete_allocator<U> other; 82 }; 83 addressnew_delete_allocator84 static pointer address(reference r) { return &r; } addressnew_delete_allocator85 static const_pointer address(const_reference r) { return &r; } allocatenew_delete_allocator86 static pointer allocate(const size_type n, const void* = 0) 87 { return (pointer) new char[n * sizeof(T)]; } deallocatenew_delete_allocator88 static void deallocate(const pointer p, const size_type) 89 { delete [] (char*)p; } max_sizenew_delete_allocator90 static size_type max_size() { return (std::numeric_limits<size_type>::max)(); } 91 operator ==new_delete_allocator92 bool operator==(const new_delete_allocator &) const { return true; } operator !=new_delete_allocator93 bool operator!=(const new_delete_allocator &) const { return false; } 94 new_delete_allocatornew_delete_allocator95 new_delete_allocator() { } 96 template <typename U> new_delete_allocatornew_delete_allocator97 new_delete_allocator(const new_delete_allocator<U> &) { } 98 constructnew_delete_allocator99 static void construct(const pointer p, const_reference t) 100 { new ((void *) p) T(t); } destroynew_delete_allocator101 static void destroy(const pointer p) 102 { p->~T(); } 103 }; 104 105 #ifdef _MSC_VER 106 #pragma warning(pop) 107 #endif 108 109 #endif 110