forked from grishka/libtgvoip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BufferPool.cpp
61 lines (53 loc) · 1.18 KB
/
BufferPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include "BufferPool.h"
#include "logging.h"
#include <stdlib.h>
#include <assert.h>
using namespace tgvoip;
BufferPool::BufferPool(unsigned int size, unsigned int count){
assert(count<=64);
buffers[0]=(unsigned char*) malloc(size*count);
bufferCount=count;
unsigned int i;
for(i=1;i<count;i++){
buffers[i]=buffers[0]+i*size;
}
usedBuffers=0;
this->size=size;
}
BufferPool::~BufferPool(){
free(buffers[0]);
}
unsigned char* BufferPool::Get(){
MutexGuard m(mutex);
int i;
for(i=0;i<bufferCount;i++){
if(!((usedBuffers >> i) & 1)){
usedBuffers|=(1LL << i);
return buffers[i];
}
}
return NULL;
}
void BufferPool::Reuse(unsigned char* buffer){
MutexGuard m(mutex);
int i;
for(i=0;i<bufferCount;i++){
if(buffers[i]==buffer){
usedBuffers&= ~(1LL << i);
return;
}
}
LOGE("pointer passed isn't a valid buffer from this pool");
abort();
}
size_t BufferPool::GetSingleBufferSize(){
return size;
}
size_t BufferPool::GetBufferCount(){
return (size_t) bufferCount;
}