All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SharedMemory.h
Go to the documentation of this file.
1 /*
2  * SharedMemory.h
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef SHAREDMEMORY_H_
23 #define SHAREDMEMORY_H_
24 
25 #include <HSEP/Mutex.h>
26 #include <HSEP/Gate.h>
27 #include <HSEP/ErrorCode.h>
28 #include <HSEP/ManagedBuffer.h>
29 #include <HSEP/HSEPObject.h>
30 
31 #include <mutex>
32 #include <string>
33 
34 using namespace std;
35 
36 namespace HSEP {
37 
74  class SharedMemory : public HSEPObject {
75 
76  bool aOwner;
77  bool aInitialized;
78  string aHeaderName;
79  string aBufferName;
80 
81  int aSharedHeaderHandle;
82  int aSharedBufferHandle;
83 
84  ManagedBuffer* aSharedHeaderPtr;
85  char* aSharedBufferPtr;
86 
87  private:
88 
89  char* calcAddressPtr(int pSlotId) const;
90  void cleanSlot (int pSlotId);
91 
92  public:
93 
98  SharedMemory(const string& pNameRef);
99 
108  SharedMemory(const string& pNameRef, size_t pSlotSize, int pSlotQty);
109 
114  virtual ~SharedMemory();
115 
120  int obtainSlot();
121 
129  void releaseSlot(int pSlotId);
130 
147  ErrorCode write(int pSlotId, char* pSourcePtr ,size_t pBufferSize);
148  ErrorCode read (int pSlotId, char* pDestinationPtr,size_t pBufferSize);
149 
150 
155  int wait();
156 
163  void signal(int pSlotId);
164 
165  }; // SharedMemory class
166 
167 } // HSEP namespace
168 
169 #endif /* SHAREDMEMORY_H_ */