MongoDB源码:SharedBuffer

作者: 江海小流 | 来源:发表于2020-11-22 22:21 被阅读0次

    相关的类:

    • BSONObjectBuilder
    • BufBuilder
    • SharedBufferAllocator

    SharedBufferAllocator 基于 SharedBuffer 提供 Allocator的功能,那么SharedBuffer是如何实现的呢?
    首先,我们先看一下SharedBuffer的成员变量:

    class SharedBuffer {
        // ...
    private:
        boost::intrusive_ptr<Holder> _holder;
    };
    

    能够发现 SharedBuffer 使用了一个指向 Holder 的 boost::intrusive_ptr ,Holder 顾名思义用途是存储,因此我们先看一下Holder 是怎么实现。

    Holder 的成员变量如下:

    class Holder {
    public:
        explicit Holder(unsigned initial, size_t capacity)
            : _refCount(initial), _capacity(capacity) {
                invariant(capacity == _capacity);
            }
    
        AtomicWord<unsigned> _refCount;
        uint32_t _capacity;
    };
    

    Holder 使用AtomicWord<unsigned> _refCount 实现引用计数,使用 uint32_t _capacity 存储空间的长度,此外,指向 Holder 的指针是 boost::intrusive_ptr,因此 Holder 需要实现 intrusive_ptr_add_ref 和 intrusive_ptr_release 两个函数如下:

    // these are called automatically by boost::intrusive_ptr
    friend void intrusive_ptr_add_ref(Holder* h) {
        h->_refCount.fetchAndAdd(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);
        }
    }
    

    但是,Holder不是存储一段空间吗,那么存储的空间在哪里呢?Holder 与存储空间相关的函数如下:

    char* data() {
        return reinterpret_cast<char*>(this + 1);
    }
    
    const char* data() const {
        return reinterpret_cast<const char*>(this + 1);
    }
    

    data() 返回数据使用 this+1 的指针类型转换,为什么可以这样实现?观察到 Holder 的构造方法如下:

    static SharedBuffer takeOwnership(void* holderPrefixedData, size_t capacity) {
        // Initialize the refcount to 1 so we don't need to increment it in the constructor
        // (see private Holder* constructor above).
        //
        // TODO: Should dassert alignment of holderPrefixedData here if possible.
        return SharedBuffer(new (holderPrefixedData) Holder(1U, capacity));
    }
    

    可以发现,holderPrefixedData 指向的内存空间除了为Holder实例分配的空间外,还有 capacity 大小的bytes空间。而 this 类型为 Holder,因此 this + 1 会使得指针偏移 sizeof(Holder),从而指向未使用的空间,这样Holder就达到了存储一段空间的目的。
    基于Holder,SharedBuffer对外提供创建一个固定大小的Buffer,类似CopyOnWrite的CopyOnRealloc的功能的接口,具体实现如下:

    static SharedBuffer allocate(size_t bytes) {
        return takeOwnership(mongoMalloc(sizeof(Holder) + bytes), bytes);
    }
    
    /**
    * Resizes the buffer, copying the current contents.
    *
    * Like ::realloc() this can be called on a null SharedBuffer.
    *
    * This method is illegal to call if any other SharedBuffer instances share this buffer since
    * they wouldn't be updated and would still try to delete the original buffer.
    */
    void realloc(size_t size) {
        invariant(!_holder || !_holder->isShared());
    
        const size_t realSize = size + sizeof(Holder);
        void* newPtr = mongoRealloc(_holder.get(), realSize);
    
        // Get newPtr into _holder with a ref-count of 1 without touching the current pointee of
        // _holder which is now invalid.
        auto tmp = SharedBuffer::takeOwnership(newPtr, size);
        _holder.detach();
        _holder = std::move(tmp._holder);
    }
    
    /**
    * Resizes the buffer, copying the current contents. If shared, an exclusive copy is made.
    */
    void reallocOrCopy(size_t size) {
        if (isShared()) {
            auto tmp = SharedBuffer::allocate(size);
            memcpy(tmp._holder->data(),
                   _holder->data(),
                   std::min(size, static_cast<size_t>(_holder->_capacity)));
            swap(tmp);
        } else if (_holder) {
            realloc(size);
        } else {
            *this = SharedBuffer::allocate(size);
        }
    }
    
    char* get() const {
        return _holder ? _holder->data() : nullptr;
    }
    

    总的来说,SharedBuffer提供如下特性:

    • 创建一个固定大小的SharedBuffer;
    • 修改一个SharedBuffer的大小,如果该SharedBuffer的数据是共享的,则复制一份已有数据到新分配的内存空间上,并指向新分配的内存空间
    • 使用Holder和boost::intrusive_ptr确保不会发生内存泄漏
    • 复制 SharedBuffer 会使得分配的内存空间的引用计数增加

    相关文章

      网友评论

        本文标题:MongoDB源码:SharedBuffer

        本文链接:https://www.haomeiwen.com/subject/himpiktx.html