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