stl_allocator.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef BASE_STL_ALLOCATOR_H_
  35. #define BASE_STL_ALLOCATOR_H_
  36. #include <config.h>
  37. #include <stddef.h> // for ptrdiff_t
  38. #include <limits>
  39. #include "base/logging.h"
  40. // Generic allocator class for STL objects
  41. // that uses a given type-less allocator Alloc, which must provide:
  42. // static void* Alloc::Allocate(size_t size);
  43. // static void Alloc::Free(void* ptr, size_t size);
  44. //
  45. // STL_Allocator<T, MyAlloc> provides the same thread-safety
  46. // guarantees as MyAlloc.
  47. //
  48. // Usage example:
  49. // set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set;
  50. // CAVEAT: Parts of the code below are probably specific
  51. // to the STL version(s) we are using.
  52. // The code is simply lifted from what std::allocator<> provides.
  53. template <typename T, class Alloc>
  54. class STL_Allocator {
  55. public:
  56. typedef size_t size_type;
  57. typedef ptrdiff_t difference_type;
  58. typedef T* pointer;
  59. typedef const T* const_pointer;
  60. typedef T& reference;
  61. typedef const T& const_reference;
  62. typedef T value_type;
  63. template <class T1> struct rebind {
  64. typedef STL_Allocator<T1, Alloc> other;
  65. };
  66. STL_Allocator() { }
  67. STL_Allocator(const STL_Allocator&) { }
  68. template <class T1> STL_Allocator(const STL_Allocator<T1, Alloc>&) { }
  69. ~STL_Allocator() { }
  70. pointer address(reference x) const { return &x; }
  71. const_pointer address(const_reference x) const { return &x; }
  72. pointer allocate(size_type n, const void* = 0) {
  73. RAW_DCHECK((n * sizeof(T)) / sizeof(T) == n, "n is too big to allocate");
  74. return static_cast<T*>(Alloc::Allocate(n * sizeof(T)));
  75. }
  76. void deallocate(pointer p, size_type n) { Alloc::Free(p, n * sizeof(T)); }
  77. size_type max_size() const { return size_t(-1) / sizeof(T); }
  78. void construct(pointer p, const T& val) { ::new(p) T(val); }
  79. void construct(pointer p) { ::new(p) T(); }
  80. void destroy(pointer p) { p->~T(); }
  81. // There's no state, so these allocators are always equal
  82. bool operator==(const STL_Allocator&) const { return true; }
  83. };
  84. #endif // BASE_STL_ALLOCATOR_H_