1 /* Copyright (c) 2006, Google Inc.
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: Maxim Lifantsev
35 #ifndef BASE_STL_ALLOCATOR_H_
36 #define BASE_STL_ALLOCATOR_H_
40 #include <stddef.h> // for ptrdiff_t
43 #include "base/logging.h"
45 // Generic allocator class for STL objects
46 // that uses a given type-less allocator Alloc, which must provide:
47 // static void* Alloc::Allocate(size_t size);
48 // static void Alloc::Free(void* ptr, size_t size);
50 // STL_Allocator<T, MyAlloc> provides the same thread-safety
51 // guarantees as MyAlloc.
54 // set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set;
55 // CAVEAT: Parts of the code below are probably specific
56 // to the STL version(s) we are using.
57 // The code is simply lifted from what std::allocator<> provides.
58 template <typename T
, class Alloc
>
61 typedef size_t size_type
;
62 typedef ptrdiff_t difference_type
;
64 typedef const T
* const_pointer
;
66 typedef const T
& const_reference
;
69 template <class T1
> struct rebind
{
70 typedef STL_Allocator
<T1
, Alloc
> other
;
74 STL_Allocator(const STL_Allocator
&) { }
75 template <class T1
> STL_Allocator(const STL_Allocator
<T1
, Alloc
>&) { }
78 pointer
address(reference x
) const { return &x
; }
79 const_pointer
address(const_reference x
) const { return &x
; }
81 pointer
allocate(size_type n
, const void* = 0) {
82 RAW_DCHECK((n
* sizeof(T
)) / sizeof(T
) == n
, "n is too big to allocate");
83 return static_cast<T
*>(Alloc::Allocate(n
* sizeof(T
)));
85 void deallocate(pointer p
, size_type n
) { Alloc::Free(p
, n
* sizeof(T
)); }
87 size_type
max_size() const { return size_t(-1) / sizeof(T
); }
89 void construct(pointer p
, const T
& val
) { ::new(p
) T(val
); }
90 void construct(pointer p
) { ::new(p
) T(); }
91 void destroy(pointer p
) { p
->~T(); }
93 // There's no state, so these allocators are always equal
94 bool operator==(const STL_Allocator
&) const { return true; }
97 #endif // BASE_STL_ALLOCATOR_H_