lichao
2021-03-26 a100bb2af8afc5fb61abc54e0b616cc44bb0b814
src/msg.cpp
@@ -32,4 +32,45 @@
    *static_cast<MsgMetaV1*>(p) = *this;
}
bool Msg::Build(SharedMemory &shm, const MQId &src_id, const void *data, const size_t size, const bool refcount)
{
    if (!data || !size) {
        return false;
    }
    void *p = shm.Alloc(sizeof(MsgMetaV1) + size);
    if (!p) {
        return false;
    }
    RefCount *rc = 0;
    if (refcount) {
        rc = shm.New<RefCount>();
        if (!rc) {
            shm.Dealloc(p);
            return false;
        }
    }
    MsgMetaV1 meta;
    meta.data_size_ = size;
    meta.src_id_ = src_id;
    meta.Pack(p);
    memcpy(static_cast<char *>(p) + sizeof(meta), data, size);
    Msg(p, rc).swap(*this);
    return true;
}
int Msg::Release(SharedMemory &shm) const
{
    if (IsCounted()) {
        const int n = count_->Dec();
        if (n != 0) {
            return n;
        }
    }
    // free data
    shm.Dealloc(ptr_);
    shm.Delete(count_);
    return 0;
}
} // namespace bhome_shm