libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moBuckets.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moBuckets.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 *******************************************************************************/
30 
31 #include "moBuckets.h"
32 
34  m_pBuffer = NULL;
35  m_pAttachedBucket = NULL;
36  m_bEmpty = true;
37  m_lBufferSize = 0;
38 }
39 
41 
42 }
43 
44 
46  return m_Lock.Lock();
47 }
48 
50  return m_Lock.Unlock();
51 }
52 
54  return m_lBufferSize;
55 }
56 
57 
59  MOubyte *toreturn = NULL;
60  Lock();
61  toreturn = m_pBuffer;
62  Unlock();
63  return toreturn;
64 }
65 
67  Lock();
68  if(m_lBufferSize!=0 && m_pBuffer!=NULL) {
69  delete [] m_pBuffer;
70  m_lBufferSize = 0;
71  m_pBuffer = NULL;
72  }
73  m_bEmpty = true;
74  Unlock();
75 }
76 
77 void moBucket::BuildBucket( MOlong size , int setvalue ) {
78  Lock();
79  m_lBufferSize = size;
81  memset((void*)m_pBuffer,setvalue, m_lBufferSize );
82  m_bEmpty = false;
83  Unlock();
84 }
85 
86 
87 void moBucket::SetBuffer( MOlong size, MOubyte *pbuf) {
88  Lock();
89  m_lBufferSize = size;
91  memcpy((void*)m_pBuffer,(const void*) pbuf, m_lBufferSize );
92  m_bEmpty = false;
93  Unlock();
94 }
95 
96 
97 void moBucket::Copy( MOlong size , MOubyte *pbuf ) {
98  Lock();
99  if(m_pBuffer && pbuf &&(m_lBufferSize>=size) ) {
100  memcpy((void*)m_pBuffer,(const void*) pbuf, size );
101  m_bEmpty = false;
102  }
103  Unlock();
104 }
105 
106 
108  Lock();
109  if(m_pAttachedBucket==NULL)
110  m_pAttachedBucket = pbucket;
111  Unlock();
112 }
113 
115  return m_pAttachedBucket;
116 }
117 
118 
119 //================================================
120 // BUCKETS POOL
121 //================================================
122 
124  m_pRetreivedBucket = NULL;
127  m_nBuckets = 0;
128  m_lMaxBuckets = 1;
129 }
130 
132  while(RetreiveBucket()!=NULL) {
134  }
135 }
136 
137 bool
139  return( m_nBuckets == 0 );
140 }
141 
142 bool
144  return( m_nBuckets == m_lMaxBuckets );
145 }
146 
147 
148 
150 
151  if((pBucket==NULL) ||(m_nBuckets == m_lMaxBuckets) )
152  return false;
153 
154 
155  m_PoolLock.Lock();
156  if( m_nBuckets == 0) {//lista vacia
157  m_pFirstBucketToGo = pBucket;
158  m_pLastBucketToGo = pBucket;
159  } else {
160  m_pLastBucketToGo->AttachBucket( pBucket );
162  }
163  m_nBuckets++;
164  m_PoolLock.Unlock();
165  return true;
166 
167 }
168 
169 bool moBucketsPool::AddBucket( MOlong size , MOubyte *pbuf ) {//take an empty created bucket and fill it with pbuf
170 
171  moBucket* pB = NULL;
172 
173  if(!IsFull()) {
174  pB = GetEmptyBucket();
175  if(pB!=NULL) {//EMPTY TO FILL
176 
177  if(pB->GetSize() >= size)
178  pB->Copy( size, pbuf );
179  else return false;
180 
181  } else {//CREATE
182  pB = new moBucket();
183  if(pB)
184  pB->SetBuffer( size, pbuf );//create buffer and fill it
185  else return false;
186  }
187  } else return false;
188 
189  if(pB!=NULL)
190  return AddBucket(pB);
191  else
192  return false;
193 }
194 
195 moBucket* moBucketsPool::GetEmptyBucket() {//get an empty bucket to fill it
196  m_PoolLock.Lock();
197 
198  moBucket *emptyone = NULL;
199 
200  m_PoolLock.Unlock();
201  return emptyone;
202 }
203 
204 
206  m_PoolLock.Lock();
207 
208  moBucket* retreived = NULL;
209 
210  retreived = m_pFirstBucketToGo;
211  //testing, unicamente el primer cuadro...
212  //reacomodamos
213  if( m_nBuckets == 0 ) {
214  retreived = NULL;
215  } else if( m_nBuckets == 1 ) {//solo uno en la lista
216  m_pFirstBucketToGo = NULL;
217  m_pLastBucketToGo = NULL;//la lista queda anulada
218  } else if( m_nBuckets > 1 ) {
220  }
221  if(m_nBuckets>0) {
222  m_nBuckets--;
223  //caso de haber sacado un bucket, lo ponemos en la lista de los vacios...
224  /*
225  if( m_pLastEmptyBucket == NULL ) {
226  m_pFirstEmptyBucket = m_pLastEmptyBucket = retreived;
227  m_pFirstEmptyBucket->AttachBucket(NULL);
228  } else {
229  m_pLastEmptyBucket->AttachBucket(retreived);
230  m_pLastEmptyBucket = retreived;
231  } */
232  }
233 
234  m_pRetreivedBucket = retreived;
235 
236  m_PoolLock.Unlock();
237  return retreived;
238 }
239 
241  return m_pRetreivedBucket;
242 }
243 
245  if (m_pRetreivedBucket) {
247  delete m_pRetreivedBucket;
248  m_pRetreivedBucket = NULL;
249  }
250  if (m_pRetreivedBucket==NULL) { return true; }
251  else { return false; }
252 }
253 
255  m_PoolLock.Lock();
256 
257  moBucket* retreived = NULL;
258 
259  retreived = m_pFirstBucketToGo;
260 
261  m_PoolLock.Unlock();
262  return retreived;
263 }
264 
bool Unlock()
Definition: moLock.cpp:137
virtual ~moBucket()
Definition: moBuckets.cpp:40
bool DestroyRetreivedBucket()
Definition: moBuckets.cpp:244
moBucket * GetActualBucket()
Definition: moBuckets.cpp:254
moBucket * m_pLastEmptyBucket
Definition: moBuckets.h:180
moBucket * m_pFirstEmptyBucket
Definition: moBuckets.h:179
bool Unlock()
Libera el acceso al buffer interno.
Definition: moBuckets.cpp:49
moBucket * m_pFirstBucketToGo
Definition: moBuckets.h:176
moBucket * m_pLastBucketToGo
Definition: moBuckets.h:177
bool IsEmpty()
Definition: moBuckets.cpp:138
bool Lock()
Paraliza el acceso al buffer interno.
Definition: moBuckets.cpp:45
MOubyte * m_pBuffer
puntero al espacio en memoria
Definition: moBuckets.h:135
MOlong m_lBufferSize
tamaño del espacio en memoria (buffer)
Definition: moBuckets.h:129
MOlong m_lMaxBuckets
Definition: moBuckets.h:174
#define MOlong
Definition: moTypes.h:391
moBucket * m_pRetreivedBucket
Definition: moBuckets.h:171
moBucket * GetEmptyBucket()
Definition: moBuckets.cpp:195
void Copy(MOlong size, MOubyte *pbuf)
Copia al espacio de memoria los valores de otro espacio de memoria.
Definition: moBuckets.cpp:97
bool Lock()
Definition: moLock.cpp:101
void EmptyBucket()
Libera el espacio de memoria.
Definition: moBuckets.cpp:66
bool m_bEmpty
indicador si el buffer está vacío o lleno (con datos)
Definition: moBuckets.h:132
moBucket * m_pAttachedBucket
moBucket enlazado
Definition: moBuckets.h:136
moLock m_PoolLock
Definition: moBuckets.h:175
Espacio en memoria para compartir datos entre objetos.
Definition: moBuckets.h:53
MOlong GetSize()
Devuelve el tamaño en bytes asignado por el buffer.
Definition: moBuckets.cpp:53
MOubyte * GetBuffer()
Devuelve el puntero al buffer de datos.
Definition: moBuckets.cpp:58
moBucket * RetreiveBucket()
Definition: moBuckets.cpp:205
void AttachBucket(moBucket *pbucket)
Enlaza un moBucket.
Definition: moBuckets.cpp:107
void SetBuffer(MOlong size, MOubyte *pbuf)
Crea un espacio de memoria y asigna los valores desde un puntero a otro espacio de memoria...
Definition: moBuckets.cpp:87
virtual ~moBucketsPool()
Definition: moBuckets.cpp:131
moBucket * RetreivedBucket()
Definition: moBuckets.cpp:240
MOlong m_nBuckets
Definition: moBuckets.h:168
moBucket * GetAttachedBucket()
Devuelve el moBucket enlazado a este.
Definition: moBuckets.cpp:114
#define MOubyte
Definition: moTypes.h:399
moLock m_Lock
semáforo para el acceso asincrónico
Definition: moBuckets.h:134
bool AddBucket(moBucket *pBucket)
Definition: moBuckets.cpp:149
void BuildBucket(MOlong size, int setvalue)
Habilita el buffer en memoria con el valor prefijado.
Definition: moBuckets.cpp:77