Skip to content

Commit

Permalink
fix msvc, make index signed, add moving constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Nigmatullin committed May 31, 2023
1 parent a00630a commit 71cb32c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
22 changes: 13 additions & 9 deletions sdk/include/opentelemetry/sdk/metrics/data/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ class AdaptingIntegerArray
// Construct an adapting integer array of a given size.
explicit AdaptingIntegerArray(size_t size) : backing_(std::vector<uint8_t>(size, 0)) {}
AdaptingIntegerArray(const AdaptingIntegerArray &other) = default;
AdaptingIntegerArray(AdaptingIntegerArray &&other) = default;
AdaptingIntegerArray &operator=(const AdaptingIntegerArray &other) = default;
AdaptingIntegerArray &operator=(AdaptingIntegerArray &&other) = default;

/**
* Increments the value at the specified index by the given count in the array.
Expand Down Expand Up @@ -85,7 +87,9 @@ class AdaptingCircularBufferCounter
public:
explicit AdaptingCircularBufferCounter(size_t max_size) : backing_(max_size) {}
AdaptingCircularBufferCounter(const AdaptingCircularBufferCounter &other) = default;
AdaptingCircularBufferCounter(AdaptingCircularBufferCounter &&other) = default;
AdaptingCircularBufferCounter &operator=(const AdaptingCircularBufferCounter &other) = default;
AdaptingCircularBufferCounter &operator=(AdaptingCircularBufferCounter &&other) = default;

/**
* The first index with a recording. May be negative.
Expand All @@ -94,7 +98,7 @@ class AdaptingCircularBufferCounter
*
* @return the first index with a recording.
*/
size_t StartIndex() const { return start_index_; }
int32_t StartIndex() const { return start_index_; }

/**
* The last index with a recording. May be negative.
Expand All @@ -103,7 +107,7 @@ class AdaptingCircularBufferCounter
*
* @return The last index with a recording.
*/
size_t EndIndex() const { return end_index_; }
int32_t EndIndex() const { return end_index_; }

/**
* Returns true if no recordings, false if at least one recording.
Expand All @@ -125,23 +129,23 @@ class AdaptingCircularBufferCounter
* @param delta How much to increment the index by.
* @return success status.
*/
bool Increment(size_t index, uint64_t delta);
bool Increment(int32_t index, uint64_t delta);

/**
* Get the number of recordings for the given index.
*
* @return the number of recordings for the index, or 0 if the index is out of bounds.
*/
uint64_t Get(size_t index);
uint64_t Get(int32_t index);

private:
size_t ToBufferIndex(size_t index) const;
size_t ToBufferIndex(int32_t index) const;

static constexpr size_t kNullIndex = std::numeric_limits<size_t>::max();
static constexpr int32_t kNullIndex = std::numeric_limits<int32_t>::min();

size_t start_index_ = kNullIndex;
size_t end_index_ = kNullIndex;
size_t base_index_ = kNullIndex;
int32_t start_index_ = kNullIndex;
int32_t end_index_ = kNullIndex;
int32_t base_index_ = kNullIndex;
AdaptingIntegerArray backing_;
};

Expand Down
14 changes: 7 additions & 7 deletions sdk/src/metrics/data/circular_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct AdaptingIntegerArrayGet
struct AdaptingIntegerArraySize
{
template <typename T>
uint64_t operator()(const std::vector<T> &backing)
size_t operator()(const std::vector<T> &backing)
{
return backing.size();
}
Expand All @@ -55,7 +55,7 @@ struct AdaptingIntegerArrayClear
template <typename T>
void operator()(std::vector<T> &backing)
{
std::fill(backing.begin(), backing.end(), 0);
std::fill(backing.begin(), backing.end(), static_cast<T>(0));
}
};

Expand Down Expand Up @@ -124,7 +124,7 @@ void AdaptingCircularBufferCounter::Clear()
backing_.Clear();
}

bool AdaptingCircularBufferCounter::Increment(size_t index, uint64_t delta)
bool AdaptingCircularBufferCounter::Increment(int32_t index, uint64_t delta)
{
if (Empty())
{
Expand Down Expand Up @@ -157,7 +157,7 @@ bool AdaptingCircularBufferCounter::Increment(size_t index, uint64_t delta)
return true;
}

uint64_t AdaptingCircularBufferCounter::Get(size_t index)
uint64_t AdaptingCircularBufferCounter::Get(int32_t index)
{
if (index < start_index_ || index > end_index_)
{
Expand All @@ -166,15 +166,15 @@ uint64_t AdaptingCircularBufferCounter::Get(size_t index)
return backing_.Get(ToBufferIndex(index));
}

size_t AdaptingCircularBufferCounter::ToBufferIndex(size_t index) const
size_t AdaptingCircularBufferCounter::ToBufferIndex(int32_t index) const
{
// Figure out the index relative to the start of the circular buffer.
if (index < base_index_)
{
// If index is before the base one, wrap around.
return index + backing_.Size() - base_index_;
return static_cast<size_t>(index + backing_.Size() - base_index_);
}
return index - base_index_;
return static_cast<size_t>(index - base_index_);
}

} // namespace metrics
Expand Down

0 comments on commit 71cb32c

Please sign in to comment.