XRootD
Loading...
Searching...
No Matches
XrdXrootdMonFMap.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d X r o o t d M o n F M a p . h h */
4/* */
5/* (c) 2012 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#include <unistd.h>
34
36
39
40/******************************************************************************/
41/* S t a t i c M e m b e r s */
42/******************************************************************************/
43
44long XrdXrootdMonFMap::invVal = 1;
45long XrdXrootdMonFMap::valVal = ~1;
46
47/******************************************************************************/
48/* F r e e */
49/******************************************************************************/
50
51bool XrdXrootdMonFMap::Free(int slotNum)
52{
53// Validate the data before freeing the slot
54//
55 if (!fMap || slotNum < 0 || slotNum >= fmSize || fMap[slotNum].cVal & invVal)
56 return false;
57
58// Plase this entry on the free list
59//
60 fMap[slotNum].cPtr = free.cPtr;
61 fMap[slotNum].cVal |= invVal;
62 free.cPtr = &fMap[slotNum];
63 return true;
64}
65
66/******************************************************************************/
67/* Private: I n i t */
68/******************************************************************************/
69
70bool XrdXrootdMonFMap::Init()
71{
72 static const int bytes = fmSize * sizeof(cvPtr);
73 static int pagsz = getpagesize();
74 void *mPtr;
75 int alignment, i;
76
77// Allocate memory
78//
79 alignment = (bytes < pagsz ? 1024 : pagsz);
80 if (posix_memalign(&mPtr, alignment, bytes)) return false;
81 fMap = (cvPtr *)mPtr;
82
83// Chain all the entries together
84//
85 for (i = 1; i < fmSize; i++)
86 {fMap[i-1].cPtr = &fMap[i];
87 fMap[i-1].cVal |= invVal;
88 }
89 fMap[fmSize-1].cVal = invVal;
90 free.cPtr = &fMap[0];
91 return true;
92}
93
94/******************************************************************************/
95/* I n s e r t */
96/******************************************************************************/
97
99{
100 cvPtr *mEnt;
101
102// Check if we have a free slot available
103//
104 if (!free.cVal) {if (fMap || !Init()) return -1;}
105
106// Return the free slot (Init() gaurantees one is available)
107//
108 mEnt = free.cPtr;
109 free.cPtr = free.cPtr->cPtr;
110 free.cVal &= valVal;
111 mEnt->vPtr = fsP;
112 return mEnt - fMap;
113}
114
115/******************************************************************************/
116/* N e x t */
117/******************************************************************************/
118
120{
121
122// Return next valid pointer
123//
124 for (; slotNum < fmSize-1; slotNum++)
125 {if (!(fMap[slotNum].cVal & invVal)) return fMap[slotNum++].vPtr;}
126
127// At the end of the map
128//
129 return 0;
130}
static const int fmSize
bool Free(int slotNum)
XrdXrootdFileStats * Next(int &slotNum)
int Insert(XrdXrootdFileStats *fsP)