]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/PageStack.h
Fix eCAP issues after rev.14093
[thirdparty/squid.git] / src / ipc / mem / PageStack.h
CommitLineData
be17aa82 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
bbc27441
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
be17aa82
AR
7 */
8
9#ifndef SQUID_IPC_MEM_PAGE_STACK_H
10#define SQUID_IPC_MEM_PAGE_STACK_H
11
12#include "ipc/AtomicWord.h"
3a8c5551 13#include "ipc/mem/FlexibleArray.h"
be17aa82 14
9199139f
AR
15namespace Ipc
16{
be17aa82 17
9199139f
AR
18namespace Mem
19{
be17aa82 20
68353d5a
DK
21class PageId;
22
be17aa82
AR
23/// Atomic container of "free" page numbers inside a single SharedMemory space.
24/// Assumptions: all page numbers are unique, positive, have an known maximum,
25/// and can be temporary unavailable as long as they are never trully lost.
9199139f
AR
26class PageStack
27{
be17aa82
AR
28public:
29 typedef uint32_t Value; ///< stack item type (a free page number)
30
68353d5a 31 PageStack(const uint32_t aPoolId, const unsigned int aCapacity, const size_t aPageSize);
be17aa82 32
68353d5a
DK
33 unsigned int capacity() const { return theCapacity; }
34 size_t pageSize() const { return thePageSize; }
b940ff87 35 /// lower bound for the number of free pages
68353d5a 36 unsigned int size() const { return max(0, theSize.get()); }
b940ff87 37
be17aa82 38 /// sets value and returns true unless no free page numbers are found
68353d5a 39 bool pop(PageId &page);
be17aa82 40 /// makes value available as a free page number to future pop() callers
68353d5a
DK
41 void push(PageId &page);
42
43 bool pageIdIsValid(const PageId &page) const;
44
45 /// total shared memory size required to share
46 static size_t SharedMemorySize(const uint32_t aPoolId, const unsigned int capacity, const size_t pageSize);
47 size_t sharedMemorySize() const;
be17aa82 48
551f8a18
DK
49 /// shared memory size required only by PageStack, excluding
50 /// shared counters and page data
51 static size_t StackSize(const unsigned int capacity);
52 size_t stackSize() const;
53
be17aa82 54private:
e963b5b5
AR
55 /// stack index and size type (may temporary go negative)
56 typedef int Offset;
be17aa82 57
68353d5a
DK
58 // these help iterate the stack in search of a free spot or a page
59 Offset next(const Offset idx) const { return (idx + 1) % theCapacity; }
60 Offset prev(const Offset idx) const { return (theCapacity + idx - 1) % theCapacity; }
be17aa82 61
68353d5a
DK
62 const uint32_t thePoolId; ///< pool ID
63 const Offset theCapacity; ///< stack capacity, i.e. theItems size
64 const size_t thePageSize; ///< page size, used to calculate shared memory size
65 /// lower bound for the number of free pages (may get negative!)
794d4c0c 66 Atomic::WordT<Offset> theSize;
be17aa82 67
68353d5a 68 /// last readable item index; just a hint, not a guarantee
794d4c0c 69 Atomic::WordT<Offset> theLastReadable;
68353d5a 70 /// first writable item index; just a hint, not a guarantee
794d4c0c 71 Atomic::WordT<Offset> theFirstWritable;
be17aa82 72
794d4c0c 73 typedef Atomic::WordT<Value> Item;
3a8c5551 74 Ipc::Mem::FlexibleArray<Item> theItems; ///< page number storage
be17aa82
AR
75};
76
77} // namespace Mem
78
79} // namespace Ipc
80
81#endif // SQUID_IPC_MEM_PAGE_STACK_H
f53969cc 82