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
SharedMem.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 
19 #ifndef SHAREDMEM_H
20 #define SHAREDMEM_H
21 
22 #include <pthread.h>
23 #include "PDBLogger.h"
24 #include "SlabAllocator.h"
25 
26 #ifndef USE_MEMCACHED_SLAB_ALLOCATOR
27 #include "tlsf.h"
28 #endif
29 
30 #include <memory>
31 using namespace std;
32 class SharedMem;
33 typedef shared_ptr<SharedMem> SharedMemPtr;
34 
35 
36 //this class wraps a shared memory buffer pool for allocating pages
37 //this class uses mmap system call
38 
39 class SharedMem {
40 public:
41  SharedMem(size_t shmMemSize, pdb::PDBLoggerPtr logger);
42  ~SharedMem();
43  void lock();
44  void unlock();
45  void* malloc(size_t size);
46  void* mallocAlign(size_t size, size_t alignment, int& offset);
47  void free(void* ptr, size_t size);
48  long long computeOffset(void* shmAddress);
49  void* getPointer(size_t offset);
50  static char* addressRoundUp(char* address, size_t roundTo);
51  static size_t roundUp(size_t size, size_t roundTo);
52  static size_t roundDown(size_t size, size_t roundTo);
53  void* _malloc_unsafe(size_t size);
54  void _free_unsafe(void* ptr, size_t size);
55  size_t getShmSize();
56 
57 protected:
58  int initialize();
59  void destroy();
60  int getMem();
61  int initMallocs();
62  int initMutex();
63 
64 private:
65  pthread_mutex_t* memLock;
67 #ifdef USE_MEMCACHED_SLAB_ALLOCATOR
68  SlabAllocatorPtr allocator;
69 #else
71  void* my_tlsf;
72 #endif
73  void* memPool;
74  size_t shmMemSize;
75 };
76 
77 #endif /* SHAREDMEM_H */
pdb::PDBLoggerPtr logger
Definition: SharedMem.h:66
shared_ptr< SlabAllocator > SlabAllocatorPtr
Definition: SlabAllocator.h:12
shared_ptr< SharedMem > SharedMemPtr
Definition: SharedMem.h:32
size_t shmMemSize
Definition: SharedMem.h:74
void * my_tlsf
Definition: SharedMem.h:71
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40
pthread_mutex_t * memLock
Definition: SharedMem.h:65
void * memPool
Definition: SharedMem.h:73
tlsfAllocator allocator
Definition: SharedMem.h:70