wangzhengquan
2020-07-09 91f003aac4c95f4d2a2fc0782c9bea9d484b6919
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef __SHM_ALLOCATOR_H__
#define __SHM_ALLOCATOR_H__
#include "usg_common.h"
#include "mm.h"
#include <new>
#include <cstdlib> // for exit()
#include <climits> // for UNIX_MAX
#include <cstddef>
 
 
 
template<class T> class SHMAllocator
{
public:
  typedef T               value_type;
  typedef T*              pointer;
  typedef const T*        const_pointer;
  typedef T&              reference;
  typedef const T&        const_reference;
  typedef size_t          size_type;
  typedef ptrdiff_t       difference_type;
 
 
  SHMAllocator() {};
  ~SHMAllocator() {};
  template<class U> SHMAllocator(const SHMAllocator<U>& t) { };
  template<class U> struct rebind { typedef SHMAllocator<U> other; };
 
  pointer allocate(size_type n, const void* hint=0) {
//        fprintf(stderr, "allocate n=%u, hint= %p\n",n, hint);
    return((T*) (mm_malloc(n * sizeof(T))));
  }
 
  void deallocate(pointer p, size_type n) {
//        fprintf(stderr, "dealocate n=%u" ,n);
    mm_free((void*)p);
  }
 
  void construct(pointer p, const T& value) {
    ::new(p) T(value);
  }
 
  void construct(pointer p)
  {
    ::new(p) T();
  }
 
  void destroy(pointer p) {
    p->~T();
  }
 
  pointer address(reference x) {
    return (pointer)&x;
  }
 
  const_pointer address(const_reference x) {
    return (const_pointer)&x;
  }
 
  size_type max_size() const {
    return size_type(UINT_MAX/sizeof(T));
  }
};
 
 
// template<class charT, class traits = char _traits<charT>,
// class Allocator = allocator<charT> >  
 
 
 
 
typedef std::basic_string<char, std::char_traits<char>, SHMAllocator<char> > shmstring;
 
#endif