forked from microsoft/Multiverso
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblob.cpp
More file actions
47 lines (38 loc) · 1.02 KB
/
blob.cpp
File metadata and controls
47 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "multiverso/blob.h"
#include "multiverso/util/allocator.h"
#include "multiverso/util/log.h"
namespace multiverso {
Blob::Blob(size_t size) : size_(size) {
CHECK(size > 0);
data_ = Allocator::Get()->Alloc(size);
}
// Construct from external memory. Will copy a new piece
Blob::Blob(const void* data, size_t size) : size_(size) {
data_ = Allocator::Get()->Alloc(size);
memcpy(data_, data, size_);
}
Blob::Blob(void* data, size_t size) : size_(size) {
data_ = Allocator::Get()->Alloc(size);
memcpy(data_, data, size_);
}
Blob::Blob(const Blob& rhs) {
if (rhs.size() != 0) {
Allocator::Get()->Refer(rhs.data_);
}
this->data_ = rhs.data_;
this->size_ = rhs.size_;
}
Blob::~Blob() {
if (data_ != nullptr) {
Allocator::Get()->Free(data_);
}
}
// Shallow copy by default. Call \ref CopyFrom for a deep copy
void Blob::operator=(const Blob& rhs) {
if (rhs.size() != 0) {
Allocator::Get()->Refer(rhs.data_);
}
this->data_ = rhs.data_;
this->size_ = rhs.size_;
}
} // namespace multiverso