| 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 | #ifndef UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_ | ||
| 33 | #define UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_ | ||
| 34 | |||
| 35 | #include <sparsehash/internal/sparseconfig.h> | ||
| 36 | #include <stdlib.h> // for malloc/realloc/free | ||
| 37 | #include <stddef.h> // for ptrdiff_t | ||
| 38 | #include <new> // for placement new | ||
| 39 | |||
| 40 | _START_GOOGLE_NAMESPACE_ | ||
| 41 | |||
| 42 | template<class T> | ||
| 43 | class libc_allocator_with_realloc { | ||
| 44 | public: | ||
| 45 | typedef T value_type; | ||
| 46 | typedef size_t size_type; | ||
| 47 | typedef ptrdiff_t difference_type; | ||
| 48 | |||
| 49 | typedef T* pointer; | ||
| 50 | typedef const T* const_pointer; | ||
| 51 | typedef T& reference; | ||
| 52 | typedef const T& const_reference; | ||
| 53 | |||
| 54 | 0 | libc_allocator_with_realloc() {} | |
| 55 | 0 | libc_allocator_with_realloc(const libc_allocator_with_realloc&) {} | |
| 56 | 0 | ~libc_allocator_with_realloc() {} | |
| 57 | |||
| 58 | pointer address(reference r) const { return &r; } | ||
| 59 | const_pointer address(const_reference r) const { return &r; } | ||
| 60 | |||
| 61 | pointer allocate(size_type n, const_pointer = 0) { | ||
| 62 | return static_cast<pointer>(malloc(n * sizeof(value_type))); | ||
| 63 | } | ||
| 64 | 0 | void deallocate(pointer p, size_type) { | |
| 65 | 0 | free(p); | |
| 66 | 0 | } | |
| 67 | pointer reallocate(pointer p, size_type n) { | ||
| 68 | return static_cast<pointer>(realloc(p, n * sizeof(value_type))); | ||
| 69 | } | ||
| 70 | |||
| 71 | size_type max_size() const { | ||
| 72 | return static_cast<size_type>(-1) / sizeof(value_type); | ||
| 73 | } | ||
| 74 | |||
| 75 | void construct(pointer p, const value_type& val) { | ||
| 76 | new(p) value_type(val); | ||
| 77 | } | ||
| 78 | void destroy(pointer p) { p->~value_type(); } | ||
| 79 | |||
| 80 | template <class U> | ||
| 81 | libc_allocator_with_realloc(const libc_allocator_with_realloc<U>&) {} | ||
| 82 | |||
| 83 | template<class U> | ||
| 84 | struct rebind { | ||
| 85 | typedef libc_allocator_with_realloc<U> other; | ||
| 86 | }; | ||
| 87 | }; | ||
| 88 | |||
| 89 | // libc_allocator_with_realloc<void> specialization. | ||
| 90 | template<> | ||
| 91 | class libc_allocator_with_realloc<void> { | ||
| 92 | public: | ||
| 93 | typedef void value_type; | ||
| 94 | typedef size_t size_type; | ||
| 95 | typedef ptrdiff_t difference_type; | ||
| 96 | typedef void* pointer; | ||
| 97 | typedef const void* const_pointer; | ||
| 98 | |||
| 99 | template<class U> | ||
| 100 | struct rebind { | ||
| 101 | typedef libc_allocator_with_realloc<U> other; | ||
| 102 | }; | ||
| 103 | }; | ||
| 104 | |||
| 105 | template<class T> | ||
| 106 | inline bool operator==(const libc_allocator_with_realloc<T>&, | ||
| 107 | const libc_allocator_with_realloc<T>&) { | ||
| 108 | return true; | ||
| 109 | } | ||
| 110 | |||
| 111 | template<class T> | ||
| 112 | inline bool operator!=(const libc_allocator_with_realloc<T>&, | ||
| 113 | const libc_allocator_with_realloc<T>&) { | ||
| 114 | return false; | ||
| 115 | } | ||
| 116 | |||
| 117 | _END_GOOGLE_NAMESPACE_ | ||
| 118 | |||
| 119 | #endif // UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_ |