00001 /******************************************************************************/ 00002 /* */ 00003 /* X r d O u c B u f f e r . c c */ 00004 /* */ 00005 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */ 00006 /* All Rights Reserved */ 00007 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00008 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00009 /******************************************************************************/ 00010 00011 // $Id: XrdNetBuffer.cc 25553 2008-09-26 07:57:42Z ganis $ 00012 00013 const char *XrdNetBufferCVSID = "$Id: XrdNetBuffer.cc 25553 2008-09-26 07:57:42Z ganis $"; 00014 00015 #ifndef WIN32 00016 #include <unistd.h> 00017 #endif 00018 #include <sys/types.h> 00019 #include <stdlib.h> 00020 #if !defined(__macos__) && !defined(__FreeBSD__) 00021 #include <malloc.h> 00022 #endif 00023 00024 #include "XrdNet/XrdNetBuffer.hh" 00025 #include "XrdSys/XrdSysPlatform.hh" 00026 00027 /******************************************************************************/ 00028 /* X r d N e t B u f f e r Q M e t h o d s */ 00029 /******************************************************************************/ 00030 /******************************************************************************/ 00031 /* C o n s t r u c t o r */ 00032 /******************************************************************************/ 00033 00034 XrdNetBufferQ::XrdNetBufferQ(int bsz, int maxb) 00035 { 00036 size = bsz; 00037 alignit = (size < sysconf(_SC_PAGESIZE) 00038 ? size : sysconf(_SC_PAGESIZE)); 00039 maxbuff = maxb; 00040 numbuff = 0; 00041 } 00042 00043 /******************************************************************************/ 00044 /* D e s t r u c t o r */ 00045 /******************************************************************************/ 00046 00047 XrdNetBufferQ::~XrdNetBufferQ() 00048 { 00049 XrdNetBuffer *bp; 00050 00051 while((bp = BuffStack.Pop())) delete bp; 00052 } 00053 00054 /******************************************************************************/ 00055 /* A l l o c */ 00056 /******************************************************************************/ 00057 00058 XrdNetBuffer *XrdNetBufferQ::Alloc() 00059 { 00060 XrdNetBuffer *bp; 00061 00062 // Lock the data area 00063 // 00064 BuffList.Lock(); 00065 00066 // Either return a new buffer or an old one 00067 // 00068 if ((bp = BuffStack.Pop())) numbuff--; 00069 else if ((bp = new XrdNetBuffer(this)) 00070 && !(bp->data = (char *)memalign(alignit, size))) 00071 {delete bp; bp = 0;} 00072 00073 // Unlock the data area 00074 // 00075 BuffList.UnLock(); 00076 00077 // Return the buffer 00078 // 00079 return bp; 00080 } 00081 00082 /******************************************************************************/ 00083 /* R e c y c l e */ 00084 /******************************************************************************/ 00085 00086 void XrdNetBufferQ::Recycle(XrdNetBuffer *bp) 00087 { 00088 00089 // Check if we have enough objects, if so, delete ourselves and return 00090 // 00091 if (numbuff >= maxbuff) {delete bp; return;} 00092 bp->dlen = 0; 00093 00094 // Add the buffer to the recycle list 00095 // 00096 BuffList.Lock(); 00097 BuffStack.Push(&bp->BuffLink); 00098 numbuff++; 00099 BuffList.UnLock(); 00100 return; 00101 } 00102 00103 /******************************************************************************/ 00104 /* S e t */ 00105 /******************************************************************************/ 00106 00107 void XrdNetBufferQ::Set(int maxb) 00108 { 00109 00110 // Lock the data area, set max buffers, unlock and return 00111 // 00112 BuffList.Lock(); 00113 maxbuff = maxb; 00114 BuffList.UnLock(); 00115 return; 00116 } 00117 00118 /******************************************************************************/ 00119 /* X r d N e t B u f f e r M e t h o d s */ 00120 /******************************************************************************/ 00121 /******************************************************************************/ 00122 /* C o n s t r u c t o r */ 00123 /******************************************************************************/ 00124 00125 XrdNetBuffer::XrdNetBuffer(XrdNetBufferQ *bq) : BuffLink(this) 00126 { 00127 BuffQ= bq; 00128 data = 0; 00129 dlen = 0; 00130 }