]>
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"
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.
22 typedef uint32_t Value
; ///< stack item type (a free page number)
24 PageStack(const uint32_t aPoolId
, const unsigned int aCapacity
, const size_t aPageSize
);
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()); }
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
);
36 bool pageIdIsValid(const PageId
&page
) const;
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;
43 /// stack index and size type (may temporary go negative)
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
; }
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
;
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
69 #endif // SQUID_IPC_MEM_PAGE_STACK_H