![]() |
|
|||
File indexing completed on 2025-03-13 09:31:05
0001 #ifndef __OUC_BUFF__ 0002 #define __OUC_BUFF__ 0003 /******************************************************************************/ 0004 /* */ 0005 /* X r d O u c B u f f e r . h h */ 0006 /* */ 0007 /* (c) 2013 by the Board of Trustees of the Leland Stanford, Jr., University */ 0008 /* All Rights Reserved */ 0009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 0010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 0011 /* */ 0012 /* This file is part of the XRootD software suite. */ 0013 /* */ 0014 /* XRootD is free software: you can redistribute it and/or modify it under */ 0015 /* the terms of the GNU Lesser General Public License as published by the */ 0016 /* Free Software Foundation, either version 3 of the License, or (at your */ 0017 /* option) any later version. */ 0018 /* */ 0019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 0020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 0021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 0022 /* License for more details. */ 0023 /* */ 0024 /* You should have received a copy of the GNU Lesser General Public License */ 0025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 0026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 0027 /* */ 0028 /* The copyright holder's institutional names and contributor's names may not */ 0029 /* be used to endorse or promote products derived from this software without */ 0030 /* specific prior written permission of the institution or contributor. */ 0031 /******************************************************************************/ 0032 0033 #include <cstdlib> 0034 0035 #include "XrdOuc/XrdOucChain.hh" 0036 #include "XrdSys/XrdSysPthread.hh" 0037 0038 /******************************************************************************/ 0039 /* X r d O u c B u f f P o o l */ 0040 /******************************************************************************/ 0041 0042 class XrdOucBuffer; 0043 0044 //----------------------------------------------------------------------------- 0045 //! These classes allow for buffer management to minimize data copying. They 0046 //! are typically used in conjunction with the XrdOucErrInfo class. The 0047 //! XrdOucBuffPool class defines a pool of buffers and one such object must 0048 //! exist for each buffer pool (there can be many such pools). This object 0049 //! manufactures XrdOucBuffer objects. You can also create XrdOucBuffers 0050 //! without using a buffer pool (i.e. one time buffers). See the XrdOucBuffer 0051 //! constructor for details on how to do this and the associated caveats. 0052 //----------------------------------------------------------------------------- 0053 0054 class XrdOucBuffPool 0055 { 0056 friend class XrdOucBuffer; 0057 public: 0058 0059 //----------------------------------------------------------------------------- 0060 //! Allocate a buffer object. 0061 //! 0062 //! @param sz - the desired size. It os rounded up to be a multiple of 0063 //! incBsz but cannot exceed maxBsz. 0064 //! 0065 //! @return !0 - pointer to usable buffer object of suitable size. 0066 //! @return =0 - insufficient memort ro allocate a buffer. 0067 //----------------------------------------------------------------------------- 0068 0069 XrdOucBuffer *Alloc(int sz); 0070 0071 //----------------------------------------------------------------------------- 0072 //! Obtain the maximum size a buffer can have. 0073 //! 0074 //! @return The maximum size a buffer can be. 0075 //----------------------------------------------------------------------------- 0076 0077 inline int MaxSize() const {return maxBsz;} 0078 0079 //----------------------------------------------------------------------------- 0080 //! Constructor 0081 //! 0082 //! @param minsz - the minimum size a buffer can have. If it is smaller than 0083 //! 1024 it is set to 1024. The minsz is also adjusted to be 0084 //! equal to the closest smaller value of 1024*(2**n) (i.e. 1K, 0085 //! 2k, 4K, etc). If it's greater than 16MB, it is set to 16MB. 0086 //! @param maxsz - the maximum size a buffer can have and must be >= minsz. 0087 //! If it's >minsz it is rounded up to the next minsz increment. 0088 //! Buffer sizes are always allocated in minsz increments. 0089 //! @param minh - the minimum number of buffers that should be held in 0090 //! reserve when a buffer is recycled. 0091 //! @param maxh - the maximum number of buffers that should be held in 0092 //! reserve when a buffer is recycled. The value applies to the 0093 //! smallest buffer size and is progessively reduced as the 0094 //! buffer size increases. If maxh < minh it is set to minh. 0095 //! @param rate - specifies how quickly the hold vale is to be reduced as 0096 //! buffer sizes increase. A rate of 0 specifies a purely linear 0097 //! decrease. Higher values logrithmically decrease the hold. 0098 //----------------------------------------------------------------------------- 0099 0100 XrdOucBuffPool(int minsz=4096, int maxsz=65536, 0101 int minh=1, int maxh=16, 0102 int rate=1); 0103 0104 //----------------------------------------------------------------------------- 0105 //! Destructor - You must not destroy this object prior to recycling all 0106 //! oustanding buffers allocated out of this pool. 0107 //----------------------------------------------------------------------------- 0108 0109 ~XrdOucBuffPool() {delete [] bSlot;} 0110 0111 private: 0112 static int alignit; 0113 0114 struct BuffSlot 0115 {XrdSysMutex SlotMutex; 0116 XrdOucBuffer *buffFree; 0117 int size; 0118 short numbuff; 0119 short maxbuff; 0120 0121 void Recycle(XrdOucBuffer *bP); 0122 0123 BuffSlot() : buffFree(0), size(0), 0124 numbuff(0), maxbuff(0) {} 0125 ~BuffSlot(); 0126 }; 0127 0128 BuffSlot *bSlot; 0129 int incBsz; 0130 int shfBsz; 0131 int rndBsz; 0132 int maxBsz; 0133 int slots; 0134 }; 0135 0136 /******************************************************************************/ 0137 /* X r d O u c B u f f e r */ 0138 /******************************************************************************/ 0139 0140 class XrdOucBuffer 0141 { 0142 friend class XrdOucBuffPool; 0143 0144 public: 0145 0146 //----------------------------------------------------------------------------- 0147 //! Get the pointer to the buffer. 0148 //! 0149 //! @return pointer to the buffer. 0150 //----------------------------------------------------------------------------- 0151 0152 inline char *Buffer() const {return data;} 0153 0154 //----------------------------------------------------------------------------- 0155 //! Get the size of the buffer. 0156 //! 0157 //! @return size of the buffer. 0158 //----------------------------------------------------------------------------- 0159 0160 inline int BuffSize() const {return size;} 0161 0162 //----------------------------------------------------------------------------- 0163 //! Produce a clone of this buffer. 0164 //! 0165 //! @param trim - when true the memory buffer is trimmed to be of 0166 //! sufficient size to hold the actual data. Otherwise, the 0167 //! cloned memory buffer is of the same length. 0168 //! 0169 //! @return !0 - pointer to the cloned buffer. 0170 //! =0 - insufficient memory to clone the buffer. 0171 //----------------------------------------------------------------------------- 0172 0173 XrdOucBuffer *Clone(bool trim=true); 0174 0175 //----------------------------------------------------------------------------- 0176 //! Get a pointer to the data in the buffer. 0177 //! 0178 //! @return pointer to the data. 0179 //----------------------------------------------------------------------------- 0180 0181 inline char *Data() const {return data+doff;} 0182 0183 //----------------------------------------------------------------------------- 0184 //! Get a pointer to the data in the buffer and the length of the data. 0185 //! 0186 //! @param dataL - place where the length is to be stored. 0187 //! 0188 //! @return pointer to the data with dataL holding its length. 0189 //----------------------------------------------------------------------------- 0190 0191 inline char *Data(int &dataL) const {dataL = dlen; return data+doff;} 0192 0193 //----------------------------------------------------------------------------- 0194 //! Get the data length. 0195 //! 0196 //! @return The data length. 0197 //----------------------------------------------------------------------------- 0198 0199 inline int DataLen() {return dlen;} 0200 0201 //----------------------------------------------------------------------------- 0202 //! Highjack the buffer contents and reinitialize the original buffer. 0203 //! 0204 //! @param bPsz - the desired size to be given to the highjacked buffer. If 0205 //! zero, the current size is used. Same size resictions apply 0206 //! as for buffer pool Alloc(), above. 0207 //! 0208 //! @return !0 - pointer to a usable buffer object which is identical to the 0209 //! original buffer. The original buffer was reallocated with 0210 //! the specified size. 0211 //! @return =0 - insufficient memory to allocate a buffer. 0212 //----------------------------------------------------------------------------- 0213 0214 XrdOucBuffer *Highjack(int bPsz=0); 0215 0216 //----------------------------------------------------------------------------- 0217 //! Recycle the buffer. The buffer may be reused in the future. 0218 //----------------------------------------------------------------------------- 0219 0220 inline void Recycle() {buffPool->bSlot[slot].Recycle(this);} 0221 0222 //----------------------------------------------------------------------------- 0223 //! Resize the buffer. 0224 //! 0225 //! @param newsz - the size that the buffer is to have. The same restrictions 0226 //! apply as for buffer pool Alloc(), above. 0227 //! 0228 //! @return true - buffer has been reallocated. 0229 //! @return false - insufficient memoy to reallocated the buffer. 0230 //----------------------------------------------------------------------------- 0231 0232 bool Resize(int newsz); 0233 0234 //----------------------------------------------------------------------------- 0235 //! Set the data length of offset. 0236 //! 0237 //! @param dataL - the length of the data. 0238 //! @param dataO - the offset of the data in the buffer. 0239 //----------------------------------------------------------------------------- 0240 0241 inline void SetLen(int dataL, int dataO=0) {dlen = dataL; doff = dataO;} 0242 0243 //----------------------------------------------------------------------------- 0244 //! Public constructor. You can create one-time buffers not associated with a 0245 //! buffer pool via new to associated your own storage area that will be 0246 //! freed when the buffer is recycled. This may be handy to pass along such a 0247 //! buffer to XrdOucErrInfo in a pinch. A one-time buffer is restricted and 0248 //! the Clone(), Highjack() and Resize() methods will always fail. However, 0249 //! all the other methods will work in the expected way. 0250 //! 0251 //! @param buff - pointer to a storage area obtained via posix_memalign() 0252 //! and it will be released via free(). 0253 //! @param blen - the size of the buffer as well as the data length. 0254 //! Use SetLen() to set a new data length if it differs. 0255 //----------------------------------------------------------------------------- 0256 0257 XrdOucBuffer(char *buff, int blen); 0258 0259 private: 0260 XrdOucBuffer(XrdOucBuffPool *pP, int snum) 0261 : data(0), dlen(0), doff(0), size(pP->bSlot[snum].size), 0262 slot(snum), buffPool(pP) {} 0263 0264 XrdOucBuffer() 0265 : data(0), dlen(0), doff(0), size(0), slot(0), buffPool(0) {} 0266 0267 ~XrdOucBuffer() {if (data) free(data);} 0268 0269 char *data; 0270 int dlen; 0271 int doff; 0272 int size; 0273 int slot; 0274 union{XrdOucBuffer *buffNext; 0275 XrdOucBuffPool *buffPool; 0276 }; 0277 }; 0278 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |