1
// Copyright (c) 2010, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
31
// Author: Guilin Chen
33
#ifndef UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_
34
#define UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_
36
#include <google/sparsehash/sparseconfig.h>
38
#include <stdlib.h> // for malloc/realloc/free
39
#include <stddef.h> // for ptrdiff_t
42
_START_GOOGLE_NAMESPACE_
45
class libc_allocator_with_realloc {
48
typedef size_t size_type;
49
typedef ptrdiff_t difference_type;
52
typedef const T* const_pointer;
54
typedef const T& const_reference;
56
libc_allocator_with_realloc() {}
57
libc_allocator_with_realloc(const libc_allocator_with_realloc&) {}
58
~libc_allocator_with_realloc() {}
60
pointer address(reference r) const { return &r; }
61
const_pointer address(const_reference r) const { return &r; }
63
pointer allocate(size_type n, const_pointer = 0) {
64
return static_cast<pointer>(malloc(n * sizeof(value_type)));
66
void deallocate(pointer p, size_type) {
69
pointer reallocate(pointer p, size_type n) {
70
return static_cast<pointer>(realloc(p, n * sizeof(value_type)));
73
size_type max_size() const {
74
return static_cast<size_type>(-1) / sizeof(value_type);
77
void construct(pointer p, const value_type& val) {
78
new(p) value_type(val);
80
void destroy(pointer p) { p->~value_type(); }
83
libc_allocator_with_realloc(const libc_allocator_with_realloc<U>&) {}
87
typedef libc_allocator_with_realloc<U> other;
91
// libc_allocator_with_realloc<void> specialization.
93
class libc_allocator_with_realloc<void> {
95
typedef void value_type;
96
typedef size_t size_type;
97
typedef ptrdiff_t difference_type;
98
typedef void* pointer;
99
typedef const void* const_pointer;
103
typedef libc_allocator_with_realloc<U> other;
108
inline bool operator==(const libc_allocator_with_realloc<T>&,
109
const libc_allocator_with_realloc<T>&) {
114
inline bool operator!=(const libc_allocator_with_realloc<T>&,
115
const libc_allocator_with_realloc<T>&) {
119
_END_GOOGLE_NAMESPACE_
121
#endif // UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_