All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ManagedBuffer.h
Go to the documentation of this file.
1 /*
2  * ManagedBuffer.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 MANAGEDBUFFER_H_
23 #define MANAGEDBUFFER_H_
24 
25 #include <HSEP/Mutex.h>
26 #include <HSEP/Gate.h>
27 
28 namespace HSEP {
29 
36  class ManagedBuffer {
37 
38  SharedMutex aMutex;
39  SharedGate aGate;
40  size_t aSlotSize;
41  int aSlotQty;
42  int aLastFreeSlot;
43 
44  int aSignaledSlot;
45 
46  public:
47 
57  ManagedBuffer(size_t pSlotSize, int pSlotQty) {
58  aSlotSize = pSlotSize;
59  aSlotQty = pSlotQty;
60  aLastFreeSlot = pSlotQty-1;
61  aSignaledSlot = -1;
62  }
64 
73  char* calcAddressPtr(char* pBufferPtr, int pSlotId) const;
74 
80  void cleanSlot(char* pBufferPtr, int pSlotId);
81 
87  void pushFree(char* pSharedBufferPtr, int pSlotId);
88 
97  int popFree (char* pSharedBufferPtr);
98 
104  const char* dataPtr (char* pSharedBufferPtr) const;
105 
110  size_t dataSize() const;
111 
116  int wait();
117 
122  void signal(int pSlotId);
123 
128  int slotQty() const {
129  return aSlotQty;
130  }
131 
136  size_t slotSize() const {
137  return aSlotSize;
138  }
139 
140  }; // ManagedBuffer class
141 
142 } // HSEP namespace
143 
144 #endif /* MANAGEDBUFFER_H_ */