XRootD
XrdNetBuffer.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c B u f f e r . c c */
4 /* */
5 /* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #ifndef WIN32
32 #include <unistd.h>
33 #endif
34 #include <sys/types.h>
35 #include <cstdlib>
36 
37 #include "XrdNet/XrdNetBuffer.hh"
38 #include "XrdSys/XrdSysPlatform.hh"
39 
40 /******************************************************************************/
41 /* X r d N e t B u f f e r Q M e t h o d s */
42 /******************************************************************************/
43 /******************************************************************************/
44 /* C o n s t r u c t o r */
45 /******************************************************************************/
46 
47 XrdNetBufferQ::XrdNetBufferQ(int bsz, int maxb)
48 {
49  size = bsz;
50  alignit = (size < sysconf(_SC_PAGESIZE)
51  ? size : sysconf(_SC_PAGESIZE));
52  maxbuff = maxb;
53  numbuff = 0;
54 }
55 
56 /******************************************************************************/
57 /* D e s t r u c t o r */
58 /******************************************************************************/
59 
61 {
62  XrdNetBuffer *bp;
63 
64  while((bp = BuffStack.Pop())) delete bp;
65 }
66 
67 /******************************************************************************/
68 /* A l l o c */
69 /******************************************************************************/
70 
72 {
73  XrdNetBuffer *bp;
74 
75 // Lock the data area
76 //
77  BuffList.Lock();
78 
79 // Either return a new buffer or an old one
80 //
81  if ((bp = BuffStack.Pop())) numbuff--;
82  else if ((bp = new XrdNetBuffer(this))
83  && posix_memalign((void **)&(bp->data), alignit, size))
84  {delete bp; bp = 0;}
85 
86 // Unlock the data area
87 //
88  BuffList.UnLock();
89 
90 // Return the buffer
91 //
92  return bp;
93 }
94 
95 /******************************************************************************/
96 /* R e c y c l e */
97 /******************************************************************************/
98 
100 {
101 
102 // Check if we have enough objects, if so, delete ourselves and return
103 //
104  if (numbuff >= maxbuff) {delete bp; return;}
105  bp->dlen = 0;
106 
107 // Add the buffer to the recycle list
108 //
109  BuffList.Lock();
110  BuffStack.Push(&bp->BuffLink);
111  numbuff++;
112  BuffList.UnLock();
113  return;
114 }
115 
116 /******************************************************************************/
117 /* S e t */
118 /******************************************************************************/
119 
120 void XrdNetBufferQ::Set(int maxb)
121 {
122 
123 // Lock the data area, set max buffers, unlock and return
124 //
125  BuffList.Lock();
126  maxbuff = maxb;
127  BuffList.UnLock();
128  return;
129 }
130 
131 /******************************************************************************/
132 /* X r d N e t B u f f e r M e t h o d s */
133 /******************************************************************************/
134 /******************************************************************************/
135 /* C o n s t r u c t o r */
136 /******************************************************************************/
137 
139 {
140  BuffQ= bq;
141  data = 0;
142  dlen = 0;
143 }
XrdOucStack< XrdNetBuffer > BuffStack
Definition: XrdNetBuffer.hh:61
XrdSysMutex BuffList
Definition: XrdNetBuffer.hh:60
XrdNetBufferQ(int bsz, int maxb=16)
Definition: XrdNetBuffer.cc:47
XrdNetBuffer * Alloc()
Definition: XrdNetBuffer.cc:71
void Set(int maxb)
void Recycle(XrdNetBuffer *bp)
Definition: XrdNetBuffer.cc:99
XrdNetBuffer(XrdNetBufferQ *bq)
void Push(XrdOucQSItem< T > *item)
Definition: XrdOucChain.hh:57