00001 #ifndef __OUC_BUFF__ 00002 #define __OUC_BUFF__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d O u c B u f f e r . h h */ 00006 /* */ 00007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 #include <stdlib.h> 00034 00035 #include "XrdOuc/XrdOucChain.hh" 00036 #include "XrdSys/XrdSysPthread.hh" 00037 00038 /******************************************************************************/ 00039 /* X r d O u c B u f f P o o l */ 00040 /******************************************************************************/ 00041 00042 class XrdOucBuffer; 00043 00044 //----------------------------------------------------------------------------- 00052 //----------------------------------------------------------------------------- 00053 00054 class XrdOucBuffPool 00055 { 00056 friend class XrdOucBuffer; 00057 public: 00058 00059 //----------------------------------------------------------------------------- 00067 //----------------------------------------------------------------------------- 00068 00069 XrdOucBuffer *Alloc(int sz); 00070 00071 //----------------------------------------------------------------------------- 00075 //----------------------------------------------------------------------------- 00076 00077 inline int MaxSize() const {return maxBsz;} 00078 00079 //----------------------------------------------------------------------------- 00098 //----------------------------------------------------------------------------- 00099 00100 XrdOucBuffPool(int minsz=4096, int maxsz=65536, 00101 int minh=1, int maxh=16, 00102 int rate=1); 00103 00104 //----------------------------------------------------------------------------- 00107 //----------------------------------------------------------------------------- 00108 00109 ~XrdOucBuffPool() {delete [] bSlot;} 00110 00111 private: 00112 static int alignit; 00113 00114 struct BuffSlot 00115 {XrdSysMutex SlotMutex; 00116 XrdOucBuffer *buffFree; 00117 int size; 00118 short numbuff; 00119 short maxbuff; 00120 00121 void Recycle(XrdOucBuffer *bP); 00122 00123 BuffSlot() : buffFree(0), size(0), 00124 numbuff(0), maxbuff(0) {} 00125 ~BuffSlot(); 00126 }; 00127 00128 BuffSlot *bSlot; 00129 int incBsz; 00130 int shfBsz; 00131 int rndBsz; 00132 int maxBsz; 00133 int slots; 00134 }; 00135 00136 /******************************************************************************/ 00137 /* X r d O u c B u f f e r */ 00138 /******************************************************************************/ 00139 00140 class XrdOucBuffer 00141 { 00142 friend class XrdOucBuffPool; 00143 00144 public: 00145 00146 //----------------------------------------------------------------------------- 00150 //----------------------------------------------------------------------------- 00151 00152 inline char *Buffer() const {return data;} 00153 00154 //----------------------------------------------------------------------------- 00158 //----------------------------------------------------------------------------- 00159 00160 inline int BuffSize() const {return size;} 00161 00162 //----------------------------------------------------------------------------- 00171 //----------------------------------------------------------------------------- 00172 00173 XrdOucBuffer *Clone(bool trim=true); 00174 00175 //----------------------------------------------------------------------------- 00179 //----------------------------------------------------------------------------- 00180 00181 inline char *Data() const {return data+doff;} 00182 00183 //----------------------------------------------------------------------------- 00189 //----------------------------------------------------------------------------- 00190 00191 inline char *Data(int &dataL) const {dataL = dlen; return data+doff;} 00192 00193 //----------------------------------------------------------------------------- 00197 //----------------------------------------------------------------------------- 00198 00199 inline int DataLen() {return dlen;} 00200 00201 //----------------------------------------------------------------------------- 00212 //----------------------------------------------------------------------------- 00213 00214 XrdOucBuffer *Highjack(int bPsz=0); 00215 00216 //----------------------------------------------------------------------------- 00218 //----------------------------------------------------------------------------- 00219 00220 inline void Recycle() {buffPool->bSlot[slot].Recycle(this);} 00221 00222 //----------------------------------------------------------------------------- 00230 //----------------------------------------------------------------------------- 00231 00232 bool Resize(int newsz); 00233 00234 //----------------------------------------------------------------------------- 00239 //----------------------------------------------------------------------------- 00240 00241 inline void SetLen(int dataL, int dataO=0) {dlen = dataL; doff = dataO;} 00242 00243 //----------------------------------------------------------------------------- 00255 //----------------------------------------------------------------------------- 00256 00257 XrdOucBuffer(char *buff, int blen); 00258 00259 private: 00260 XrdOucBuffer(XrdOucBuffPool *pP, int snum) 00261 : data(0), dlen(0), doff(0), size(pP->bSlot[snum].size), 00262 slot(snum), buffPool(pP) {} 00263 00264 XrdOucBuffer() 00265 : data(0), dlen(0), doff(0), size(0), slot(0), buffPool(0) {} 00266 00267 ~XrdOucBuffer() {if (data) free(data);} 00268 00269 char *data; 00270 int dlen; 00271 int doff; 00272 int size; 00273 int slot; 00274 union{XrdOucBuffer *buffNext; 00275 XrdOucBuffPool *buffPool; 00276 }; 00277 }; 00278 #endif