All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ManagedBuffer.cpp
Go to the documentation of this file.
1 /*
2  * ManagedBuffer.cpp
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 #include <HSEP/ManagedBuffer.h>
23 #include <cstring>
24 
25 using namespace std;
26 
27 namespace HSEP {
28 
29  char* ManagedBuffer::calcAddressPtr(char* pBufferPtr, int pSlotId) const {
30 
31  char* vSlotPtr =
32  pBufferPtr + // data buffer start point
33  sizeof(int)*aSlotQty + // free index queue
34  (aSlotSize * // a memory slot size
35  pSlotId); // zero based slot position
36 
37  return vSlotPtr;
38 
39  } // ManagedBuffer::calcAddress
40 
41  void ManagedBuffer::cleanSlot(char* pBufferPtr, int pSlotId) {
42  char* vSlotPtr = calcAddressPtr(pBufferPtr,pSlotId);
43  memset(vSlotPtr,0,aSlotSize);
44  } // ManagedBuffer::cleanSlot
45 
46 
47  void ManagedBuffer::pushFree(char* pSharedBufferPtr, int pSlotId) {
48 
49  // aMutex is a SharedMutex ... with GCC there is an issue with the
50  // polymorphism, because it is not recognizing the inheritance and
51  // uses the aMutex as a local variable instead of a class attribute.
52  //
53  ExclusiveScope((Mutex*)&aMutex);
54 
55  if (aLastFreeSlot != (aSlotQty-1)) {
56  aLastFreeSlot++;
57  int* vQueueHeadPtr = (int*)pSharedBufferPtr;
58  vQueueHeadPtr[aLastFreeSlot] = pSlotId;
59  }
60 
61  } // ManagedBuffer::pushFree
62 
63  int ManagedBuffer::popFree(char* pSharedBufferPtr) {
64 
65  ExclusiveScope((Mutex*)&aMutex);
66 
67  int vResult = -1;
68 
69  if (aLastFreeSlot >= 0) {
70  int* vQueueHeadPtr = (int*)pSharedBufferPtr;
71  vResult = vQueueHeadPtr[aLastFreeSlot];
72  aLastFreeSlot--;
73  }
74 
75  return vResult;
76 
77  } // ManagedBuffer::popFree
78 
79  const char* ManagedBuffer::dataPtr(char* pSharedBufferPtr) const {
80 
81  return pSharedBufferPtr+(aSlotQty*sizeof(int));
82 
83  } // ManagedBuffer::data
84 
85  size_t ManagedBuffer::dataSize() const {
86 
87  return (aSlotQty*sizeof(int) + (aSlotQty*aSlotSize));
88 
89  } // ManagedBuffer::dataSize
90 
91 
92  int ManagedBuffer::wait() {
93 
94  int vResult;
95 
96  aGate.wait();
97  vResult = aSignaledSlot;
98 
99  return vResult;
100 
101  } // ManagedBuffer::wait
102 
103 
104  void ManagedBuffer::signal(int pSlotId) {
105 
106  aSignaledSlot = pSlotId;
107  aGate.signal();
108 
109  } // ManagedBuffer::signal
110 
111 } // HSEP namespace
112 
113 
114