XRootD
XrdOucSid.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d O u c S i d . c c */
4 /* */
5 /* (c) 2013 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 #include "XrdOuc/XrdOucSid.hh"
32 
33 /******************************************************************************/
34 /* C o n s t r u c t o r */
35 /******************************************************************************/
36 
37 XrdOucSid::XrdOucSid(int numSid, bool mtproof, XrdOucSid *glblSid)
38 {
39 
40 // Set obvious values
41 //
42  sidLock = mtproof;
43  globalSid = glblSid;
44 
45 // Calculate actual values
46 //
47  sidSize = (numSid / 8) + ((numSid % 8 ? 1 : 0) * 8);
48  sidMax = sidSize * 8;
49  sidFree = 0;
50 
51 // Allocate a sid table for the number we want
52 //
53  sidVec = (unsigned char *)malloc(sidSize);
54  memset(sidVec, 255, sidSize);
55 }
56 
57 /******************************************************************************/
58 /* D e s t r u c t o r */
59 /******************************************************************************/
60 
62 {
63  if (sidVec) free(sidVec);
64 }
65 
66 /******************************************************************************/
67 /* O b t a i n */
68 /******************************************************************************/
69 
71 {
72 // 0000 0001 0010 0011 0100 0101 0110 0111
73  static const // 1000 1001 1010 1011 1100 1101 1110 1111
74  unsigned char mask[] = {0x00,0x11,0x22,0x11,0x44,0x11,0x22,0x11,
75  0x88,0x11,0x22,0x11,0x44,0x11,0x22,0x11};
76  bool aOK = true;
77 
78 // Lock if need be
79 //
80  if (sidLock) sidMutex.Lock();
81 
82 // Realign the vector
83 //
84  while(sidFree < sidSize && sidVec[sidFree] == 0x00) sidFree++;
85 
86 // Allocate a local free sid if we actually have one free. Otherwise, try to
87 // allocate one from the global sid table.
88 //
89  if (sidFree < sidSize)
90  {int sidMask, sidNum, sidByte = sidVec[sidFree] & 0xff;
91  if (sidByte & 0x0f)
92  {sidMask = mask[sidByte & 0x0f] & 0x0f;
93  sidNum = (sidMask > 4 ? 3 : sidMask>>1);
94  } else {
95  sidMask = mask[sidByte >> 4] & 0xf0;
96  sidNum = (sidMask > 64 ? 7 : (sidMask>>5) + 4);
97  }
98  sidVec[sidFree] &= ~sidMask;
99  sidP->sidS = (sidFree * 8) + sidNum;
100  } else if (globalSid)
101  {aOK = globalSid->Obtain(sidP);
102  sidP->sidS += sidMax;
103  } else aOK = false;
104 
105 // Unlock if locked and return result
106 //
107  if (sidLock) sidMutex.UnLock();
108  return aOK;
109 }
110 
111 /******************************************************************************/
112 /* R e l e a s e */
113 /******************************************************************************/
114 
116 {
117  static const // 0 1 2 3 4 5 6 7
118  unsigned char mask[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
119  bool aOK = true;
120 
121 // Lock if need be
122 //
123  if (sidLock) sidMutex.Lock();
124 
125 // If this is a local sid, then handle it locally. Otherwise, try releasing
126 // using the global sid pool.
127 //
128  if (sidP->sidS < sidMax)
129  {int sidIdx = (sidP->sidS)>>3;
130  sidVec[sidIdx] |= mask[(sidP->sidS)%8];
131  if (sidIdx < sidFree) sidFree = sidIdx;
132  } else if (globalSid)
133  {short gSid = sidP->sidS - sidMax;
134  aOK = globalSid->Release((theSid *)&gSid);
135  } else aOK = false;
136 
137 // Unlock if locked and return result
138 //
139  if (sidLock) sidMutex.UnLock();
140  return aOK;
141 }
142 
143 /******************************************************************************/
144 /* R e s e t */
145 /******************************************************************************/
146 
148 {
149  if (sidLock) sidMutex.Lock();
150  if (sidVec) memset(sidVec, 0xff, sidSize);
151  if (sidLock) sidMutex.UnLock();
152 }
XrdOucSid(int numSid=256, bool mtproof=true, XrdOucSid *glblSid=0)
Definition: XrdOucSid.cc:37
void Reset()
Definition: XrdOucSid.cc:147
bool Obtain(theSid *sidP)
Definition: XrdOucSid.cc:70
~XrdOucSid()
Definition: XrdOucSid.cc:61
bool Release(theSid *sidP)
Definition: XrdOucSid.cc:115
The type to pass to Obtain(). Simply cast the char[2] to (theSid *).
Definition: XrdOucSid.hh:53
XrdOucSid * sidP
Definition: XrdPss.cc:107