libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo es el conjunto de objetos y funciones, que permiten ejecutar las operaciones básicas de la plataforma Moldeo, y que compone su núcleo.
moBuckets.cpp
Ir a la documentación de este archivo.
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;
125  m_pFirstBucketToGo = NULL;
126  m_pLastBucketToGo = NULL;
127  m_pFirstEmptyBucket = m_pLastEmptyBucket = NULL;
128  m_nBuckets = 0;
129  m_lMaxBuckets = 10;
130 }
131 
133  while(RetreiveBucket()!=NULL) {
134  DestroyRetreivedBucket();
135  }
136 }
137 
138 bool
140  return( m_nBuckets == 0 );
141 }
142 
143 bool
145  return( m_nBuckets == m_lMaxBuckets );
146 }
147 
148 
149 
151 
152  if((pBucket==NULL) ||(m_nBuckets == m_lMaxBuckets) )
153  return false;
154 
155 
156  m_PoolLock.Lock();
157  if( m_nBuckets == 0) {//lista vacia
158  m_pFirstBucketToGo = pBucket;
159  m_pLastBucketToGo = pBucket;
160  m_nBuckets = 1;
161  } else if (m_pLastBucketToGo) {
162  m_pLastBucketToGo->AttachBucket( pBucket );
163  m_pLastBucketToGo = pBucket;
164  m_nBuckets++;
165  }
166  m_PoolLock.Unlock();
167  return true;
168 
169 }
170 
171 bool moBucketsPool::AddBucket( MOlong size , MOubyte *pbuf ) {//take an empty created bucket and fill it with pbuf
172 
173  moBucket* pB = NULL;
174 
175  if(!IsFull()) {
176  pB = GetEmptyBucket();
177  if(pB!=NULL) {//EMPTY TO FILL
178 
179  if(pB->GetSize() >= size)
180  pB->Copy( size, pbuf );
181  else return false;
182 
183  } else {//CREATE
184  pB = new moBucket();
185  if(pB)
186  pB->SetBuffer( size, pbuf );//create buffer and fill it
187  else return false;
188  }
189  } else return false;
190 
191  if(pB!=NULL)
192  return AddBucket(pB);
193  else
194  return false;
195 }
196 
197 moBucket* moBucketsPool::GetEmptyBucket() {//get an empty bucket to fill it
198  m_PoolLock.Lock();
199 
200  moBucket *emptyone = NULL;
201 
202  m_PoolLock.Unlock();
203  return emptyone;
204 }
205 
206 
208  m_PoolLock.Lock();
209 
210  moBucket* retreived = NULL;
211 
212  retreived = m_pFirstBucketToGo;
213  //testing, unicamente el primer cuadro...
214  //reacomodamos
215  if( m_nBuckets == 0 ) {
216  retreived = NULL;
217  } else if( m_nBuckets == 1 ) {//solo uno en la lista
218  m_pFirstBucketToGo = NULL;
219  m_pLastBucketToGo = NULL;//la lista queda anulada
220  } else if( m_nBuckets > 1 ) {
221  m_pFirstBucketToGo = m_pFirstBucketToGo->GetAttachedBucket();//default
222  }
223  if(m_nBuckets>0) {
224  m_nBuckets--;
225  //caso de haber sacado un bucket, lo ponemos en la lista de los vacios...
226  /*
227  if( m_pLastEmptyBucket == NULL ) {
228  m_pFirstEmptyBucket = m_pLastEmptyBucket = retreived;
229  m_pFirstEmptyBucket->AttachBucket(NULL);
230  } else {
231  m_pLastEmptyBucket->AttachBucket(retreived);
232  m_pLastEmptyBucket = retreived;
233  } */
234  }
235 
236  m_pRetreivedBucket = retreived;
237 
238  m_PoolLock.Unlock();
239  return retreived;
240 }
241 
243  return m_pRetreivedBucket;
244 }
245 
247  if (m_pRetreivedBucket) {
248  m_pRetreivedBucket->EmptyBucket();
249  delete m_pRetreivedBucket;
250  m_pRetreivedBucket = NULL;
251  }
252  if (m_pRetreivedBucket==NULL) { return true; }
253  else { return false; }
254 }
255 
257  m_PoolLock.Lock();
258 
259  moBucket* retreived = NULL;
260 
261  retreived = m_pFirstBucketToGo;
262 
263  m_PoolLock.Unlock();
264  return retreived;
265 }
266 
bool Unlock()
Definition: moLock.cpp:137
virtual ~moBucket()
Definition: moBuckets.cpp:40
bool DestroyRetreivedBucket()
Definition: moBuckets.cpp:246
moBucket * GetActualBucket()
Definition: moBuckets.cpp:256
bool Unlock()
Libera el acceso al buffer interno.
Definition: moBuckets.cpp:49
bool IsEmpty()
Definition: moBuckets.cpp:139
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
#define MOlong
Definition: moTypes.h:391
moBucket * GetEmptyBucket()
Definition: moBuckets.cpp:197
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
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:207
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:132
moBucket * RetreivedBucket()
Definition: moBuckets.cpp:242
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:150
void BuildBucket(MOlong size, int setvalue)
Habilita el buffer en memoria con el valor prefijado.
Definition: moBuckets.cpp:77