ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

127 lines
3.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #ifndef CPPCMS_ALLOCATORS
  20. #define CPPCMS_ALLOCATORS
  21. #include "config.h"
  22. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  23. # include <boost/interprocess/managed_external_buffer.hpp>
  24. #else
  25. # include <cppcms_boost/interprocess/managed_external_buffer.hpp>
  26. namespace boost = cppcms_boost;
  27. #endif
  28. #include "posix_util.h"
  29. #include "cppcms_error.h"
  30. #include <string>
  31. namespace cppcms {
  32. namespace impl {
  33. class shmem_control : public booster::noncopyable{
  34. public:
  35. shmem_control(size_t size) :
  36. size_(size),
  37. region_(mmap_anonymous(size)),
  38. memory_(boost::interprocess::create_only,region_,size_)
  39. {
  40. }
  41. ~shmem_control()
  42. {
  43. ::munmap(region_,size_);
  44. }
  45. inline size_t size()
  46. {
  47. return size_;
  48. }
  49. inline size_t available()
  50. {
  51. mutex::guard g(lock_);
  52. return memory_.get_free_memory();
  53. }
  54. inline void *malloc(size_t s)
  55. {
  56. mutex::guard g(lock_);
  57. try {
  58. return memory_.allocate(s);
  59. }
  60. catch(boost::interprocess::bad_alloc const &/*alloc*/)
  61. {
  62. return 0;
  63. }
  64. }
  65. inline void free(void *p)
  66. {
  67. mutex::guard g(lock_);
  68. return memory_.deallocate(p);
  69. }
  70. private:
  71. mutex lock_;
  72. size_t size_;
  73. void *region_;
  74. boost::interprocess::managed_external_buffer memory_;
  75. };
  76. template<typename T,shmem_control *&mm>
  77. class shmem_allocator {
  78. public :
  79. typedef T value_type;
  80. typedef T *pointer;
  81. typedef T &reference;
  82. typedef const T &const_reference;
  83. typedef const T *const_pointer;
  84. typedef std::size_t size_type;
  85. typedef std::ptrdiff_t difference_type;
  86. template<typename U>
  87. struct rebind {
  88. typedef shmem_allocator<U,mm> other;
  89. };
  90. template<typename U> shmem_allocator (const shmem_allocator< U, mm > &a) { };
  91. shmem_allocator (const shmem_allocator &__a){ };
  92. shmem_allocator() {};
  93. inline pointer allocate(size_type cnt, std::allocator<void>::const_pointer = 0) const
  94. {
  95. void *memory=mm->malloc(cnt*sizeof(T));
  96. if(!memory) {
  97. throw std::bad_alloc();
  98. }
  99. return (pointer)memory;
  100. };
  101. inline void deallocate(pointer p, size_type) const
  102. {
  103. mm->free(p);
  104. };
  105. inline void construct(pointer p, const T& t) const { new(p) T(t); }
  106. inline void destroy(pointer p) const { p->~T(); }
  107. inline bool operator==(shmem_allocator const&) const { return true; }
  108. inline bool operator!=(shmem_allocator const& a) const { return false; }
  109. };
  110. } // impl
  111. } // cppcms
  112. #endif