]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PageStack.h
Convert most Segment::mem() calls to reserve().
[thirdparty/squid.git] / src / ipc / mem / PageStack.h
1 /*
2 * $Id$
3 *
4 */
5
6 #ifndef SQUID_IPC_MEM_PAGE_STACK_H
7 #define SQUID_IPC_MEM_PAGE_STACK_H
8
9 #include "ipc/AtomicWord.h"
10 #include "ipc/mem/Segment.h"
11
12 namespace Ipc {
13
14 namespace Mem {
15
16 /// Atomic container of "free" page numbers inside a single SharedMemory space.
17 /// Assumptions: all page numbers are unique, positive, have an known maximum,
18 /// and can be temporary unavailable as long as they are never trully lost.
19 class PageStack {
20 public:
21 typedef uint32_t Value; ///< stack item type (a free page number)
22
23 /// creates a new shared stack that can hold up to capacity items
24 PageStack(const String &id, const unsigned int capacity);
25 /// attaches to the identified shared stack
26 PageStack(const String &id);
27 /// unlinks shared memory segment
28 static void Unlink(const String &id);
29
30 /// lower bound for the number of free pages
31 unsigned int size() const { return max(0, shared->theSize.get()); }
32
33 /// sets value and returns true unless no free page numbers are found
34 bool pop(Value &value);
35 /// makes value available as a free page number to future pop() callers
36 void push(const Value value);
37
38 private:
39 /// stack index and size type (may temporary go negative)
40 typedef int Offset;
41
42 struct Shared {
43 Shared(const unsigned int aCapacity);
44
45 /// total shared memory size required to share
46 static size_t MemSize(const unsigned int capacity);
47
48 // these help iterate the stack in search of a free spot or a page
49 Offset next(const Offset idx) const { return (idx + 1) % theCapacity; }
50 Offset prev(const Offset idx) const { return (theCapacity + idx - 1) % theCapacity; }
51
52 const Offset theCapacity; ///< stack capacity, i.e. theItems size
53 /// lower bound for the number of free pages (may get negative!)
54 AtomicWordT<Offset> theSize;
55
56 /// last readable item index; just a hint, not a guarantee
57 AtomicWordT<Offset> theLastReadable;
58 /// first writable item index; just a hint, not a guarantee
59 AtomicWordT<Offset> theFirstWritable;
60
61 typedef AtomicWordT<Value> Item;
62 Item theItems[]; ///< page number storage
63 };
64
65 Segment shm; ///< shared memory segment to store metadata (and pages)
66 Shared *shared; ///< our metadata, shared among all stack users
67 };
68
69 } // namespace Mem
70
71 } // namespace Ipc
72
73 #endif // SQUID_IPC_MEM_PAGE_STACK_H