Skip to content

Commit c4305a1

Browse files
committed
fix: queue element handling
1 parent ee5d479 commit c4305a1

1 file changed

Lines changed: 24 additions & 20 deletions

File tree

packages/react-native-audio-api/common/cpp/audioapi/utils/BoundedPriorityQueue.hpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ class BoundedPriorityQueue {
1919
public:
2020
explicit BoundedPriorityQueue() : size_(0) {
2121
static_assert(isPowerOfTwo(capacity_), "BoundedPriorityQueue's capacity must be a power of 2");
22-
buffer_ = static_cast<T *>(
23-
::operator new[](capacity_ * sizeof(T), static_cast<std::align_val_t>(alignof(T))));
22+
buffer_ = static_cast<TimestampedElement *>(::operator new[](
23+
capacity_ * sizeof(TimestampedElement),
24+
static_cast<std::align_val_t>(alignof(TimestampedElement))));
2425
}
2526

2627
~BoundedPriorityQueue() {
2728
for (size_t i = 0; i < size_; ++i) {
28-
buffer_[i].~T();
29+
buffer_[i].data.~T();
2930
}
30-
::operator delete[](buffer_, capacity_ * sizeof(T), static_cast<std::align_val_t>(alignof(T)));
31+
::operator delete[](
32+
buffer_,
33+
capacity_ * sizeof(TimestampedElement),
34+
static_cast<std::align_val_t>(alignof(TimestampedElement)));
3135
}
3236

3337
BoundedPriorityQueue(const BoundedPriorityQueue &) = delete;
@@ -56,12 +60,12 @@ class BoundedPriorityQueue {
5660
if (isEmpty()) [[unlikely]] {
5761
return false;
5862
}
59-
out = std::move(buffer_[0]);
60-
buffer_[0].~T();
63+
out = std::move(buffer_[0].data);
64+
buffer_[0].~TimestampedElement();
6165
--size_;
6266
if (size_ > 0) {
63-
new (&buffer_[0]) T(std::move(buffer_[size_]));
64-
buffer_[size_].~T();
67+
new (&buffer_[0]) TimestampedElement(std::move(buffer_[size_]));
68+
buffer_[size_].~TimestampedElement();
6569
siftDown(0);
6670
}
6771
return true;
@@ -73,11 +77,11 @@ class BoundedPriorityQueue {
7377
if (isEmpty()) [[unlikely]] {
7478
return false;
7579
}
76-
buffer_[0].~T();
80+
buffer_[0].~TimestampedElement();
7781
--size_;
7882
if (size_ > 0) {
79-
new (&buffer_[0]) T(std::move(buffer_[size_]));
80-
buffer_[size_].~T();
83+
new (&buffer_[0]) TimestampedElement(std::move(buffer_[size_]));
84+
buffer_[size_].~TimestampedElement();
8185
siftDown(0);
8286
}
8387
return true;
@@ -86,25 +90,25 @@ class BoundedPriorityQueue {
8690
/// @brief Peek at the top (highest priority) element without removing it.
8791
/// @return A const reference to the top element.
8892
[[nodiscard]] inline const T &peekFront() const noexcept {
89-
return buffer_[0];
93+
return buffer_[0].data;
9094
}
9195

9296
/// @brief Peek at the last (lowest priority) element without removing it.
9397
/// @return A reference to the last element.
9498
[[nodiscard]] inline T &peekFrontMut() noexcept {
95-
return buffer_[size_ - 1];
99+
return buffer_[size_ - 1].data;
96100
}
97101

98102
/// @brief Peek at the last (lowest priority) element without removing it.
99103
/// @return A reference to the last element.
100104
[[nodiscard]] inline const T &peekBack() const noexcept {
101-
return buffer_[size_ - 1];
105+
return buffer_[size_ - 1].data;
102106
}
103107

104108
/// @brief Peek at the last (lowest priority) element without removing it.
105109
/// @return A reference to the last element.
106110
[[nodiscard]] inline T &peekBackMut() noexcept {
107-
return buffer_[size_ - 1];
111+
return buffer_[size_ - 1].data;
108112
}
109113

110114
/// @brief Check if the queue is empty.
@@ -200,11 +204,11 @@ class BoundedPriorityQueue {
200204
}
201205

202206
void swapAt(size_t a, size_t b) noexcept {
203-
T tmp(std::move(buffer_[a]));
204-
buffer_[a].~T();
205-
new (&buffer_[a]) T(std::move(buffer_[b]));
206-
buffer_[b].~T();
207-
new (&buffer_[b]) T(std::move(tmp));
207+
TimestampedElement tmp(std::move(buffer_[a]));
208+
buffer_[a].~TimestampedElement();
209+
new (&buffer_[a]) TimestampedElement(std::move(buffer_[b]));
210+
buffer_[b].~TimestampedElement();
211+
new (&buffer_[b]) TimestampedElement(std::move(tmp));
208212
}
209213
};
210214

0 commit comments

Comments
 (0)