]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PageStack.h
Rework shared object design and management API.
[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
11 namespace Ipc {
12
13 namespace Mem {
14
15 class PageId;
16
17 /// Atomic container of "free" page numbers inside a single SharedMemory space.
18 /// Assumptions: all page numbers are unique, positive, have an known maximum,
19 /// and can be temporary unavailable as long as they are never trully lost.
20 class PageStack {
21 public:
22 typedef uint32_t Value; ///< stack item type (a free page number)
23
24 PageStack(const uint32_t aPoolId, const unsigned int aCapacity, const size_t aPageSize);
25
26 unsigned int capacity() const { return theCapacity; }
27 size_t pageSize() const { return thePageSize; }
28 /// lower bound for the number of free pages
29 unsigned int size() const { return max(0, theSize.get()); }
30
31 /// sets value and returns true unless no free page numbers are found
32 bool pop(PageId &page);
33 /// makes value available as a free page number to future pop() callers
34 void push(PageId &page);
35
36 bool pageIdIsValid(const PageId &page) const;
37
38 /// total shared memory size required to share
39 static size_t SharedMemorySize(const uint32_t aPoolId, const unsigned int capacity, const size_t pageSize);
40 size_t sharedMemorySize() const;
41
42 private:
43 /// stack index and size type (may temporary go negative)
44 typedef int Offset;
45
46 // these help iterate the stack in search of a free spot or a page
47 Offset next(const Offset idx) const { return (idx + 1) % theCapacity; }
48 Offset prev(const Offset idx) const { return (theCapacity + idx - 1) % theCapacity; }
49
50 const uint32_t thePoolId; ///< pool ID
51 const Offset theCapacity; ///< stack capacity, i.e. theItems size
52 const size_t thePageSize; ///< page size, used to calculate shared memory 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 } // namespace Mem
66
67 } // namespace Ipc
68
69 #endif // SQUID_IPC_MEM_PAGE_STACK_H