1// Copyright (c) 2010, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// ---
31
32#pragma once
33
34#include <cstdlib> // for malloc/realloc/free
35#include <cstddef> // for ptrdiff_t
36#include <new> // for placement new
37
38namespace google {
39template <class T>
40class libc_allocator_with_realloc {
41 public:
42 typedef T value_type;
43 typedef size_t size_type;
44 typedef ptrdiff_t difference_type;
45
46 typedef T* pointer;
47 typedef const T* const_pointer;
48 typedef T& reference;
49 typedef const T& const_reference;
50
51 libc_allocator_with_realloc() {}
52 libc_allocator_with_realloc(const libc_allocator_with_realloc&) {}
53 ~libc_allocator_with_realloc() {}
54
55 pointer address(reference r) const { return &r; }
56 const_pointer address(const_reference r) const { return &r; }
57
58 pointer allocate(size_type n, const_pointer = 0) {
59 return static_cast<pointer>(malloc(n * sizeof(value_type)));
60 }
61 void deallocate(pointer p, size_type) { free(p); }
62 pointer reallocate(pointer p, size_type n) {
63 // p points to a storage array whose objects have already been destroyed
64 // cast to void* to prevent compiler warnings about calling realloc() on
65 // an object which cannot be relocated in memory
66 return static_cast<pointer>(realloc(static_cast<void*>(p), n * sizeof(value_type)));
67 }
68
69 size_type max_size() const {
70 return static_cast<size_type>(-1) / sizeof(value_type);
71 }
72
73 void construct(pointer p, const value_type& val) { new (p) value_type(val); }
74 void destroy(pointer p) { p->~value_type(); }
75
76 template <class U>
77 libc_allocator_with_realloc(const libc_allocator_with_realloc<U>&) {}
78
79 template <class U>
80 struct rebind {
81 typedef libc_allocator_with_realloc<U> other;
82 };
83};
84
85// libc_allocator_with_realloc<void> specialization.
86template <>
87class libc_allocator_with_realloc<void> {
88 public:
89 typedef void value_type;
90 typedef size_t size_type;
91 typedef ptrdiff_t difference_type;
92 typedef void* pointer;
93 typedef const void* const_pointer;
94
95 template <class U>
96 struct rebind {
97 typedef libc_allocator_with_realloc<U> other;
98 };
99};
100
101template <class T>
102inline bool operator==(const libc_allocator_with_realloc<T>&,
103 const libc_allocator_with_realloc<T>&) {
104 return true;
105}
106
107template <class T>
108inline bool operator!=(const libc_allocator_with_realloc<T>&,
109 const libc_allocator_with_realloc<T>&) {
110 return false;
111}
112
113} // namespace google
114