35 #define INVALID_OFFSET 0xFFFFFFFF
39 typedef struct s_SbmInstance
58 static U32BIT FindFreeBuff1( U32BIT *control, U32BIT total )
61 for (ndx = 0; ndx != total; ndx++, control++)
63 for (cbit = 0x80000000, i = 31; cbit; cbit >>= 1, i--)
65 if ((cbit & *control) == 0)
68 return (ndx << 5) + i;
72 return INVALID_OFFSET;
75 static U32BIT FindFreeBuff2( U32BIT *control, U32BIT total )
77 U32BIT cbit, ndx, i, bndl, bndh;
81 for (ndx = bndl; ndx != bndh; ndx++, control++)
83 for (cbit = 0x03, i = 0; cbit; cbit <<= 2, i += 2)
85 if ((cbit & *control) == 0)
88 return (ndx << 5) + i;
92 control -= bndh - bndl;
96 for (cbit = 0xC0000000, i = 30; cbit; cbit >>= 2, i -= 2)
98 if ((cbit & *control) == 0)
101 return (bndl << 5) + i;
106 for (ndx = bndh; ndx != total; ndx++, control++)
108 for (cbit = 0xC0000000, i = 30; cbit; cbit >>= 2, i -= 2)
110 if ((cbit & *control) == 0)
113 return (ndx << 5) + i;
117 return INVALID_OFFSET;
120 static U32BIT FindFreeBuff3( U32BIT *control, U32BIT total )
122 U32BIT cbit, ndx, i, bndl, bndh;
126 for (ndx = bndl; ndx != bndh; ndx++, control++)
128 for (cbit = 0x38000000, i = 27; cbit; cbit >>= 3, i -= 3)
130 if ((cbit & *control) == 0)
133 return (ndx << 5) + i;
137 control -= bndh - bndl;
141 for (cbit = 0x38000000, i = 27; cbit; cbit >>= 3, i -= 3)
143 if ((cbit & *control) == 0)
146 return (bndl << 5) + i;
151 for (ndx = bndh; ndx != total; ndx++, control++)
153 for (cbit = 0x38000000, i = 27; cbit; cbit >>= 3, i -= 3)
155 if ((cbit & *control) == 0)
158 return (ndx << 5) + i;
162 return INVALID_OFFSET;
165 static U32BIT FindFreeBuff4( U32BIT *control, U32BIT total )
167 U32BIT cbit, ndx, i, bndl, bndh;
171 for (ndx = bndl; ndx != bndh; ndx++, control++)
173 for (cbit = 0xF0000000, i = 28; cbit; cbit >>= 4, i -= 4)
175 if ((cbit & *control) == 0)
178 return (ndx << 5) + i;
182 control -= bndh - bndl;
186 for (cbit = 0xF0000000, i = 28; cbit; cbit >>= 4, i -= 4)
188 if ((cbit & *control) == 0)
191 return (bndl << 5) + i;
196 for (ndx = bndh; ndx != total; ndx++, control++)
198 for (cbit = 0xF, i = 0; cbit; cbit <<= 4, i += 4)
200 if ((cbit & *control) == 0)
203 return (ndx << 5) + i;
207 return INVALID_OFFSET;
210 static U32BIT FindFreeBuff5( U32BIT *control, U32BIT total )
212 U32BIT cbit, ndx, i, bndl, bndh;
213 bndl = (total >> 2) + (total >> 3);
214 bndh = (total >> 1) + (total >> 3);
216 for (ndx = bndl; ndx != bndh; ndx++, control++)
218 for (cbit = 0x3E000000, i = 25; cbit; cbit >>= 5, i -= 5)
220 if ((cbit & *control) == 0)
223 return (ndx << 5) + i;
227 control -= bndh - bndl;
231 for (cbit = 0x3E000000, i = 25; cbit; cbit >>= 5, i -= 5)
233 if ((cbit & *control) == 0)
236 return (bndl << 5) + i;
241 for (ndx = bndh; ndx != total; ndx++, control++)
243 for (cbit = 0x3E000000, i = 25; cbit; cbit >>= 5, i -= 5)
245 if ((cbit & *control) == 0)
248 return (ndx << 5) + i;
252 return INVALID_OFFSET;
255 static U32BIT FindFreeBuff6( U32BIT *control, U32BIT total )
257 U32BIT cbit, ndx, i, bndl, bndh;
259 bndh = (total >> 1) + (total >> 2);
261 for (ndx = bndl; ndx != bndh; ndx++, control++)
263 for (cbit = 0x3F000000, i = 24; cbit; cbit >>= 6, i -= 6)
265 if ((cbit & *control) == 0)
268 return (ndx << 5) + i;
272 control -= bndh - bndl;
276 for (cbit = 0x3F000000, i = 24; cbit; cbit >>= 6, i -= 6)
278 if ((cbit & *control) == 0)
281 return (bndl << 5) + i;
286 for (ndx = bndh; ndx != total; ndx++, control++)
288 for (cbit = 0xFC, i = 2; cbit; cbit <<= 6, i += 6)
290 if ((cbit & *control) == 0)
293 return (ndx << 5) + i;
297 return INVALID_OFFSET;
300 static U32BIT FindFreeBuff7( U32BIT *control, U32BIT total )
302 U32BIT cbit, ndx, i, bndl;
303 bndl = (total >> 1) + (total >> 3);
305 for (ndx = bndl; ndx != total; ndx++, control++)
307 for (cbit = 0xFE00000, i = 21; cbit; cbit >>= 7, i -= 7)
309 if ((cbit & *control) == 0)
312 return (ndx << 5) + i;
316 control -= total - bndl;
320 for (cbit = 0x7F0, i = 4; cbit; cbit <<= 7, i += 7)
322 if ((cbit & *control) == 0)
325 return (bndl << 5) + i;
329 return INVALID_OFFSET;
332 static U32BIT FindFreeBuff8( U32BIT *control, U32BIT total )
339 if (*control != 0xFFFFFFFF)
341 for (cbit = 0xFF, i = 0; cbit; cbit <<= 8, i += 8)
343 if ((cbit & *control) == 0)
346 return (total << 5) + i;
351 return INVALID_OFFSET;
375 sz = (size1 >> 1) & 0x7;
376 sbm->lock(sbm->mtx_sem);
380 offset = FindFreeBuff1( sbm->control, sbm->totalBuffs );
384 offset = FindFreeBuff2( sbm->control, sbm->totalBuffs );
388 offset = FindFreeBuff3( sbm->control, sbm->totalBuffs );
392 offset = FindFreeBuff4( sbm->control, sbm->totalBuffs );
396 offset = FindFreeBuff5( sbm->control, sbm->totalBuffs );
400 offset = FindFreeBuff6( sbm->control, sbm->totalBuffs );
404 offset = FindFreeBuff7( sbm->control, sbm->totalBuffs );
408 offset = FindFreeBuff8( sbm->control, sbm->totalBuffs );
410 sbm->unlock(sbm->mtx_sem);
411 if (offset == INVALID_OFFSET ||
412 offset >= (sbm->totalBuffs << 5))
419 pBuff = sbm->buffers + (offset << 9);
434 U32BIT offset, mask, shft, ndx;
437 offset = (buffer - sbm->buffers) >> 9;
438 size = (buffer[1] & 0x0F) >> 1;
440 shft = offset & 0x1f;
442 if (ndx < sbm->totalBuffs &&
445 mask = (1 << (size + 1)) - 1;
446 sbm->lock(sbm->mtx_sem);
447 sbm->control[ndx] &= ~(mask << shft);
448 sbm->unlock(sbm->mtx_sem);
459 S_SbmInstance *sbm = NULL;
460 U32BIT totalBuffs, csize;
465 if (pSetup->memAlloc != NULL &&
466 pSetup->memFree != NULL &&
467 pSetup->lock != NULL &&
468 pSetup->unlock != NULL &&
469 pSetup->mtx_sem != NULL &&
470 pSetup->bufferSize != 0)
473 totalBuffs = pSetup->bufferSize;
476 buffs = pSetup->memAlloc( totalBuffs << 16 );
487 csize = totalBuffs *
sizeof(U32BIT);
488 sbm = pSetup->memAlloc(
sizeof(S_SbmInstance) + csize );
491 pSetup->memFree( buffs );
495 sbm->memFree = pSetup->memFree;
496 sbm->lock = pSetup->lock;
497 sbm->unlock = pSetup->unlock;
498 sbm->mtx_sem = pSetup->mtx_sem;
499 sbm->totalBuffs = totalBuffs;
500 sbm->buffers = buffs;
501 sbm->control = (U32BIT *)(sbm + 1);
502 memset(sbm->control, 0, csize);
524 control = sbm->control;
525 sbm->lock(sbm->mtx_sem);
526 for (count = 0; count != sbm->totalBuffs; count++, control++)
534 sbm->unlock(sbm->mtx_sem);
538 *pBufMtx = sbm->mtx_sem;
540 sbm->memFree( sbm->buffers );
Interface to platform debug functions.
void STB_SPDebugWrite(const char *format,...)
Write debug string to serial/debug port. <CR><LF> characters will be automatically added to the end o...
Section Buffer Manager (for use by Section Filter Manager and client) Provides mechanism to allocate ...
H_SbmInstance SBM_CreateInstance(S_SbmSetup *pSetup)
Create Section Buffer Manager instance, using setup structure.
U8BIT * SBM_AllocateBuffer(H_SbmInstance sbm, U8BIT size1)
Allocate buffer for a DVB section.
void SBM_DestroyInstance(H_SbmInstance sbm, void **pBufMtx)
Destroy Section Filter Manager instance, and return mutexes so that they may be destroyed by client...
void SBM_ReleaseBuffer(H_SbmInstance sbm, U8BIT *buffer)
Release DVB section buffer allocated with SBM_AllocateBuffer.