sys_allocator.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2000 Stephen Cleary
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_SYS_ALLOCATOR_H
  7. #define BOOST_SYS_ALLOCATOR_H
  8. #ifdef _MSC_VER
  9. #pragma warning(push)
  10. #pragma warning(disable:4100)
  11. #endif
  12. // Symbols: malloc_allocator, new_delete_allocator
  13. #include <cstddef>
  14. #include <cstdlib>
  15. #include <boost/limits.hpp>
  16. #include <new>
  17. template <typename T>
  18. struct malloc_allocator
  19. {
  20. typedef T * pointer;
  21. typedef const T * const_pointer;
  22. typedef T & reference;
  23. typedef const T & const_reference;
  24. typedef T value_type;
  25. typedef std::size_t size_type;
  26. typedef std::ptrdiff_t difference_type;
  27. template <typename U>
  28. struct rebind
  29. {
  30. typedef malloc_allocator<U> other;
  31. };
  32. static pointer address(reference r) { return &r; }
  33. static const_pointer address(const_reference r) { return &r; }
  34. static pointer allocate(const size_type n, const void* = 0)
  35. {
  36. const pointer ret = (pointer) std::malloc(n * sizeof(T));
  37. if (ret == 0)
  38. throw std::bad_alloc();
  39. return ret;
  40. }
  41. static void deallocate(const pointer p, const size_type)
  42. { std::free(p); }
  43. static size_type max_size() { return (std::numeric_limits<size_type>::max)(); }
  44. bool operator==(const malloc_allocator &) const { return true; }
  45. bool operator!=(const malloc_allocator &) const { return false; }
  46. malloc_allocator() { }
  47. template <typename U>
  48. malloc_allocator(const malloc_allocator<U> &) { }
  49. static void construct(const pointer p, const_reference t)
  50. { new ((void *) p) T(t); }
  51. static void destroy(const pointer p)
  52. { p->~T(); }
  53. };
  54. template <typename T>
  55. struct new_delete_allocator
  56. {
  57. typedef T * pointer;
  58. typedef const T * const_pointer;
  59. typedef T & reference;
  60. typedef const T & const_reference;
  61. typedef T value_type;
  62. typedef std::size_t size_type;
  63. typedef std::ptrdiff_t difference_type;
  64. template <typename U>
  65. struct rebind
  66. {
  67. typedef new_delete_allocator<U> other;
  68. };
  69. static pointer address(reference r) { return &r; }
  70. static const_pointer address(const_reference r) { return &r; }
  71. static pointer allocate(const size_type n, const void* = 0)
  72. { return (pointer) new char[n * sizeof(T)]; }
  73. static void deallocate(const pointer p, const size_type)
  74. { delete [] (char*)p; }
  75. static size_type max_size() { return (std::numeric_limits<size_type>::max)(); }
  76. bool operator==(const new_delete_allocator &) const { return true; }
  77. bool operator!=(const new_delete_allocator &) const { return false; }
  78. new_delete_allocator() { }
  79. template <typename U>
  80. new_delete_allocator(const new_delete_allocator<U> &) { }
  81. static void construct(const pointer p, const_reference t)
  82. { new ((void *) p) T(t); }
  83. static void destroy(const pointer p)
  84. { p->~T(); }
  85. };
  86. #ifdef _MSC_VER
  87. #pragma warning(pop)
  88. #endif
  89. #endif