-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More progress on file round-trips #23
Changes from all commits
3f6f3ed
3b11218
70ac170
229b82c
41ee7b1
69dd557
91c730b
1ae9bad
9e6ad34
3c98d91
f3978ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2016 Feather Developers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "feather/buffer.h" | ||
|
||
namespace feather { | ||
|
||
Buffer::Buffer(const std::shared_ptr<Buffer>& parent, int64_t offset, | ||
int64_t size) { | ||
data_ = parent->data() + offset; | ||
size_ = size; | ||
parent_ = parent; | ||
} | ||
|
||
std::shared_ptr<Buffer> MutableBuffer::GetImmutableView() { | ||
return std::make_shared<Buffer>(this->get_shared_ptr(), 0, size()); | ||
} | ||
|
||
OwnedMutableBuffer::OwnedMutableBuffer(int64_t size) { | ||
Resize(size); | ||
} | ||
|
||
void OwnedMutableBuffer::Resize(int64_t new_size) { | ||
size_ = new_size; | ||
buffer_owner_.resize(new_size); | ||
data_ = buffer_owner_.data(); | ||
mutable_data_ = buffer_owner_.data(); | ||
} | ||
|
||
} // namespace feather |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2016 Feather Developers | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef FEATHER_BUFFER_H | ||
#define FEATHER_BUFFER_H | ||
|
||
#include <cstdint> | ||
#include <cstdlib> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace feather { | ||
|
||
// ---------------------------------------------------------------------- | ||
// Buffer classes | ||
|
||
// Immutable API for a chunk of bytes which may or may not be owned by the | ||
// class instance | ||
class Buffer : public std::enable_shared_from_this<Buffer> { | ||
public: | ||
Buffer(const uint8_t* data, int64_t size) : | ||
data_(data), | ||
size_(size) {} | ||
|
||
// An offset into data that is owned by another buffer, but we want to be | ||
// able to retain a valid pointer to it even after other shared_ptr's to the | ||
// parent buffer have been destroyed | ||
Buffer(const std::shared_ptr<Buffer>& parent, int64_t offset, int64_t size); | ||
|
||
std::shared_ptr<Buffer> get_shared_ptr() { | ||
return shared_from_this(); | ||
} | ||
|
||
const uint8_t* data() const { | ||
return data_; | ||
} | ||
|
||
int64_t size() const { | ||
return size_; | ||
} | ||
|
||
// Returns true if this Buffer is referencing memory (possibly) owned by some | ||
// other buffer | ||
bool is_shared() const { | ||
return static_cast<bool>(parent_); | ||
} | ||
|
||
const std::shared_ptr<Buffer> parent() const { | ||
return parent_; | ||
} | ||
|
||
protected: | ||
const uint8_t* data_; | ||
int64_t size_; | ||
|
||
// nullptr by default, but may be set | ||
std::shared_ptr<Buffer> parent_; | ||
}; | ||
|
||
// A Buffer whose contents can be mutated | ||
class MutableBuffer : public Buffer { | ||
public: | ||
MutableBuffer(uint8_t* data, int64_t size) : | ||
Buffer(data, size) { | ||
mutable_data_ = data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me who ends up owning the data for this class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No owner indicated here, only that you can mutate the buffer. I can add more detailed comments |
||
} | ||
|
||
uint8_t* mutable_data() { | ||
return mutable_data_; | ||
} | ||
|
||
// Get a read-only view of this buffer | ||
std::shared_ptr<Buffer> GetImmutableView(); | ||
|
||
protected: | ||
MutableBuffer() : | ||
Buffer(nullptr, 0), | ||
mutable_data_(nullptr) {} | ||
|
||
uint8_t* mutable_data_; | ||
}; | ||
|
||
// A MutableBuffer whose memory is owned by the class instance. For example, | ||
// for reading data out of files that you want to deallocate when this class is | ||
// garbage-collected | ||
class OwnedMutableBuffer : public MutableBuffer { | ||
public: | ||
explicit OwnedMutableBuffer(int64_t size); | ||
void Resize(int64_t new_size); | ||
|
||
private: | ||
std::vector<uint8_t> buffer_owner_; | ||
}; | ||
|
||
} // namespace feather | ||
|
||
#endif // FEATHER_BUFFER_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should that just be a
std::shared_ptr<Buffer>
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, otherwise you end up copying the passed argument twice (once on pass-by-value, then again when assigning to
Buffer::parent_
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but why do you care about that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, copying a shared pointer is not totally trivial, since there are C++11 atomics involved with managing the internal reference count.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://msdn.microsoft.com/en-us/library/hh279669.aspx
"Pass the shared_ptr by reference or const reference. In this case, the reference count is not incremented, and the callee can access the pointer as long as the caller does not go out of scope. Or, the callee can decide to create a shared_ptr based on the reference, and thereby become a shared owner. Use this option when the caller has no knowledge of the callee, or when you must pass a shared_ptr and want to avoid the copy operation for performance reasons."