]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PageStack.h
6 #ifndef SQUID_IPC_MEM_PAGE_STACK_H
7 #define SQUID_IPC_MEM_PAGE_STACK_H
9 #include "ipc/AtomicWord.h"
10 #include "ipc/mem/Segment.h"
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.
21 typedef uint32_t Value
; ///< stack item type (a free page number)
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
);
30 /// lower bound for the number of free pages
31 unsigned int size() const { return max(0, shared
->theSize
.get()); }
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
);
39 /// stack index and size type (may temporary go negative)
43 Shared(const unsigned int aCapacity
);
45 /// total shared memory size required to share
46 static size_t MemSize(const unsigned int capacity
);
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
; }
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
;
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
;
61 typedef AtomicWordT
<Value
> Item
;
62 Item theItems
[]; ///< page number storage
65 Segment shm
; ///< shared memory segment to store metadata (and pages)
66 Shared
*shared
; ///< our metadata, shared among all stack users
73 #endif // SQUID_IPC_MEM_PAGE_STACK_H