Skip to content

Commit eb70bac

Browse files
committed
Clean up ArduinoQueue
1 parent 0622746 commit eb70bac

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
* `release-new-version.sh` script
1111

1212
### Changed
13+
* Shortened `ArduinoQueue` push and pop operations
1314

1415
### Deprecated
1516

1617
### Removed
1718

1819
### Fixed
20+
* `ArduinoQueue` no longer leaks memory
1921

2022
### Security
2123

cpp/arduino/ci/Queue.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,26 @@ class ArduinoCIQueue {
2727
}
2828

2929
inline unsigned long size() const { return mSize; }
30-
3130
inline bool empty() const { return 0 == mSize; }
32-
3331
T front() const { return empty() ? mNil : mFront->data; }
34-
3532
T back() const { return empty() ? mNil : mBack->data; }
3633

3734
bool push(const T& v)
3835
{
3936
Node *n = new Node;
4037
if (n == NULL) return false;
41-
4238
n->data = v;
4339
n->next = NULL;
44-
45-
if (mFront == NULL)
46-
{
47-
mFront = mBack = n;
48-
} else {
49-
mBack->next = n;
50-
mBack = n;
51-
}
52-
53-
++mSize;
54-
return true;
40+
mBack = (mFront == NULL ? mFront : mBack->next) = n;
41+
return ++mSize;
5542
}
5643

5744
void pop() {
5845
if (empty()) return;
59-
if (mFront == mBack) {
60-
mFront = mBack = NULL;
61-
} else {
62-
Node* n = mFront;
63-
mFront = mFront->next;
64-
delete n;
65-
}
66-
67-
--mSize;
46+
Node* n = mFront;
47+
mFront = mFront->next;
48+
delete n;
49+
if (--mSize) mBack = NULL;
6850
}
6951

7052
void clear() { while (!empty()) pop(); }

0 commit comments

Comments
 (0)