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.
 
 
 
 
 
 

119 lines
2.3 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_IMPL_SHMEM_ALLOCATOR_H
  9. #define CPPCMS_IMPL_SHMEM_ALLOCATOR_H
  10. #include <assert.h>
  11. #include <string.h>
  12. #include <iostream>
  13. #include <iomanip>
  14. #include <cppcms/cstdint.h>
  15. #include <cppcms/config.h>
  16. #include "basic_allocator.h"
  17. #include "posix_util.h"
  18. #include "buddy_allocator.h"
  19. #include <cppcms/cppcms_error.h>
  20. #include <string>
  21. #include <limits>
  22. namespace cppcms {
  23. namespace impl {
  24. class shmem_control : public booster::noncopyable{
  25. public:
  26. shmem_control(size_t size) :
  27. size_(size),
  28. region_(mmap_anonymous(size)),
  29. memory_(0)
  30. {
  31. if(size < sizeof(*memory_))
  32. throw cppcms_error("shared memory size is too small");
  33. memory_ = new (region_) cppcms::impl::buddy_allocator(size_);
  34. }
  35. ~shmem_control()
  36. {
  37. memory_->~buddy_allocator();
  38. ::munmap(region_,size_);
  39. }
  40. inline size_t size()
  41. {
  42. return size_;
  43. }
  44. inline size_t available()
  45. {
  46. mutex::guard g(lock_);
  47. return memory_->total_free_memory();
  48. }
  49. inline size_t max_available()
  50. {
  51. mutex::guard g(lock_);
  52. return memory_->max_free_chunk();
  53. }
  54. inline void *malloc(size_t s)
  55. {
  56. mutex::guard g(lock_);
  57. return memory_->malloc(s);
  58. }
  59. inline void free(void *p)
  60. {
  61. mutex::guard g(lock_);
  62. return memory_->free(p);
  63. }
  64. private:
  65. mutex lock_;
  66. size_t size_;
  67. void *region_;
  68. cppcms::impl::buddy_allocator *memory_;
  69. };
  70. template<typename T,shmem_control *&mm>
  71. class shmem_allocator : public basic_allocator<shmem_allocator<T,mm>, T > {
  72. public :
  73. typedef basic_allocator<shmem_allocator<T,mm>, T > super;
  74. template<typename U>
  75. struct rebind {
  76. typedef shmem_allocator<U,mm> other;
  77. };
  78. template<typename U>
  79. shmem_allocator (const shmem_allocator< U, mm > &) :
  80. super()
  81. {
  82. }
  83. shmem_allocator (const shmem_allocator &) :
  84. super()
  85. {
  86. }
  87. shmem_allocator()
  88. {
  89. }
  90. void *malloc(size_t n) const
  91. {
  92. return mm->malloc(n);
  93. }
  94. void free(void *p) const
  95. {
  96. return mm->free(p);
  97. }
  98. };
  99. } //
  100. } //
  101. #endif