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.
 
 
 
 
 
 

98 lines
2.1 KiB

  1. #ifndef CPPCMS_ALLOCATORS
  2. #define CPPCMS_ALLOCATORS
  3. #include "config.h"
  4. #ifdef CPPCMS_USE_EXTERNAL_BOOST
  5. # include <boost/interprocess/managed_external_buffer.hpp>
  6. #else
  7. # include <cppcms_boost/interprocess/managed_external_buffer.hpp>
  8. namespace boost = cppcms_boost;
  9. #endif
  10. #include "posix_util.h"
  11. #include "cppcms_error.h"
  12. #include <string>
  13. namespace cppcms {
  14. namespace impl {
  15. class shmem_control : public util::noncopyable{
  16. public:
  17. shmem_control(size_t size) :
  18. size_(size),
  19. region_(mmap_anonymous(size)),
  20. memory_(boost::interprocess::create_only,region_,size_)
  21. {
  22. }
  23. ~shmem_control()
  24. {
  25. ::munmap(region_,size_);
  26. }
  27. inline size_t available()
  28. {
  29. mutex::guard g(lock_);
  30. return memory_.get_free_memory();
  31. }
  32. inline void *malloc(size_t s)
  33. {
  34. mutex::guard g(lock_);
  35. return memory_.allocate(s);
  36. }
  37. inline void free(void *p)
  38. {
  39. mutex::guard g(lock_);
  40. return memory_.deallocate(p);
  41. }
  42. private:
  43. process_shared_mutex lock_;
  44. size_t size_;
  45. void *region_;
  46. boost::interprocess::managed_external_buffer memory_;
  47. };
  48. template<typename T,shmem_control *&mm>
  49. class shmem_allocator {
  50. public :
  51. typedef T value_type;
  52. typedef T *pointer;
  53. typedef T &reference;
  54. typedef const T &const_reference;
  55. typedef const T *const_pointer;
  56. typedef std::size_t size_type;
  57. typedef std::ptrdiff_t difference_type;
  58. template<typename U>
  59. struct rebind {
  60. typedef shmem_allocator<U,mm> other;
  61. };
  62. template<typename U> shmem_allocator (const shmem_allocator< U, mm > &a) { };
  63. shmem_allocator (const shmem_allocator &__a){ };
  64. shmem_allocator() {};
  65. inline pointer allocate(size_type cnt, std::allocator<void>::const_pointer = 0) const
  66. {
  67. return void *memory=mm->malloc(cnt*sizeof(T));
  68. if(!memory) {
  69. throw std::bad_alloc();
  70. }
  71. return (pointer)memory;
  72. };
  73. inline void deallocate(pointer p, size_type) const
  74. {
  75. mm->free(p);
  76. };
  77. inline void construct(pointer p, const T& t) const { new(p) T(t); }
  78. inline void destroy(pointer p) const { p->~T(); }
  79. inline bool operator==(shmem_allocator const&) const { return true; }
  80. inline bool operator!=(shmem_allocator const& a) const { return false; }
  81. };
  82. };
  83. #endif