MemBlob.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_MEMBLOB_H_
10 #define SQUID_MEMBLOB_H_
11 
12 #define MEMBLOB_DEBUGSECTION 24
13 
14 #include "base/InstanceId.h"
15 #include "base/RefCount.h"
16 #include "mem/forward.h"
17 
20 {
21 public:
22  MemBlobStats();
23 
25  std::ostream& dump(std::ostream& os) const;
26 
28 
29 public:
30  uint64_t alloc;
31  uint64_t live;
32  uint64_t append;
33  uint64_t liveBytes;
34 };
35 
45 class MemBlob: public RefCountable
46 {
48 
49 public:
51  typedef uint32_t size_type;
52 
54  static const MemBlobStats& GetStats();
55 
57  explicit MemBlob(const size_type reserveSize);
58 
60  MemBlob(const char *buffer, const size_type bufferSize);
61 
62  virtual ~MemBlob();
63 
65  size_type spaceSize() const { return capacity - size; }
66 
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;
76  }
77 
83  void appended(const size_type n);
84 
92  void append(const char *source, const size_type n);
93 
95  void clear() { size = 0; }
96 
98  std::ostream & dump(std::ostream &os) const;
99 
100 public:
101  /* MemBlob users should avoid these and must treat them as read-only */
102  char *mem;
106 
107 private:
109 
110  void memAlloc(const size_type memSize);
111 
113  bool isAppendOffset(const size_type off) const { return off == size; }
114 
116  bool willFit(const size_type n) const { return n <= spaceSize(); }
117 
118  /* copying is not implemented */
119  MemBlob(const MemBlob &);
120  MemBlob& operator =(const MemBlob &);
121 };
122 
123 #endif /* SQUID_MEMBLOB_H_ */
124 
uint64_t alloc
number of MemBlob instances created so far
Definition: MemBlob.h:30
uint32_t size_type
Definition: MemBlob.h:51
uint64_t liveBytes
the total size of currently allocated storage
Definition: MemBlob.h:33
void appended(const size_type n)
Definition: MemBlob.cc:108
size_type capacity
size of the raw allocated memory block
Definition: MemBlob.h:103
static MemBlobStats Stats
class-wide statistics
Definition: MemBlob.h:108
bool canAppend(const size_type off, const size_type n) const
Definition: MemBlob.h:73
bool isAppendOffset(const size_type off) const
whether the offset points to the end of the used area
Definition: MemBlob.h:113
uint64_t live
number of MemBlob instances currently alive
Definition: MemBlob.h:31
MemBlobStats & operator+=(const MemBlobStats &)
Definition: MemBlob.cc:26
MemBlob & operator=(const MemBlob &)
RefCount< MemBlob > Pointer
Definition: MemBlob.h:50
static const MemBlobStats & GetStats()
obtain a const view of class-wide statistics
Definition: MemBlob.cc:128
MEMPROXY_CLASS(MemBlob)
bool willFit(const size_type n) const
whether n more bytes can be appended
Definition: MemBlob.h:116
MemBlob(const size_type reserveSize)
create a new MemBlob with at least reserveSize capacity
Definition: MemBlob.cc:51
std::ostream & dump(std::ostream &os) const
dump debugging information
Definition: MemBlob.cc:134
char * mem
raw allocated memory block
Definition: MemBlob.h:102
void memAlloc(const size_type memSize)
Definition: MemBlob.cc:89
std::ostream & dump(std::ostream &os) const
dumps class-wide statistics
Definition: MemBlob.cc:37
void append(const char *source, const size_type n)
Definition: MemBlob.cc:116
Various MemBlob class-wide statistics.
Definition: MemBlob.h:19
virtual ~MemBlob()
Definition: MemBlob.cc:71
uint64_t append
number of MemBlob::append() calls
Definition: MemBlob.h:32
size_type spaceSize() const
the number unused bytes at the end of the allocated blob
Definition: MemBlob.h:65
size_type size
maximum allocated memory in use by callers
Definition: MemBlob.h:104
const InstanceId< MemBlob > id
blob identifier
Definition: MemBlob.h:105
MemBlobStats()
Definition: MemBlob.cc:22
void clear()
extends the available space to the entire allocated blob
Definition: MemBlob.h:95

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors