XRootD
XrdCmsKey.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d C m s K e y . c c */
4 /* */
5 /* (c) 2007 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 <cerrno>
32 #include <cstring>
33 
34 #include "XrdCms/XrdCmsKey.hh"
35 #include "XrdCms/XrdCmsTrace.hh"
36 #include "XrdOuc/XrdOucCRC.hh"
37 #include "XrdSys/XrdSysError.hh"
38 
39 using namespace XrdCms;
40 
41 /******************************************************************************/
42 /* C l a s s X r d C m s K e y */
43 /******************************************************************************/
44 /******************************************************************************/
45 /* public s e t H a s h */
46 /******************************************************************************/
47 
49 {
50  if (!Len) Len = strlen(Val);
51  if (!(Hash = XrdOucCRC::CRC32((const unsigned char *)Val, Len))) Hash = 1;
52 }
53 
54 /******************************************************************************/
55 /* C l a s s X r d C m s K e y I t e m */
56 /******************************************************************************/
57 /******************************************************************************/
58 /* S t a t i c D a t a */
59 /******************************************************************************/
60 
61 XrdCmsKeyItem *XrdCmsKeyItem::TockTable[TickRate] = {0};
62 XrdCmsKeyItem *XrdCmsKeyItem::Free = 0;
63 int XrdCmsKeyItem::numFree = 0;
64 int XrdCmsKeyItem::numHave = 0;
65 int XrdCmsKeyItem::numNull = 0;
66 
67 /******************************************************************************/
68 /* static public A l l o c */
69 /******************************************************************************/
70 
71 XrdCmsKeyItem *XrdCmsKeyItem::Alloc(unsigned int theTock)
72 {
73  XrdCmsKeyItem *kP;
74 
75 // Try to allocate an existing item or replenish the list
76 //
77  do {if ((kP = Free))
78  {Free = kP->Next;
79  numFree--;
80  theTock &= TickMask;
81  kP->Key.TOD = theTock;
82  kP->Key.TODRef = TockTable[theTock];
83  TockTable[theTock] = kP;
84  if (!(kP->Key.Ref++)) kP->Key.Ref = 1;
85  kP->Loc.roPend = kP->Loc.rwPend = 0;
86  return kP;
87  }
88  numNull++;
89  } while(Replenish());
90 
91 // We failed
92 //
93  Say.Emsg("Key", ENOMEM, "create key item");
94  return (XrdCmsKeyItem *)0;
95 }
96 
97 /******************************************************************************/
98 /* public R e c y c l e */
99 /******************************************************************************/
100 
102 {
103  static char *noKey = (char *)"";
104 
105 // Clear up data areas
106 //
107  if (Key.Val && Key.Val != noKey) {free(Key.Val); Key.Val = noKey;}
108  Key.Ref++; Key.Hash = 0;
109 
110 // Put entry on the free list
111 //
112  Next = Free; Free = this;
113  numFree++;
114 }
115 
116 /******************************************************************************/
117 /* public R e l o a d */
118 /******************************************************************************/
119 
121 {
122  Key.TOD &= static_cast<unsigned char>(TickMask);
123  Key.TODRef = TockTable[Key.TOD];
124  TockTable[Key.TOD] = this;
125 }
126 
127 /******************************************************************************/
128 /* static public R e p l e n i s h */
129 /******************************************************************************/
130 
132 {
133  EPNAME("Replenish");
134  XrdCmsKeyItem *kP;
135  int i;
136 
137 // Allocate a quantum of free elements and chain them into the free list
138 //
139  if (!(kP = new XrdCmsKeyItem[minAlloc])) return 0;
140  DEBUG("old free " <<numFree <<" + " <<minAlloc <<" = " <<numHave+minAlloc);
141 
142 // We would do this in an initializer but that causes problems when alloacting
143 // temporary items on the stack. So, manually put these on the free list.
144 //
145  i = minAlloc;
146  while(i--) {kP->Next = Free; Free = kP; kP++;}
147 
148 // Return the number we have free
149 //
150  numHave += minAlloc;
151  numFree += minAlloc;
152  return numFree;
153 }
154 
155 /******************************************************************************/
156 /* static public S t a t s */
157 /******************************************************************************/
158 
159 void XrdCmsKeyItem::Stats(int &isAlloc, int &isFree, int &wasNull)
160 {
161 
162  isAlloc = numHave;
163  isFree = numFree;
164  wasNull = numNull;
165  numNull = 0;
166 }
167 
168 /******************************************************************************/
169 /* static public U n l o a d */
170 /******************************************************************************/
171 
172 XrdCmsKeyItem *XrdCmsKeyItem::Unload(unsigned int theTock)
173 {
174  XrdCmsKeyItem myItem, *nP, *pP = &myItem;
175 
176 // Remove all entries from the indicated list. If any entries have been
177 // reassigned to a different list, move them to the right list. Otherwise,
178 // make the entry unfindable by clearing the hash code. Since item recycling
179 // requires knowing the hash code, we save it elsewhere in the object.
180 //
181  theTock &= TickMask;
182  myItem.Key.TODRef = TockTable[theTock]; TockTable[theTock] = 0;
183  while((nP = pP->Key.TODRef))
184  if (nP->Key.TOD == theTock)
185  {nP->Loc.HashSave = nP->Key.Hash; nP->Key.Hash = 0; pP = nP;}
186  else {pP->Key.TODRef = nP->Key.TODRef;
187  nP->Key.TODRef = TockTable[nP->Key.TOD];
188  TockTable[nP->Key.TOD] = nP;
189  }
190  return myItem.Key.TODRef;
191 }
192 
193 /******************************************************************************/
194 
196 {
197  XrdCmsKeyItem *kP, *pP = 0;
198  unsigned int theTock = theItem->Key.TOD & TickMask;
199 
200 // Remove the entry from the right list
201 //
202  kP = TockTable[theTock];
203  while(kP && kP != theItem) {pP = kP; kP = kP->Key.TODRef;}
204  if (kP)
205  {if (pP) pP->Key.TODRef = kP->Key.TODRef;
206  else TockTable[theTock] = kP->Key.TODRef;
207  kP->Loc.HashSave = kP->Key.Hash; kP->Key.Hash = 0;
208  }
209  return kP;
210 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
#define EPNAME(x)
Definition: XrdBwmTrace.hh:56
bool Hash
static XrdCmsKeyItem * Unload(unsigned int theTock)
Definition: XrdCmsKey.cc:172
static XrdCmsKeyItem * Alloc(unsigned int theTock)
Definition: XrdCmsKey.cc:71
static int Replenish()
Definition: XrdCmsKey.cc:131
void Recycle()
Definition: XrdCmsKey.cc:101
XrdCmsKeyLoc Loc
Definition: XrdCmsKey.hh:129
XrdCmsKey Key
Definition: XrdCmsKey.hh:130
void Reload()
Definition: XrdCmsKey.cc:120
XrdCmsKeyItem * Next
Definition: XrdCmsKey.hh:131
static void Stats(int &isAlloc, int &isFree, int &wasEmpty)
Definition: XrdCmsKey.cc:159
short rwPend
Definition: XrdCmsKey.hh:102
short roPend
Definition: XrdCmsKey.hh:101
void setHash()
Definition: XrdCmsKey.cc:48
unsigned int Hash
Definition: XrdCmsKey.hh:53
XrdCmsKeyItem * TODRef
Definition: XrdCmsKey.hh:51
unsigned char TOD
Definition: XrdCmsKey.hh:55
unsigned char Ref
Definition: XrdCmsKey.hh:56
static uint32_t CRC32(const unsigned char *data, int count)
Definition: XrdOucCRC.cc:171
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95
XrdSysError Say