1 /*
2  * Copyright (C) 1996-2021 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 /* DEBUG: section 19    Store Memory Primitives */
10 
11 #include "squid.h"
12 #include "mem/Pool.h"
13 #include "mem_node.h"
14 
15 static ptrdiff_t makeMemNodeDataOffset();
16 
17 static ptrdiff_t _mem_node_data_offset = makeMemNodeDataOffset();
18 
19 /*
20  * Calculate the offset between the start of a mem_node and
21  * its 'data' member
22  */
23 static ptrdiff_t
makeMemNodeDataOffset()24 makeMemNodeDataOffset()
25 {
26     mem_node *p = 0L;
27     return ptrdiff_t(&p->data);
28 }
29 
30 /*
31  * This is the callback when storeIOWrite() is done.  We need to
32  * clear the write_pending flag for the mem_node.  First we have
33  * to calculate the start of the mem_node based on the character
34  * buffer that we wrote.  ick.
35  */
36 void
memNodeWriteComplete(void * d)37 memNodeWriteComplete(void* d)
38 {
39     mem_node* n = (mem_node*)((char*)d - _mem_node_data_offset);
40     assert(n->write_pending);
41     n->write_pending = false;
42 }
43 
mem_node(int64_t offset)44 mem_node::mem_node(int64_t offset) :
45     nodeBuffer(0,offset,data),
46     write_pending(false)
47 {
48     *data = 0;
49 }
50 
~mem_node()51 mem_node::~mem_node()
52 {}
53 
54 size_t
InUseCount()55 mem_node::InUseCount()
56 {
57     return Pool().inUseCount();
58 }
59 
60 size_t
StoreMemSize()61 mem_node::StoreMemSize()
62 {
63     return InUseCount() * SM_PAGE_SIZE;
64 }
65 
66 int64_t
start() const67 mem_node::start() const
68 {
69     assert (nodeBuffer.offset >= 0);
70     return nodeBuffer.offset;
71 }
72 
73 int64_t
end() const74 mem_node::end() const
75 {
76     return nodeBuffer.offset + nodeBuffer.length;
77 }
78 
79 Range<int64_t>
dataRange() const80 mem_node::dataRange() const
81 {
82     return Range<int64_t> (start(), end());
83 }
84 
85 size_t
space() const86 mem_node::space() const
87 {
88     return SM_PAGE_SIZE - nodeBuffer.length;
89 }
90 
91 bool
contains(int64_t const & location) const92 mem_node::contains (int64_t const &location) const
93 {
94     if (start() <= location && end() > location)
95         return true;
96 
97     return false;
98 }
99 
100 /* nodes can not be sparse */
101 bool
canAccept(int64_t const & location) const102 mem_node::canAccept (int64_t const &location) const
103 {
104     if (location == end() && space() > 0)
105         return true;
106 
107     return false;
108 }
109 
110 bool
operator <(mem_node const & rhs) const111 mem_node::operator < (mem_node const & rhs) const
112 {
113     return start() < rhs.start();
114 }
115 
116