Shared Memory Allocator in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 2
Reputation: juanbuffer is an unknown quantity at this point 
Solved Threads: 0
juanbuffer juanbuffer is offline Offline
Newbie Poster

Shared Memory Allocator in C++

 
0
  #1
Feb 22nd, 2008
Hi everybody,

I'm developing a daemon in c/c++ and I'm having some trouble with shared memory. I create different processes that have to access the same memory region. Everything was OK until I decided to use a vector containing objects. I read that I have to define an allocator specifing how to reserve memory. OK that what I'm trying to do. I have defined my allocator, allocate, and deallocate functions work perfectly but I have problems with the construct function, it simply raises a SIGSEGV.

Is my first time using shared memory and I'm really discouraged. Suggestions and comments are always welcome. Thanks in advance.

  1. #ifndef _NEW_MMAPALLOCATOR_H
  2. #define _NEW_MMAPALLOCATOR_H 1
  3.  
  4. extern "C"
  5. {
  6. #include <sys/types.h>
  7. #include <sys/mman.h>
  8. }
  9.  
  10. #include <limits>
  11. #include <vector>
  12. #include <iostream>
  13. #include <iterator>
  14.  
  15. namespace leimyalloc {
  16. template <typename T>
  17. class mmap_allocator {
  18. public:
  19. typedef size_t size_type;
  20. typedef ptrdiff_t difference_type;
  21. typedef T* pointer;
  22. typedef T const * const_pointer;
  23. typedef T& reference;
  24. typedef T const & const_reference;
  25. typedef T value_type;
  26.  
  27. //rebind
  28. template <typename U>
  29. struct rebind {
  30. typedef mmap_allocator <U> other;
  31. };
  32.  
  33. pointer address (reference value ) const {
  34. return &value;
  35. }
  36.  
  37. const_pointer address (const_reference value) const {
  38. return &value;
  39. }
  40.  
  41. mmap_allocator () throw () {}
  42.  
  43. mmap_allocator ( mmap_allocator const &) throw () {}
  44.  
  45. template <typename U>
  46. mmap_allocator (mmap_allocator <U> const &) throw () {}
  47.  
  48. ~mmap_allocator() throw () {}
  49.  
  50. size_type max_size () const throw() {
  51. return ::std::numeric_limits <size_type>::max() / sizeof(T);
  52. }
  53.  
  54. pointer allocate (size_type num, void * hint = 0) {
  55. int fd=open("/dev/zero",O_RDWR);
  56. if(fd==-1){
  57. cerr << "Cannot open /dev/zero" << endl;
  58. }
  59. pointer p = (pointer) ::mmap(0, sizeof(pointer), PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0);
  60. int * val = (int *)p;
  61. if(val && *val == -1)
  62. p = NULL;
  63.  
  64. return p;
  65. }
  66.  
  67. void construct (pointer p, const_reference value) {
  68. new((void *)p) T(value); //placement new
  69. }
  70.  
  71. void destroy (pointer p) {
  72. p->~T();
  73. }
  74.  
  75. void deallocate (pointer p, size_type num) {
  76. ::munmap((caddr_t) p, num);
  77. }
  78.  
  79. };
  80.  
  81. template <typename T1, typename T2>
  82. bool operator == (mmap_allocator <T1> const &, mmap_allocator <T2> const &) throw () {
  83. return true;
  84. }
  85.  
  86. template <typename T1, typename T2>
  87. bool operator != (mmap_allocator <T1> const &, mmap_allocator <T2> const &) throw () {
  88. return false;
  89. }
  90.  
  91. }
  92. #end if
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Shared Memory Allocator in C++

 
0
  #2
Feb 22nd, 2008
> my first time using shared memory and I'm really discouraged.
it is just that the problem is (much) harder than you imagine it to be.

for this to work, a shared memory segment should be created once and when allocate is called we have to sub-allocate memory blocks out of this segment. a good place to learn about a simple way of doing it is to read the section on implementing malloc() and free() in 'The C Programming Language' (Kernighan and Ritchie).

now we encounter the first problem: when creating shared memory (and memory mapped files) to communicate between two processes, the memory segment can be mapped at a different address in each process. a shared memory segment that is just created is merely a raw chunk of memory. in order to implement the allocation mechanism, we need to store pointers inside this shared memory segment. since the normal pointer stores an absolute address, that address is only valid for the process that placed the object there (unless all processes map the mapped region in the same address). to be able to simulate pointers in mapped regions, we have to create a smart pointer that uses offsets instead of absolute addresses. (this would also be true for user-defined objects that are placed into shared memory; any pointer member must be a smart pointer. and there can be no reference members at all).

the second problem is much simpler to solve; we need to synchronize access to the shared memory structures from multiple processes.

there are several other issues to be addressed. for details, see the documentation for boost.interprocess library (still in vault) at http://igaztanaga.drivehq.com/interprocess/
Last edited by vijayan121; Feb 22nd, 2008 at 2:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: juanbuffer is an unknown quantity at this point 
Solved Threads: 0
juanbuffer juanbuffer is offline Offline
Newbie Poster

Re: Shared Memory Allocator in C++

 
0
  #3
Feb 23rd, 2008
Thank you very much for the info. I will keep on trying. And yes you're right is much more difficult than I thought. I will try to use Boost libraries, they seem simple to be used.

Cheers
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC