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 | // Symbols: malloc_allocator, new_delete_allocator |
---|
11 | |
---|
12 | #include <cstddef> |
---|
13 | #include <cstdlib> |
---|
14 | #include <boost/limits.hpp> |
---|
15 | #include <new> |
---|
16 | |
---|
17 | template <typename T> |
---|
18 | struct malloc_allocator |
---|
19 | { |
---|
20 | typedef T * pointer; |
---|
21 | typedef const T * const_pointer; |
---|
22 | typedef T & reference; |
---|
23 | typedef const T & const_reference; |
---|
24 | typedef T value_type; |
---|
25 | |
---|
26 | typedef std::size_t size_type; |
---|
27 | typedef std::ptrdiff_t difference_type; |
---|
28 | |
---|
29 | template <typename U> |
---|
30 | struct rebind |
---|
31 | { |
---|
32 | typedef malloc_allocator<U> other; |
---|
33 | }; |
---|
34 | |
---|
35 | static pointer address(reference r) { return &r; } |
---|
36 | static const_pointer address(const_reference r) { return &r; } |
---|
37 | static pointer allocate(const size_type n, const pointer = 0) |
---|
38 | { |
---|
39 | const pointer ret = (pointer) std::malloc(n * sizeof(T)); |
---|
40 | if (ret == 0) |
---|
41 | throw std::bad_alloc(); |
---|
42 | return ret; |
---|
43 | } |
---|
44 | static void deallocate(const pointer p, const size_type) |
---|
45 | { std::free(p); } |
---|
46 | static size_type max_size() { return (std::numeric_limits<size_type>::max)(); } |
---|
47 | |
---|
48 | bool operator==(const malloc_allocator &) const { return true; } |
---|
49 | bool operator!=(const malloc_allocator &) const { return false; } |
---|
50 | |
---|
51 | malloc_allocator() { } |
---|
52 | template <typename U> |
---|
53 | malloc_allocator(const malloc_allocator<U> &) { } |
---|
54 | |
---|
55 | static void construct(const pointer p, const_reference t) |
---|
56 | { new ((void *) p) T(t); } |
---|
57 | static void destroy(const pointer p) |
---|
58 | { p->~T(); } |
---|
59 | }; |
---|
60 | |
---|
61 | template <typename T> |
---|
62 | struct new_delete_allocator |
---|
63 | { |
---|
64 | typedef T * pointer; |
---|
65 | typedef const T * const_pointer; |
---|
66 | typedef T & reference; |
---|
67 | typedef const T & const_reference; |
---|
68 | typedef T value_type; |
---|
69 | |
---|
70 | typedef std::size_t size_type; |
---|
71 | typedef std::ptrdiff_t difference_type; |
---|
72 | |
---|
73 | template <typename U> |
---|
74 | struct rebind |
---|
75 | { |
---|
76 | typedef new_delete_allocator<U> other; |
---|
77 | }; |
---|
78 | |
---|
79 | static pointer address(reference r) { return &r; } |
---|
80 | static const_pointer address(const_reference r) { return &r; } |
---|
81 | static pointer allocate(const size_type n, const pointer = 0) |
---|
82 | { return (pointer) new char[n * sizeof(T)]; } |
---|
83 | static void deallocate(const pointer p, const size_type) |
---|
84 | { delete [] p; } |
---|
85 | static size_type max_size() { return (std::numeric_limits<size_type>::max)(); } |
---|
86 | |
---|
87 | bool operator==(const new_delete_allocator &) const { return true; } |
---|
88 | bool operator!=(const new_delete_allocator &) const { return false; } |
---|
89 | |
---|
90 | new_delete_allocator() { } |
---|
91 | template <typename U> |
---|
92 | new_delete_allocator(const new_delete_allocator<U> &) { } |
---|
93 | |
---|
94 | static void construct(const pointer p, const_reference t) |
---|
95 | { new ((void *) p) T(t); } |
---|
96 | static void destroy(const pointer p) |
---|
97 | { p->~T(); } |
---|
98 | }; |
---|
99 | |
---|
100 | #endif |
---|