summaryrefslogtreecommitdiff
path: root/src/mongo/util/shared_buffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/util/shared_buffer.h')
-rw-r--r--src/mongo/util/shared_buffer.h144
1 files changed, 71 insertions, 73 deletions
diff --git a/src/mongo/util/shared_buffer.h b/src/mongo/util/shared_buffer.h
index af21b19d615..a8fd9a27b3e 100644
--- a/src/mongo/util/shared_buffer.h
+++ b/src/mongo/util/shared_buffer.h
@@ -34,95 +34,93 @@
namespace mongo {
- class SharedBuffer {
- public:
- SharedBuffer() = default;
-
- void swap(SharedBuffer& other) {
- _holder.swap(other._holder);
- }
-
- SharedBuffer(const SharedBuffer&) = default;
- SharedBuffer& operator=(const SharedBuffer&) = default;
+class SharedBuffer {
+public:
+ SharedBuffer() = default;
- SharedBuffer(SharedBuffer&& other)
- : _holder() {
- swap(other);
- }
+ void swap(SharedBuffer& other) {
+ _holder.swap(other._holder);
+ }
- SharedBuffer& operator=(SharedBuffer&& other) {
- swap(other);
- other._holder.reset();
- return *this;
- }
+ SharedBuffer(const SharedBuffer&) = default;
+ SharedBuffer& operator=(const SharedBuffer&) = default;
- static SharedBuffer allocate(size_t bytes) {
- return takeOwnership(static_cast<char*>(malloc(sizeof(Holder) + bytes)));
- }
+ SharedBuffer(SharedBuffer&& other) : _holder() {
+ swap(other);
+ }
- /**
- * Given a pointer to a region of un-owned data, prefixed by sufficient space for a
- * SharedBuffer::Holder object, return an SharedBuffer that owns the
- * memory.
- *
- * This class will call free(holderPrefixedData), so it must have been allocated in a way
- * that makes that valid.
- */
- static SharedBuffer takeOwnership(char* holderPrefixedData) {
- // Initialize the refcount to 1 so we don't need to increment it in the constructor
- // (see private Holder* constructor below).
- //
- // TODO: Should dassert alignment of holderPrefixedData
- // here if possible.
- return SharedBuffer(new(holderPrefixedData) Holder(1U));
- }
+ SharedBuffer& operator=(SharedBuffer&& other) {
+ swap(other);
+ other._holder.reset();
+ return *this;
+ }
- char* get() const {
- return _holder ? _holder->data() : NULL;
- }
+ static SharedBuffer allocate(size_t bytes) {
+ return takeOwnership(static_cast<char*>(malloc(sizeof(Holder) + bytes)));
+ }
- class Holder {
- public:
- explicit Holder(AtomicUInt32::WordType initial = AtomicUInt32::WordType())
- : _refCount(initial) {}
+ /**
+ * Given a pointer to a region of un-owned data, prefixed by sufficient space for a
+ * SharedBuffer::Holder object, return an SharedBuffer that owns the
+ * memory.
+ *
+ * This class will call free(holderPrefixedData), so it must have been allocated in a way
+ * that makes that valid.
+ */
+ static SharedBuffer takeOwnership(char* holderPrefixedData) {
+ // Initialize the refcount to 1 so we don't need to increment it in the constructor
+ // (see private Holder* constructor below).
+ //
+ // TODO: Should dassert alignment of holderPrefixedData
+ // here if possible.
+ return SharedBuffer(new (holderPrefixedData) Holder(1U));
+ }
- // these are called automatically by boost::intrusive_ptr
- friend void intrusive_ptr_add_ref(Holder* h) {
- h->_refCount.fetchAndAdd(1);
- }
+ char* get() const {
+ return _holder ? _holder->data() : NULL;
+ }
- friend void intrusive_ptr_release(Holder* h) {
- if (h->_refCount.subtractAndFetch(1) == 0) {
- // We placement new'ed a Holder in takeOwnership above,
- // so we must destroy the object here.
- h->~Holder();
- free(h);
- }
- }
+ class Holder {
+ public:
+ explicit Holder(AtomicUInt32::WordType initial = AtomicUInt32::WordType())
+ : _refCount(initial) {}
- char* data() {
- return reinterpret_cast<char *>(this + 1);
- }
+ // these are called automatically by boost::intrusive_ptr
+ friend void intrusive_ptr_add_ref(Holder* h) {
+ h->_refCount.fetchAndAdd(1);
+ }
- const char* data() const {
- return reinterpret_cast<const char *>(this + 1);
+ friend void intrusive_ptr_release(Holder* h) {
+ if (h->_refCount.subtractAndFetch(1) == 0) {
+ // We placement new'ed a Holder in takeOwnership above,
+ // so we must destroy the object here.
+ h->~Holder();
+ free(h);
}
+ }
- private:
- AtomicUInt32 _refCount;
- };
+ char* data() {
+ return reinterpret_cast<char*>(this + 1);
+ }
- private:
- explicit SharedBuffer(Holder* holder)
- : _holder(holder, /*add_ref=*/ false) {
- // NOTE: The 'false' above is because we have already initialized the Holder with a
- // refcount of '1' in takeOwnership above. This avoids an atomic increment.
+ const char* data() const {
+ return reinterpret_cast<const char*>(this + 1);
}
- boost::intrusive_ptr<Holder> _holder;
+ private:
+ AtomicUInt32 _refCount;
};
- inline void swap(SharedBuffer& one, SharedBuffer& two) {
- one.swap(two);
+private:
+ explicit SharedBuffer(Holder* holder) : _holder(holder, /*add_ref=*/false) {
+ // NOTE: The 'false' above is because we have already initialized the Holder with a
+ // refcount of '1' in takeOwnership above. This avoids an atomic increment.
}
+
+ boost::intrusive_ptr<Holder> _holder;
+};
+
+inline void swap(SharedBuffer& one, SharedBuffer& two) {
+ one.swap(two);
+}
}