]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/PagePool.h
Merged from parent (trunk r11387, v3.2.0.7+).
[thirdparty/squid.git] / src / ipc / mem / PagePool.h
1 /*
2 * $Id$
3 *
4 */
5
6 #ifndef SQUID_IPC_MEM_PAGE_POOL_H
7 #define SQUID_IPC_MEM_PAGE_POOL_H
8
9 #include "ipc/mem/PageStack.h"
10 #include "ipc/mem/Segment.h"
11
12 namespace Ipc {
13
14 namespace Mem {
15
16 class PageId;
17
18 /// Atomic container of shared memory pages. Implemented using a collection of
19 /// Segments, each with a PageStack index of free pages. All pools must be
20 /// created by a single process.
21 class PagePool {
22 public:
23 /// creates a new shared page pool that can hold up to capacity pages of pageSize size
24 PagePool(const String &id, const unsigned int capacity, const size_t pageSize);
25 /// attaches to the identified shared page pool
26 PagePool(const String &id);
27 /// unlinks shared memory segments
28 static void Unlink(const String &id);
29
30 unsigned int capacity() const { return shared->theCapacity; }
31 /// lower bound for the number of free pages
32 unsigned int size() const { return pageIndex.size(); }
33 size_t pageSize() const { return shared->thePageSize; }
34
35 /// sets page ID and returns true unless no free pages are found
36 bool get(PageId &page);
37 /// makes identified page available as a free page to future get() callers
38 void put(PageId &page);
39 /// converts page handler into a temporary writeable shared memory pointer
40 void *pagePointer(const PageId &page);
41
42 private:
43 inline bool pageIdIsValid(const PageId &page) const;
44
45 struct Shared {
46 Shared(const unsigned int aCapacity, const size_t aPageSize);
47
48 /// total shared memory size required to share
49 static off_t MemSize(const unsigned int capacity, const size_t pageSize);
50
51 const unsigned int theId; ///< pool id
52 const unsigned int theCapacity; ///< number of pages in the pool
53 const size_t thePageSize; ///< page size
54
55 // TODO: add padding to make pages system page-aligned?
56 char theBuf[]; ///< pages storage
57 };
58
59 PageStack pageIndex; ///< free pages index
60 Segment shm; ///< shared memory segment to store metadata (and pages)
61 Shared *shared; ///< our metadata and page storage, shared among all pool users
62 };
63
64 } // namespace Mem
65
66 } // namespace Ipc
67
68 #endif // SQUID_IPC_MEM_PAGE_POOL_H