A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
STLSlabAllocator.h
Go to the documentation of this file.
1 /*
2  * File: STLSlabAllocator.h
3  * Author: Jia
4  * May 20, 2016, 10:57
5  */
6 
7 #ifndef STLSLABALLOCATOR_H
8 #define STLSLABALLOCATOR_H
9 
10 #include <cstddef>
11 #include "SlabAllocator.h"
12 #include <iostream>
13 
14 template <class T>
16 public:
17  typedef size_t size_type;
18  typedef T* pointer;
19  typedef T value_type;
20  typedef ptrdiff_t difference_type;
21  typedef const T* const_pointer;
22  typedef T& reference;
23  typedef const T& const_reference;
24 
25  STLSlabAllocator(size_t size) {
26  allocator = make_shared<SlabAllocator>(size, true);
27  }
28  STLSlabAllocator(void* memPool, size_t size) {
29  allocator = make_shared<SlabAllocator>(memPool, size, true);
30  }
31 
32  template <class U>
34  allocator = other.getAllocator();
35  }
36  template <class U>
37  struct rebind {
39  };
40  pointer allocate(size_type n, const void* /*hint*/ = 0) {
41  void* retPtr = allocator->do_slabs_alloc(n * sizeof(T));
42  // if(n*sizeof(T)!=24) {
43  // cout << "Size to allocate:"<< n*sizeof(T)<<"\n";
44  //}
45  if (retPtr == nullptr) {
46  std::bad_alloc exception;
47  cout << "Can't allocate more from the page! TODO: add a new page!\n";
48  throw exception;
49  }
50  return (pointer)retPtr;
51  }
53  // cout << "Size to deallocate:"<< n*sizeof(T)<<"\n";
54  return allocator->do_slabs_free(p, n * sizeof(T));
55  }
56  void construct(pointer p, const T& val) {
57  new (p) T(val);
58  }
59  void destroy(pointer p) {
60  p->~T();
61  }
63  return allocator;
64  }
65 
66 private:
68 };
69 
70 template <class T, class U>
71 bool operator==(const STLSlabAllocator<T>& left, const STLSlabAllocator<U>& right) {
72  if (left.getAllocator() == right.getAllocator()) {
73  return true;
74  } else {
75  return false;
76  }
77 }
78 template <class T, class U>
79 bool operator!=(const STLSlabAllocator<T>& left, const STLSlabAllocator<U>& right) {
80  if (left.getAllocator() != right.getAllocator()) {
81  return true;
82  } else {
83  return false;
84  }
85 }
86 
87 #endif
STLSlabAllocator(const STLSlabAllocator< U > &other)
shared_ptr< SlabAllocator > SlabAllocatorPtr
Definition: SlabAllocator.h:12
pointer allocate(size_type n, const void *=0)
SlabAllocatorPtr allocator
void deallocate(pointer p, size_type n)
void construct(pointer p, const T &val)
const T * const_pointer
ptrdiff_t difference_type
STLSlabAllocator< U > other
SlabAllocatorPtr getAllocator() const
bool operator!=(const STLSlabAllocator< T > &left, const STLSlabAllocator< U > &right)
const T & const_reference
bool operator==(const STLSlabAllocator< T > &left, const STLSlabAllocator< U > &right)
void destroy(pointer p)
STLSlabAllocator(size_t size)
STLSlabAllocator(void *memPool, size_t size)