]> git.ipfire.org Git - thirdparty/squid.git/blame - src/MemBlob.h
Prep for 3.4.2
[thirdparty/squid.git] / src / MemBlob.h
CommitLineData
43d1bbe4 1/*
43d1bbe4
FC
2 * SQUID Web Proxy Cache http://www.squid-cache.org/
3 * ----------------------------------------------------------
4 *
5 * Squid is the result of efforts by numerous individuals from
6 * the Internet community; see the CONTRIBUTORS file for full
7 * details. Many organizations have provided support for Squid's
8 * development; see the SPONSORS file for full details. Squid is
9 * Copyrighted (C) 2001 by the Regents of the University of
10 * California; see the COPYRIGHT file for full details. Squid
11 * incorporates software developed and/or copyrighted by other
12 * sources; see the CREDITS file for full details.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 */
28
29#ifndef SQUID_MEMBLOB_H_
30#define SQUID_MEMBLOB_H_
31
32#define MEMBLOB_DEBUGSECTION 24
33
34#include "base/InstanceId.h"
8bf217bd 35#include "base/RefCount.h"
43d1bbe4 36#include "MemPool.h"
43d1bbe4
FC
37
38/// Various MemBlob class-wide statistics.
39class MemBlobStats
40{
41public:
42 MemBlobStats();
43
44 /// dumps class-wide statistics
45 std::ostream& dump(std::ostream& os) const;
46
412da427
FC
47 MemBlobStats& operator += (const MemBlobStats&);
48
43d1bbe4 49public:
b6aa37ec
FC
50 uint64_t alloc; ///< number of MemBlob instances created so far
51 uint64_t live; ///< number of MemBlob instances currently alive
52 uint64_t append; ///< number of MemBlob::append() calls
53 uint64_t liveBytes; ///< the total size of currently allocated storage
43d1bbe4
FC
54};
55
43d1bbe4
FC
56/** Refcountable, fixed-size, content-agnostic memory buffer.
57 *
b59e6847 58 * Allocated memory block is divided into two sequential areas:
43d1bbe4
FC
59 * "used memory" and "available space". The used area can be filled during
60 * construction, grows via the append() call, and can be clear()ed.
61 *
62 * MemBlob users can cooperate to safely share the used area. However, MemBlob
63 * provides weak use accounting and no sharing protections besides refcounting.
64 */
65class MemBlob: public RefCountable
66{
67public:
68 typedef RefCount<MemBlob> Pointer;
50827187 69 typedef uint32_t size_type;
43d1bbe4
FC
70
71 MEMPROXY_CLASS(MemBlob);
72
73 /// obtain a const view of class-wide statistics
74 static const MemBlobStats& GetStats();
75
76 /// create a new MemBlob with at least reserveSize capacity
77 explicit MemBlob(const size_type reserveSize);
78
79 /// create a MemBlob containing a copy of the buffer of a given size
80 MemBlob(const char *buffer, const size_type bufferSize);
81
82 virtual ~MemBlob();
83
84 /// the number unused bytes at the end of the allocated blob
85 size_type spaceSize() const { return capacity - size; }
86
87 /** check whether the caller can successfully append() n bytes
88 *
89 * \return true the caller may append() n bytes to this blob now
90 * \param off the end of the blob area currently used by the caller
91 * \param n the total number of bytes the caller wants to append
92 */
b59e6847 93 bool canAppend(const size_type off, const size_type n) const {
43d1bbe4
FC
94 // TODO: ignore offset (and adjust size) when the blob is not shared?
95 return isAppendOffset(off) && willFit(n);
96 }
97
98 /** copies exactly n bytes from the source to the available space area,
99 * enlarging the used area by n bytes
100 *
101 * \throw TextException if there is not enough space in the blob
102 * \param source raw buffer to be copied
103 * \param n the number of bytes to copy from the source buffer
104 */
105 void append(const char *source, const size_type n);
106
107 /// extends the available space to the entire allocated blob
108 void clear() { size = 0; }
109
110 /// dump debugging information
111 std::ostream & dump(std::ostream &os) const;
112
113public:
114 /* MemBlob users should avoid these and must treat them as read-only */
115 char *mem; ///< raw allocated memory block
116 size_type capacity; ///< size of the raw allocated memory block
117 size_type size; ///< maximum allocated memory in use by callers
118 const InstanceId<MemBlob> id; ///< blob identifier
119
120private:
121 static MemBlobStats Stats; ///< class-wide statistics
122
123 void memAlloc(const size_type memSize);
43d1bbe4
FC
124
125 /// whether the offset points to the end of the used area
126 bool isAppendOffset(const size_type off) const { return off == size; }
127
128 /// whether n more bytes can be appended
129 bool willFit(const size_type n) const { return n <= spaceSize(); }
130
131 /* copying is not implemented */
132 MemBlob(const MemBlob &);
133 MemBlob& operator =(const MemBlob &);
134};
135
136MEMPROXY_CLASS_INLINE(MemBlob);
137
138#endif /* SQUID_MEMBLOB_H_ */