]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/sbuf/MemBlob.h
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_MEMBLOB_H_
10 #define SQUID_MEMBLOB_H_
12 #define MEMBLOB_DEBUGSECTION 24
14 #include "base/InstanceId.h"
15 #include "base/RefCount.h"
16 #include "mem/forward.h"
18 /// Various MemBlob class-wide statistics.
24 /// dumps class-wide statistics
25 std::ostream
& dump(std::ostream
& os
) const;
27 MemBlobStats
& operator += (const MemBlobStats
&);
30 uint64_t alloc
; ///< number of MemBlob instances created so far
31 uint64_t live
; ///< number of MemBlob instances currently alive
32 uint64_t append
; ///< number of MemBlob::append() calls
33 uint64_t liveBytes
; ///< the total size of currently allocated storage
36 /** Refcountable, fixed-size, content-agnostic memory buffer.
38 * Allocated memory block is divided into two sequential areas:
39 * "used memory" and "available space". The used area can be filled during
40 * construction, grows via the append() call, and can be clear()ed.
42 * MemBlob users can cooperate to safely share the used area. However, MemBlob
43 * provides weak use accounting and no sharing protections besides refcounting.
45 class MemBlob
: public RefCountable
47 MEMPROXY_CLASS(MemBlob
);
50 typedef RefCount
<MemBlob
> Pointer
;
51 typedef uint32_t size_type
;
53 /// obtain a const view of class-wide statistics
54 static const MemBlobStats
& GetStats();
56 /// create a new MemBlob with at least reserveSize capacity
57 explicit MemBlob(const size_type reserveSize
);
59 /// create a MemBlob containing a copy of the buffer of a given size
60 MemBlob(const char *buffer
, const size_type bufferSize
);
64 /// the number unused bytes at the end of the allocated blob
65 size_type
spaceSize() const { return capacity
- size
; }
67 /** check whether the caller can successfully append() n bytes
69 * \return true the caller may append() n bytes to this blob now
70 * \param off the end of the blob area currently used by the caller
71 * \param n the total number of bytes the caller wants to append
73 bool canAppend(const size_type off
, const size_type n
) const {
74 // TODO: ignore offset (and adjust size) when the blob is not shared?
75 return (isAppendOffset(off
) && willFit(n
)) || !n
;
78 /** adjusts internal object state as if exactly n bytes were append()ed
80 * \throw TextException if there was not enough space in the blob
81 * \param n the number of bytes that were appended
83 void appended(const size_type n
);
85 /** copies exactly n bytes from the source to the available space area,
86 * enlarging the used area by n bytes
88 * \throw TextException if there is not enough space in the blob
89 * \param source raw buffer to be copied
90 * \param n the number of bytes to copy from the source buffer
92 void append(const char *source
, const size_type n
);
94 /// extends the available space to the entire allocated blob
95 void clear() { size
= 0; }
97 /// dump debugging information
98 std::ostream
& dump(std::ostream
&os
) const;
101 /* MemBlob users should avoid these and must treat them as read-only */
102 char *mem
; ///< raw allocated memory block
103 size_type capacity
; ///< size of the raw allocated memory block
104 size_type size
; ///< maximum allocated memory in use by callers
105 const InstanceId
<MemBlob
> id
; ///< blob identifier
108 static MemBlobStats Stats
; ///< class-wide statistics
110 void memAlloc(const size_type memSize
);
112 /// whether the offset points to the end of the used area
113 bool isAppendOffset(const size_type off
) const { return off
== size
; }
115 /// whether n more bytes can be appended
116 bool willFit(const size_type n
) const { return n
<= spaceSize(); }
118 /* copying is not implemented */
119 MemBlob(const MemBlob
&);
120 MemBlob
& operator =(const MemBlob
&);
123 #endif /* SQUID_MEMBLOB_H_ */