PageStack.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
7  */
8 
9 #ifndef SQUID_IPC_MEM_PAGE_STACK_H
10 #define SQUID_IPC_MEM_PAGE_STACK_H
11 
12 #include "ipc/mem/FlexibleArray.h"
13 
14 #include <atomic>
15 
16 namespace Ipc
17 {
18 
19 namespace Mem
20 {
21 
22 class PageId;
23 
27 class PageStack
28 {
29 public:
30  typedef uint32_t Value;
31 
32  PageStack(const uint32_t aPoolId, const unsigned int aCapacity, const size_t aPageSize);
33 
34  unsigned int capacity() const { return theCapacity; }
35  size_t pageSize() const { return thePageSize; }
37  unsigned int size() const { return max(0, theSize.load()); }
38 
40  bool pop(PageId &page);
42  void push(PageId &page);
43 
44  bool pageIdIsValid(const PageId &page) const;
45 
47  static size_t SharedMemorySize(const uint32_t aPoolId, const unsigned int capacity, const size_t pageSize);
48  size_t sharedMemorySize() const;
49 
52  static size_t StackSize(const unsigned int capacity);
53  size_t stackSize() const;
54 
55 private:
57  typedef int Offset;
58 
59  // these help iterate the stack in search of a free spot or a page
60  Offset next(const Offset idx) const { return (idx + 1) % theCapacity; }
61  Offset prev(const Offset idx) const { return (theCapacity + idx - 1) % theCapacity; }
62 
63  const uint32_t thePoolId;
65  const size_t thePageSize;
66  std::atomic<Offset> theSize;
68 
70  std::atomic<Offset> theLastReadable;
72  std::atomic<Offset> theFirstWritable;
73 
74  typedef std::atomic<Value> Item;
76 };
77 
78 } // namespace Mem
79 
80 } // namespace Ipc
81 
82 #endif // SQUID_IPC_MEM_PAGE_STACK_H
83 
std::atomic< Offset > theFirstWritable
first writable item index; just a hint, not a guarantee
Definition: PageStack.h:72
void push(PageId &page)
makes value available as a free page number to future pop() callers
Definition: PageStack.cc:76
std::atomic< Offset > theLastReadable
last readable item index; just a hint, not a guarantee
Definition: PageStack.h:70
static size_t StackSize(const unsigned int capacity)
Definition: PageStack.cc:131
const uint32_t thePoolId
pool ID
Definition: PageStack.h:63
std::atomic< Value > Item
Definition: PageStack.h:74
A const & max(A const &lhs, A const &rhs)
size_t sharedMemorySize() const
Definition: PageStack.cc:117
size_t pageSize() const
Definition: PageStack.h:35
unsigned int capacity() const
Definition: PageStack.h:34
bool pop(PageId &page)
sets value and returns true unless no free page numbers are found
Definition: PageStack.cc:40
const Offset theCapacity
stack capacity, i.e. theItems size
Definition: PageStack.h:64
Ipc::Mem::FlexibleArray< Item > theItems
page number storage
Definition: PageStack.h:75
unsigned int size() const
lower bound for the number of free pages
Definition: PageStack.h:37
int Offset
stack index and size type (may temporary go negative)
Definition: PageStack.h:57
static size_t SharedMemorySize(const uint32_t aPoolId, const unsigned int capacity, const size_t pageSize)
total shared memory size required to share
Definition: PageStack.cc:123
PageStack(const uint32_t aPoolId, const unsigned int aCapacity, const size_t aPageSize)
Definition: PageStack.cc:21
size_t stackSize() const
Definition: PageStack.cc:137
std::atomic< Offset > theSize
lower bound for the number of free pages (may get negative!)
Definition: PageStack.h:67
Shared memory page identifier, address, or handler.
Definition: Page.h:21
const size_t thePageSize
Definition: PageStack.h:65
bool pageIdIsValid(const PageId &page) const
Definition: PageStack.cc:110
uint32_t Value
stack item type (a free page number)
Definition: PageStack.h:30
Offset next(const Offset idx) const
Definition: PageStack.h:60
Offset prev(const Offset idx) const
Definition: PageStack.h:61

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors