Skip to content

Commit 2608478

Browse files
author
Donatien Garnier
committed
Cleanup shared pointer implementation and add reset() methods
1 parent f45642c commit 2608478

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

platform/SharedPtr.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
* // Increase reference count
4141
* SharedPtr<MyStruct> ptr2( ptr );
4242
*
43-
* ptr =
44-
*
45-
* BLE& ble_interface = BLE::Instance();
43+
* ptr = NULL; // Reference to the struct instance is still held by ptr2
44+
*
45+
* ptr2 = NULL; // The raw pointer is freed
4646
* @endcode
4747
*
4848
*
@@ -73,10 +73,12 @@ class SharedPtr {
7373
* @brief Create new SharedPtr
7474
* @param ptr Pointer to take control over
7575
*/
76-
SharedPtr(T* ptr): _ptr(ptr) {
76+
SharedPtr(T* ptr): _ptr(ptr), _counter(NULL) {
7777
// allocate counter on the heap so it can be shared
78-
_counter = (uint32_t*) malloc(sizeof(uint32_t));
79-
*_counter = 1;
78+
if(_ptr != NULL) {
79+
_counter = (uint32_t*) malloc(sizeof(uint32_t));
80+
*_counter = 1;
81+
}
8082
}
8183

8284
/**
@@ -95,7 +97,7 @@ class SharedPtr {
9597
*/
9698
SharedPtr(const SharedPtr& source): _ptr(source._ptr), _counter(source._counter) {
9799
// increment reference counter
98-
if (_counter) {
100+
if (_ptr != NULL) {
99101
core_util_critical_section_enter();
100102
(*_counter)++;
101103
core_util_critical_section_exit();
@@ -115,10 +117,10 @@ class SharedPtr {
115117

116118
// assign new values
117119
_ptr = source.get();
118-
_counter = source.getCounter();
120+
_counter = source.get_counter();
119121

120122
// increment new counter
121-
if (_counter) {
123+
if (_ptr != NULL) {
122124
core_util_critical_section_enter();
123125
(*_counter)++;
124126
core_util_critical_section_exit();
@@ -128,6 +130,28 @@ class SharedPtr {
128130
return *this;
129131
}
130132

133+
/**
134+
* @brief Replaces the managed pointer with a new unmanaged pointer.
135+
* @param[in] ptr the new raw pointer to manage.
136+
*/
137+
void reset(T* ptr) {
138+
// clean up by decrementing counter
139+
decrement_counter();
140+
141+
if(ptr != NULL) {
142+
// allocate counter on the heap so it can be shared
143+
_counter = (uint32_t*) malloc(sizeof(uint32_t));
144+
*_counter = 1;
145+
}
146+
}
147+
148+
/**
149+
* @brief Replace the managed pointer with a NULL pointer.
150+
*/
151+
void reset() {
152+
reset(NULL);
153+
}
154+
131155
/**
132156
* @brief Raw pointer accessor.
133157
* @details Get raw pointer to object pointed to.
@@ -142,7 +166,7 @@ class SharedPtr {
142166
* @return Reference count.
143167
*/
144168
uint32_t use_count() const {
145-
if (_counter) {
169+
if (_ptr != NULL) {
146170
core_util_critical_section_enter();
147171
return *_counter;
148172
core_util_critical_section_exit();
@@ -172,15 +196,15 @@ class SharedPtr {
172196
* @return Whether or not the pointer is NULL.
173197
*/
174198
operator bool() const {
175-
return (_ptr != 0);
199+
return (_ptr != NULL);
176200
}
177201

178202
private:
179203
/**
180204
* @brief Get pointer to reference counter.
181205
* @return Pointer to reference counter.
182206
*/
183-
uint32_t* getCounter() const {
207+
uint32_t* get_counter() const {
184208
return _counter;
185209
}
186210

@@ -194,7 +218,9 @@ class SharedPtr {
194218
if (*_counter == 1) {
195219
core_util_critical_section_exit();
196220
free(_counter);
221+
_counter = NULL;
197222
delete _ptr;
223+
_ptr = NULL;
198224
} else {
199225
(*_counter)--;
200226
core_util_critical_section_exit();

0 commit comments

Comments
 (0)