22
22
#include < stdint.h>
23
23
#include < stddef.h>
24
24
25
+ #include " platform/mbed_critical.h"
26
+
25
27
/* * Shared pointer class.
26
28
*
27
- * Similar to std::shared_ptr in C++11.
29
+ * A shared pointer is a "smart" pointer that retains ownership of an object using
30
+ * reference counting accross all smart pointers referencing that object.
31
+ *
32
+ * @code
33
+ * #include "platform/SharedPtr.h"
34
+ *
35
+ * struct MyStruct { int a; };
36
+ *
37
+ * // Create shared pointer
38
+ * SharedPtr<MyStruct> ptr( new MyStruct );
39
+ *
40
+ * // Increase reference count
41
+ * SharedPtr<MyStruct> ptr2( ptr );
42
+ *
43
+ * ptr =
44
+ *
45
+ * BLE& ble_interface = BLE::Instance();
46
+ * @endcode
47
+ *
48
+ *
49
+ * It is similar to the std::shared_ptr class introduced in C++11,
50
+ * however this is not a compatible implementation (no weak pointer, no make_shared, no custom deleters, etc.)
28
51
*
29
52
* Usage: SharedPtr<class> POINTER(new class())
30
53
*
@@ -43,37 +66,39 @@ class SharedPtr {
43
66
* @brief Create empty SharedPtr not pointing to anything.
44
67
* @details Used for variable declaration.
45
68
*/
46
- SharedPtr (): pointer (NULL ), counter (NULL ) {
69
+ SharedPtr (): _ptr (NULL ), _counter (NULL ) {
47
70
}
48
71
49
72
/* *
50
73
* @brief Create new SharedPtr
51
- * @param _pointer Pointer to take control over
74
+ * @param ptr Pointer to take control over
52
75
*/
53
- SharedPtr (T* _pointer ): pointer(_pointer ) {
76
+ SharedPtr (T* ptr ): _ptr(ptr ) {
54
77
// allocate counter on the heap so it can be shared
55
- counter = (uint32_t *) malloc (sizeof (uint32_t ));
56
- *counter = 1 ;
78
+ _counter = (uint32_t *) malloc (sizeof (uint32_t ));
79
+ *_counter = 1 ;
57
80
}
58
81
59
82
/* *
60
83
* @brief Destructor.
61
84
* @details Decrement reference counter and delete object if no longer pointed to.
62
85
*/
63
86
~SharedPtr () {
64
- decrementCounter ();
87
+ decrement_counter ();
65
88
}
66
89
67
90
/* *
68
91
* @brief Copy constructor.
69
- * @details Create new SharePointer from other SharedPtr by
92
+ * @details Create new SharedPtr from other SharedPtr by
70
93
* copying pointer to original object and pointer to counter.
71
94
* @param source Object being copied from.
72
95
*/
73
- SharedPtr (const SharedPtr& source): pointer (source.pointer ), counter (source.counter ) {
96
+ SharedPtr (const SharedPtr& source): _ptr (source._ptr ), _counter (source._counter ) {
74
97
// increment reference counter
75
- if (counter) {
76
- (*counter)++;
98
+ if (_counter) {
99
+ core_util_critical_section_enter ();
100
+ (*_counter)++;
101
+ core_util_critical_section_exit ();
77
102
}
78
103
}
79
104
@@ -86,15 +111,17 @@ class SharedPtr {
86
111
SharedPtr operator =(const SharedPtr& source) {
87
112
if (this != &source) {
88
113
// clean up by decrementing counter
89
- decrementCounter ();
114
+ decrement_counter ();
90
115
91
116
// assign new values
92
- pointer = source.get ();
93
- counter = source.getCounter ();
117
+ _ptr = source.get ();
118
+ _counter = source.getCounter ();
94
119
95
120
// increment new counter
96
- if (counter) {
97
- (*counter)++;
121
+ if (_counter) {
122
+ core_util_critical_section_enter ();
123
+ (*_counter)++;
124
+ core_util_critical_section_exit ();
98
125
}
99
126
}
100
127
@@ -107,16 +134,18 @@ class SharedPtr {
107
134
* @return Pointer.
108
135
*/
109
136
T* get () const {
110
- return pointer ;
137
+ return _ptr ;
111
138
}
112
139
113
140
/* *
114
141
* @brief Reference count accessor.
115
142
* @return Reference count.
116
143
*/
117
144
uint32_t use_count () const {
118
- if (counter) {
119
- return *counter;
145
+ if (_counter) {
146
+ core_util_critical_section_enter ();
147
+ return *_counter;
148
+ core_util_critical_section_exit ();
120
149
} else {
121
150
return 0 ;
122
151
}
@@ -127,23 +156,23 @@ class SharedPtr {
127
156
* @details Override to return the object pointed to.
128
157
*/
129
158
T& operator *() const {
130
- return *pointer ;
159
+ return *_ptr ;
131
160
}
132
161
133
162
/* *
134
163
* @brief Dereference object member operator.
135
164
* @details Override to return return member in object pointed to.
136
165
*/
137
166
T* operator ->() const {
138
- return pointer ;
167
+ return _ptr ;
139
168
}
140
169
141
170
/* *
142
171
* @brief Boolean conversion operator.
143
172
* @return Whether or not the pointer is NULL.
144
173
*/
145
174
operator bool () const {
146
- return (pointer != 0 );
175
+ return (_ptr != 0 );
147
176
}
148
177
149
178
private:
@@ -152,30 +181,33 @@ class SharedPtr {
152
181
* @return Pointer to reference counter.
153
182
*/
154
183
uint32_t * getCounter () const {
155
- return counter ;
184
+ return _counter ;
156
185
}
157
186
158
187
/* *
159
188
* @brief Decrement reference counter.
160
189
* @details If count reaches zero, free counter and delete object pointed to.
161
190
*/
162
- void decrementCounter () {
163
- if (counter) {
164
- if (*counter == 1 ) {
165
- free (counter);
166
- delete pointer;
191
+ void decrement_counter () {
192
+ if (_counter) {
193
+ core_util_critical_section_enter ();
194
+ if (*_counter == 1 ) {
195
+ core_util_critical_section_exit ();
196
+ free (_counter);
197
+ delete _ptr;
167
198
} else {
168
- (*counter)--;
199
+ (*_counter)--;
200
+ core_util_critical_section_exit ();
169
201
}
170
202
}
171
203
}
172
204
173
205
private:
174
206
// pointer to shared object
175
- T* pointer ;
207
+ T* _ptr ;
176
208
177
209
// pointer to shared reference counter
178
- uint32_t * counter ;
210
+ uint32_t * _counter ;
179
211
};
180
212
181
213
/* * Non-member relational operators.
0 commit comments