Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
DEVOPS-353: Allow element count to be smaller than buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
lscheinkman committed Mar 6, 2018
1 parent c9377d5 commit 1e486fd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/nupic/ntypes/ArrayBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ using namespace nupic;
* Caller frees buffer when no longer needed.
*/
ArrayBase::ArrayBase(NTA_BasicType type, void *buffer, size_t count)
: buffer_((char *)buffer), count_(count), type_(type), own_(false) {
: buffer_((char *)buffer), count_(count), type_(type), own_(false),
bufferSize_(count) {
if (!BasicType::isValid(type)) {
NTA_THROW << "Invalid NTA_BasicType " << type
<< " used in array constructor";
Expand All @@ -53,7 +54,7 @@ ArrayBase::ArrayBase(NTA_BasicType type, void *buffer, size_t count)
* ask the ArrayBase to allocate a buffer via allocateBuffer.
*/
ArrayBase::ArrayBase(NTA_BasicType type)
: buffer_(nullptr), count_(0), type_(type), own_(false) {
: buffer_(nullptr), count_(0), type_(type), own_(false), bufferSize_(0) {
if (!BasicType::isValid(type)) {
NTA_THROW << "Invalid NTA_BasicType " << type
<< " used in array constructor";
Expand All @@ -80,7 +81,8 @@ void ArrayBase::allocateBuffer(size_t count) {
// a non-NULL value which is safe to delete. This allows us to
// disambiguate uninitialized ArrayBases and ArrayBases initialized with
// size zero.
buffer_ = new char[count_ * BasicType::getSize(type_)];
bufferSize_ = count_ * BasicType::getSize(type_);
buffer_ = new char[bufferSize_];
own_ = true;
}

Expand All @@ -91,6 +93,7 @@ void ArrayBase::setBuffer(void *buffer, size_t count) {
buffer_ = (char *)buffer;
count_ = count;
own_ = false;
bufferSize_ = count_ * BasicType::getSize(type_);
}

void ArrayBase::releaseBuffer() {
Expand All @@ -100,13 +103,24 @@ void ArrayBase::releaseBuffer() {
delete[] buffer_;
buffer_ = nullptr;
count_ = 0;
bufferSize_ = 0;
}

void *ArrayBase::getBuffer() const { return buffer_; }

size_t ArrayBase::getBufferSize() const { return bufferSize_; }

// number of elements of given type in the buffer
size_t ArrayBase::getCount() const { return count_; };

void ArrayBase::setCount(size_t count) {
NTA_CHECK(count * BasicType::getSize(type_) <= bufferSize_)
<< "Invalid count value of " << count << " given, "
<< "count must be " << bufferSize_ / BasicType::getSize(type_)
<< " or less";
count_ = count;
}

NTA_BasicType ArrayBase::getType() const { return type_; };

namespace nupic {
Expand Down
15 changes: 14 additions & 1 deletion src/nupic/ntypes/ArrayBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace nupic {
/**
* An ArrayBase is used for passing arrays of data back and forth between
* a client application and NuPIC, minimizing copying. It facilitates
* both zero-copy and one-copy operations.
* both zero-copy and one-copy operations. The array can be of variable length
* independent of buffer size. Array length cannot exceed buffer size.
*/
class ArrayBase {
public:
Expand Down Expand Up @@ -83,6 +84,17 @@ class ArrayBase {
// number of elements of given type in the buffer
size_t getCount() const;

/**
* Returns the allocated buffer size in bytes independent of array length
*/
size_t getBufferSize() const;

/**
* Set array length independent of buffer size. Array length cannot exceed
* buffer size
*/
void setCount(size_t count);

NTA_BasicType getType() const;

protected:
Expand All @@ -92,6 +104,7 @@ class ArrayBase {
size_t count_;
NTA_BasicType type_;
bool own_;
size_t bufferSize_;

private:
/**
Expand Down

0 comments on commit 1e486fd

Please sign in to comment.