| 1 |
// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 |
/* Copyright (c) 2006, Google Inc. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* * Redistributions in binary form must reproduce the above |
| 12 |
* copyright notice, this list of conditions and the following disclaimer |
| 13 |
* in the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* * Neither the name of Google Inc. nor the names of its |
| 16 |
* contributors may be used to endorse or promote products derived from |
| 17 |
* this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
* --- |
| 32 |
* Author: Maxim Lifantsev |
| 33 |
*/ |
| 34 |
|
| 35 |
|
| 36 |
#ifndef BASE_STL_ALLOCATOR_H_ |
| 37 |
#define BASE_STL_ALLOCATOR_H_ |
| 38 |
|
| 39 |
#include <config.h> |
| 40 |
|
| 41 |
#include <stddef.h> // for ptrdiff_t |
| 42 |
#include <limits> |
| 43 |
|
| 44 |
#include "base/logging.h" |
| 45 |
|
| 46 |
// Generic allocator class for STL objects |
| 47 |
// that uses a given type-less allocator Alloc, which must provide: |
| 48 |
// static void* Alloc::Allocate(size_t size); |
| 49 |
// static void Alloc::Free(void* ptr, size_t size); |
| 50 |
// |
| 51 |
// STL_Allocator<T, MyAlloc> provides the same thread-safety |
| 52 |
// guarantees as MyAlloc. |
| 53 |
// |
| 54 |
// Usage example: |
| 55 |
// set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set; |
| 56 |
// CAVEAT: Parts of the code below are probably specific |
| 57 |
// to the STL version(s) we are using. |
| 58 |
// The code is simply lifted from what std::allocator<> provides. |
| 59 |
template <typename T, class Alloc> |
| 60 |
class STL_Allocator { |
| 61 |
public: |
| 62 |
typedef size_t size_type; |
| 63 |
typedef ptrdiff_t difference_type; |
| 64 |
typedef T* pointer; |
| 65 |
typedef const T* const_pointer; |
| 66 |
typedef T& reference; |
| 67 |
typedef const T& const_reference; |
| 68 |
typedef T value_type; |
| 69 |
|
| 70 |
template <class T1> struct rebind { |
| 71 |
typedef STL_Allocator<T1, Alloc> other; |
| 72 |
}; |
| 73 |
|
| 74 |
STL_Allocator() { } |
| 75 |
STL_Allocator(const STL_Allocator&) { } |
| 76 |
template <class T1> STL_Allocator(const STL_Allocator<T1, Alloc>&) { } |
| 77 |
~STL_Allocator() { } |
| 78 |
|
| 79 |
pointer address(reference x) const { return &x; } |
| 80 |
const_pointer address(const_reference x) const { return &x; } |
| 81 |
|
| 82 |
pointer allocate(size_type n, const void* = 0) { |
| 83 |
RAW_DCHECK((n * sizeof(T)) / sizeof(T) == n, "n is too big to allocate"); |
| 84 |
return static_cast<T*>(Alloc::Allocate(n * sizeof(T))); |
| 85 |
} |
| 86 |
void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); } |
| 87 |
|
| 88 |
size_type max_size() const { return size_t(-1) / sizeof(T); } |
| 89 |
|
| 90 |
void construct(pointer p, const T& val) { ::new(p) T(val); } |
| 91 |
void construct(pointer p) { ::new(p) T(); } |
| 92 |
void destroy(pointer p) { p->~T(); } |
| 93 |
|
| 94 |
// There's no state, so these allocators are always equal |
| 95 |
bool operator==(const STL_Allocator&) const { return true; } |
| 96 |
}; |
| 97 |
|
| 98 |
#endif // BASE_STL_ALLOCATOR_H_ |