XRootD
XrdSsiShMap.hh
Go to the documentation of this file.
1 #ifndef __SSI_SHMAP__
2 #define __SSI_SHMAP__
3 /******************************************************************************/
4 /* */
5 /* X r d S s i S h M a p . h h */
6 /* */
7 /* (c) 2015 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <cstdlib>
33 #include <cstring>
34 
35 #include "XrdSsi/XrdSsiShMat.hh"
36 
37 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 
43 namespace XrdSsi
44 {
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
48 
50  {ReadOnly = 1,
51  ReadWrite = 2
52  };
53 
54 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 
60 static const int ShMap_4Resize = -1;
61 
63  {int indexSize;
64  int maxKeyLen;
65  int maxKeys;
66  int mode;
67  int options;
68  int reserved;
69 
70 //-----------------------------------------------------------------------------
72 //-----------------------------------------------------------------------------
73 
74 static const
75  int MultW = 0x88000000;
76 static const
77  int noMultW = 0x08000000;
78 static const
79  int ReUse = 0x44000000;
80 static const
81  int noReUse = 0x04000000;
82 
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 
87  ShMap_Parms() : indexSize(16381), maxKeyLen(63), maxKeys(32768),
88  mode(0640), options(0), reserved(0) {}
89 
90 //-----------------------------------------------------------------------------
92 //-----------------------------------------------------------------------------
93 static const
94  int ForResize = 0;
95 
96  ShMap_Parms(int rsz) : indexSize(0), maxKeyLen(0), maxKeys(0),
97  mode(0640), options(0), reserved(rsz) {}
98 
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
102 
104  };
105 
106 //-----------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
109 
111 
112 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
121 
122 typedef int (*ShMap_Hash_t)(const char *key);
123 
124 template<class T>
125 class ShMap
126 {
127 public:
128 
129 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
148 
149 bool Attach(const char *path, ShMap_Access access, int tmo=-1);
150 
151 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
179 
180 bool Create(const char *path, ShMap_Parms &parms);
181 
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
185 
186 void Detach();
187 
188 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
194 
195 bool Export();
196 
197 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
207 
208 bool Add(const char *key, T &val);
209 
210 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
223 
224 bool Del(const char *key, T *valP=0);
225 
226 //-----------------------------------------------------------------------------
247 //-----------------------------------------------------------------------------
248 
249 bool Enumerate(void *&jar, char *&key, T *&val);
250 
251 //-----------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------
265 
266 bool Enumerate(void *&jar);
267 
268 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
276 
277 bool Exists(const char *key);
278 
279 //-----------------------------------------------------------------------------
288 //-----------------------------------------------------------------------------
289 
290 bool Get(const char *key, T &val);
291 
292 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
325 
326 int Info(const char *vname, char *buff=0, int blen=0);
327 
328 //-----------------------------------------------------------------------------
340 //-----------------------------------------------------------------------------
341 
342 bool Rep(const char *key, T &val, T *valP=0);
343 
344 //-----------------------------------------------------------------------------
356 //-----------------------------------------------------------------------------
357 
358 bool Resize(ShMap_Parms *parms=0);
359 
360 //-----------------------------------------------------------------------------
382 //-----------------------------------------------------------------------------
383 
384 bool Sync(SyncOpt dosync, int syncqsz=256);
385 
386 //-----------------------------------------------------------------------------
401 //-----------------------------------------------------------------------------
402 
403  ShMap(const char *typeName, ShMap_Hash_t hFunc=0,
404  const char *implName=0)
405  : shMat(0), hashFunc(hFunc), typeID(strdup(typeName)),
406  implID((implName ? strdup(implName) : 0)) {}
407 
408 //-----------------------------------------------------------------------------
410 //-----------------------------------------------------------------------------
411 
413  if (typeID) free(typeID);
414  if (implID) free(implID);
415  }
416 
417 private:
418 
419 XrdSsiShMat *shMat;
420 ShMap_Hash_t hashFunc;
421 char *typeID;
422 char *implID;
423 };
424 }
425 
426 /******************************************************************************/
427 /* A c t u a l I m p l e m e n t a t i o n */
428 /******************************************************************************/
429 
430 #include "XrdSsi/XrdSsiShMap.icc"
431 #endif
int access(const char *path, int amode)
void Detach()
Detach the map from the shared memory.
bool Del(const char *key, T *valP=0)
bool Get(const char *key, T &val)
bool Attach(const char *path, ShMap_Access access, int tmo=-1)
Definition: XrdSsiShMap.icc:57
~ShMap()
Destructor.
Definition: XrdSsiShMap.hh:412
ShMap(const char *typeName, ShMap_Hash_t hFunc=0, const char *implName=0)
Definition: XrdSsiShMap.hh:403
bool Enumerate(void *&jar, char *&key, T *&val)
bool Rep(const char *key, T &val, T *valP=0)
bool Sync(SyncOpt dosync, int syncqsz=256)
bool Create(const char *path, ShMap_Parms &parms)
bool Exists(const char *key)
int Info(const char *vname, char *buff=0, int blen=0)
bool Add(const char *key, T &val)
Definition: XrdSsiShMap.icc:37
bool Resize(ShMap_Parms *parms=0)
ShMap_Access
The action parameter that must be passed to the Attach() method.
Definition: XrdSsiShMap.hh:50
@ ReadOnly
reading
Definition: XrdSsiShMap.hh:50
@ ReadWrite
reading & writing
Definition: XrdSsiShMap.hh:51
int(* ShMap_Hash_t)(const char *key)
Definition: XrdSsiShMap.hh:122
SyncOpt
Options valid for the Sync() method.
Definition: XrdSsiShMap.hh:110
static const int ShMap_4Resize
Definition: XrdSsiShMap.hh:60
static const int MultW
Bit options that may be or'd into he options member above.
Definition: XrdSsiShMap.hh:75
int maxKeys
Maximum expected keys.
Definition: XrdSsiShMap.hh:65
int mode
Mode setting for the newly created file.
Definition: XrdSsiShMap.hh:66
int maxKeyLen
Maximum key length.
Definition: XrdSsiShMap.hh:64
int reserved
Reserved for future ABI complaint use.
Definition: XrdSsiShMap.hh:68
~ShMap_Parms()
Destructor.
Definition: XrdSsiShMap.hh:103
static const int ReUse
Reuse map storage.
Definition: XrdSsiShMap.hh:79
static const int ForResize
Constructor suitable for Resize() (use ShMap_Parms(ForResize)).
Definition: XrdSsiShMap.hh:94
static const int noReUse
Opposite (default for Create)
Definition: XrdSsiShMap.hh:81
static const int noMultW
Opposite (default for Create)
Definition: XrdSsiShMap.hh:77
int options
Bit or'd ShMop_xxxx options below.
Definition: XrdSsiShMap.hh:67
int indexSize
Number of hash table entries to create.
Definition: XrdSsiShMap.hh:63
ShMap_Parms()
Constructor suitable for Create()
Definition: XrdSsiShMap.hh:87