00001 #ifndef __SSI_SHMAP__ 00002 #define __SSI_SHMAP__ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S s i S h M a p . h h */ 00006 /* */ 00007 /* (c) 2015 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00009 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00010 /* */ 00011 /* This file is part of the XRootD software suite. */ 00012 /* */ 00013 /* XRootD is free software: you can redistribute it and/or modify it under */ 00014 /* the terms of the GNU Lesser General Public License as published by the */ 00015 /* Free Software Foundation, either version 3 of the License, or (at your */ 00016 /* option) any later version. */ 00017 /* */ 00018 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00019 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00020 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00021 /* License for more details. */ 00022 /* */ 00023 /* You should have received a copy of the GNU Lesser General Public License */ 00024 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00025 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00026 /* */ 00027 /* The copyright holder's institutional names and contributor's names may not */ 00028 /* be used to endorse or promote products derived from this software without */ 00029 /* specific prior written permission of the institution or contributor. */ 00030 /******************************************************************************/ 00031 00032 #include <stdlib.h> 00033 #include <string.h> 00034 00035 #include "XrdSsi/XrdSsiShMat.hh" 00036 00037 //----------------------------------------------------------------------------- 00041 //----------------------------------------------------------------------------- 00042 00043 namespace XrdSsi 00044 { 00045 //----------------------------------------------------------------------------- 00047 //----------------------------------------------------------------------------- 00048 00049 enum ShMap_Access 00050 {ReadOnly = 1, 00051 ReadWrite = 2 00052 }; 00053 00054 //----------------------------------------------------------------------------- 00058 //----------------------------------------------------------------------------- 00059 00060 static const int ShMap_4Resize = -1; 00061 00062 struct ShMap_Parms 00063 {int indexSize; 00064 int maxKeyLen; 00065 int maxKeys; 00066 int mode; 00067 int options; 00068 int reserved; 00069 00070 //----------------------------------------------------------------------------- 00072 //----------------------------------------------------------------------------- 00073 00074 static const 00075 int MultW = 0x88000000; 00076 static const 00077 int noMultW = 0x08000000; 00078 static const 00079 int ReUse = 0x44000000; 00080 static const 00081 int noReUse = 0x04000000; 00082 00083 //----------------------------------------------------------------------------- 00085 //----------------------------------------------------------------------------- 00086 00087 ShMap_Parms() : indexSize(16381), maxKeyLen(63), maxKeys(32768), 00088 mode(0640), options(0), reserved(0) {} 00089 00090 //----------------------------------------------------------------------------- 00092 //----------------------------------------------------------------------------- 00093 static const 00094 int ForResize = 0; 00095 00096 ShMap_Parms(int rsz) : indexSize(0), maxKeyLen(0), maxKeys(0), 00097 mode(0640), options(0), reserved(rsz) {} 00098 00099 //----------------------------------------------------------------------------- 00101 //----------------------------------------------------------------------------- 00102 00103 ~ShMap_Parms() {} 00104 }; 00105 00106 //----------------------------------------------------------------------------- 00108 //----------------------------------------------------------------------------- 00109 00110 enum SyncOpt {SyncOff = 0, SyncOn, SyncAll, SyncNow, SyncQSz}; 00111 00112 //----------------------------------------------------------------------------- 00120 //----------------------------------------------------------------------------- 00121 00122 typedef int (*ShMap_Hash_t)(const char *key); 00123 00124 template<class T> 00125 class ShMap 00126 { 00127 public: 00128 00129 //----------------------------------------------------------------------------- 00147 //----------------------------------------------------------------------------- 00148 00149 bool Attach(const char *path, ShMap_Access access, int tmo=-1); 00150 00151 //----------------------------------------------------------------------------- 00176 //----------------------------------------------------------------------------- 00177 00178 bool Create(const char *path, ShMap_Parms &parms); 00179 00180 //----------------------------------------------------------------------------- 00182 //----------------------------------------------------------------------------- 00183 00184 void Detach(); 00185 00186 //----------------------------------------------------------------------------- 00191 //----------------------------------------------------------------------------- 00192 00193 bool Export(); 00194 00195 //----------------------------------------------------------------------------- 00204 //----------------------------------------------------------------------------- 00205 00206 bool Add(const char *key, T &val); 00207 00208 //----------------------------------------------------------------------------- 00220 //----------------------------------------------------------------------------- 00221 00222 bool Del(const char *key, T *valP=0); 00223 00224 //----------------------------------------------------------------------------- 00245 //----------------------------------------------------------------------------- 00246 00247 bool Enumerate(void *&jar, char *&key, T *&val); 00248 00249 //----------------------------------------------------------------------------- 00262 //----------------------------------------------------------------------------- 00263 00264 bool Enumerate(void *&jar); 00265 00266 //----------------------------------------------------------------------------- 00273 //----------------------------------------------------------------------------- 00274 00275 bool Exists(const char *key); 00276 00277 //----------------------------------------------------------------------------- 00286 //----------------------------------------------------------------------------- 00287 00288 bool Get(const char *key, T &val); 00289 00290 //----------------------------------------------------------------------------- 00322 //----------------------------------------------------------------------------- 00323 00324 int Info(const char *vname, char *buff=0, int blen=0); 00325 00326 //----------------------------------------------------------------------------- 00338 //----------------------------------------------------------------------------- 00339 00340 bool Rep(const char *key, T &val, T *valP=0); 00341 00342 //----------------------------------------------------------------------------- 00354 //----------------------------------------------------------------------------- 00355 00356 bool Resize(ShMap_Parms *parms=0); 00357 00358 //----------------------------------------------------------------------------- 00380 //----------------------------------------------------------------------------- 00381 00382 bool Sync(SyncOpt dosync, int syncqsz=256); 00383 00384 //----------------------------------------------------------------------------- 00399 //----------------------------------------------------------------------------- 00400 00401 ShMap(const char *typeName, ShMap_Hash_t hFunc=0, 00402 const char *implName=0) 00403 : shMat(0), hashFunc(hFunc), typeID(strdup(typeName)), 00404 implID((implName ? strdup(implName) : 0)) {} 00405 00406 //----------------------------------------------------------------------------- 00408 //----------------------------------------------------------------------------- 00409 00410 ~ShMap() {Detach(); 00411 if (typeID) free(typeID); 00412 if (implID) free(implID); 00413 } 00414 00415 private: 00416 00417 XrdSsiShMat *shMat; 00418 ShMap_Hash_t hashFunc; 00419 char *typeID; 00420 char *implID; 00421 }; 00422 } 00423 00424 /******************************************************************************/ 00425 /* A c t u a l I m p l e m e n t a t i o n */ 00426 /******************************************************************************/ 00427 00428 #include "XrdSsi/XrdSsiShMap.icc" 00429 #endif