XRootD
XrdOucSid Class Reference

#include <XrdOucSid.hh>

+ Collaboration diagram for XrdOucSid:

Classes

union  theSid
 The type to pass to Obtain(). Simply cast the char[2] to (theSid *). More...
 

Public Member Functions

 XrdOucSid (int numSid=256, bool mtproof=true, XrdOucSid *glblSid=0)
 
 ~XrdOucSid ()
 
bool Obtain (theSid *sidP)
 
bool Release (theSid *sidP)
 
void Reset ()
 

Detailed Description

This class implements a fast bit vector based stream ID generator. When stream ID's are generated on a connection basis, each connection can allocate a local SID object sizing the bit vector to match the expected number of simultaneous stream ID's in order to save memory. In order to accomodate overflows, the local object can be initialized with a global SID object that be used to obtain stream ID's when the local object runs out. See the constructor description for the details.

Definition at line 45 of file XrdOucSid.hh.


Class Documentation

◆ XrdOucSid::theSid

union XrdOucSid::theSid

The type to pass to Obtain(). Simply cast the char[2] to (theSid *).

Definition at line 53 of file XrdOucSid.hh.

+ Collaboration diagram for XrdOucSid::theSid:
Class Members
char sidC[2]
short sidS

Constructor & Destructor Documentation

◆ XrdOucSid()

XrdOucSid::XrdOucSid ( int  numSid = 256,
bool  mtproof = true,
XrdOucSid glblSid = 0 
)

Constructor

Parameters
numSidthe maximum number of stream ID's that this object can allocate. The value is rounded up to the next multiple of eight. Every 8 ID's requires one byte of memory. Normally, stream ID's range from 0 to 32767, though technically can go to 64K-1 but are then represented as a negative short.
mtproofWhen true (the default) the object obtains a lock before doing any operations. When false, it does not and must be protected, if need be, by some other serialization.
glblSida pointer to another SID object that can be used to obtain stream ID's should this object run out of them. Typically, this is a global pool of stream ID's.

Definition at line 37 of file XrdOucSid.cc.

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 }

◆ ~XrdOucSid()

XrdOucSid::~XrdOucSid ( )

Destructor. Only this object is destroyed. The global object, if any, is not alteered in any way.

Definition at line 61 of file XrdOucSid.cc.

62 {
63  if (sidVec) free(sidVec);
64 }

Member Function Documentation

◆ Obtain()

bool XrdOucSid::Obtain ( XrdOucSid::theSid sidP)

Obtain a stream ID. When not needed use Release() to recycle them.

Parameters
sidP-> the place where the new stream ID is to be placed.
Returns
true a stream ID was allocated.
false no more stream ID's are available. The sidP target was not altered (i.e. does not contain a stream ID).

Definition at line 70 of file XrdOucSid.cc.

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 }
bool Obtain(theSid *sidP)
Definition: XrdOucSid.cc:70
XrdOucSid * sidP
Definition: XrdPss.cc:107

References XrdSysMutex::Lock(), Obtain(), XrdProxy::sidP, and XrdSysMutex::UnLock().

Referenced by Obtain(), and XrdPssUrlInfo::setID().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Release()

bool XrdOucSid::Release ( XrdOucSid::theSid sidP)

Release a stream ID using the stream ID value.

Parameters
sidP-> to the stream ID cast as a theSid pointer.
Returns
true the stream ID was released and may be reassigned.
false the stream ID is invalid. No action performed.

Definition at line 115 of file XrdOucSid.cc.

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 }
bool Release(theSid *sidP)
Definition: XrdOucSid.cc:115

References XrdSysMutex::Lock(), Release(), XrdProxy::sidP, and XrdSysMutex::UnLock().

Referenced by XrdPssUrlInfo::~XrdPssUrlInfo(), and Release().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Reset()

void XrdOucSid::Reset ( )

Unassign all stream ID's as if the object were newly allocated. Any global stream ID object associated with this object is not reset.

Definition at line 147 of file XrdOucSid.cc.

148 {
149  if (sidLock) sidMutex.Lock();
150  if (sidVec) memset(sidVec, 0xff, sidSize);
151  if (sidLock) sidMutex.UnLock();
152 }

References XrdSysMutex::Lock(), and XrdSysMutex::UnLock().

+ Here is the call graph for this function:

The documentation for this class was generated from the following files: