34 #include <sys/types.h>
44 int XrdOucBuffPool::alignit = sysconf(_SC_PAGESIZE);
54 int minh,
int maxh,
int rate)
56 int keep, pct, i, n = 0;
60 while(minsz > 1024*(1<<n)) n++;
62 else if (n && minsz < 1024*(1<<n)) n--;
66 if (maxh < 0) maxh = 0;
67 if (minh < 0) minh = 0;
68 if (maxh < minh) maxh = minh;
69 if (rate < 0) rate = 0;
73 if (!(slots = maxsz / incBsz)) slots = 1;
74 else if (maxsz % incBsz) slots++;
75 maxBsz = slots << shfBsz;
79 bSlot =
new BuffSlot[(
unsigned int)slots];
84 for (i = 0; i < slots; i++)
85 {bSlot[i].size = n; n += incBsz;
86 pct = (slots - i + 1)*100/slots;
87 if (pct >= 100) keep = maxh;
88 else {keep = ((maxh * pct) + 55)/100 - i*rate;
89 if (keep > maxh) keep = maxh;
90 else if (keep < minh) keep = minh;
92 bSlot[i].maxbuff = keep;
102 XrdOucBuffPool::BuffSlot *sP;
108 snum = (bsz <= incBsz ? 0 : (bsz + rndBsz) >> shfBsz);
109 if (snum >= slots)
return 0;
114 sP->SlotMutex.Lock();
118 if ((bP = sP->buffFree))
119 {sP->buffFree = bP->buffNext;
125 if (sP->size >= alignit) mema = alignit;
126 else if (sP->size > 2048) mema = 4096;
127 else if (sP->size > 1024) mema = 2048;
129 if (posix_memalign((
void **)&(bP->data), mema, sP->size))
136 sP->SlotMutex.UnLock();
150 XrdOucBuffPool::BuffSlot::~BuffSlot()
154 while((bP = buffFree)) {buffFree = buffFree->buffNext;
delete bP;}
161 void XrdOucBuffPool::BuffSlot::Recycle(
XrdOucBuffer *bP)
166 if (numbuff >= maxbuff) {
delete bP;
return;}
173 bP->buffNext = buffFree;
198 buffPool = &nullPool;
212 newsz = (
trim ? doff+dlen : size);
216 if (!(newbP = buffPool->Alloc(newsz)))
return 0;
222 memcpy(newbP->data, data, dlen+doff);
236 if (xsz <= 0) xsz = size;
240 if (!(newbP = buffPool->Alloc(xsz)))
return 0;
263 if (!(newbP =
Highjack(newsz)))
return false;
void trim(std::string &str)
XrdOucBuffer * Alloc(int sz)
XrdOucBuffPool(int minsz=4096, int maxsz=65536, int minh=1, int maxh=16, int rate=1)
friend class XrdOucBuffer
XrdOucBuffer(char *buff, int blen)
XrdOucBuffer * Clone(bool trim=true)
void Recycle()
Recycle the buffer. The buffer may be reused in the future.
XrdOucBuffer * Highjack(int bPsz=0)