-
Notifications
You must be signed in to change notification settings - Fork 3k
Re-add Shared Pointer Class into platform features #7815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
platform/SharedPtr.h
Outdated
|
||
// increment new counter | ||
if (counter) { | ||
(*counter)++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use an atomic so we get the same guarantee as the regular shared_ptr ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hadn't realised it wasn't done atomically, will update the implementation :)
platform/SharedPtr.h
Outdated
* | ||
* Similar to std::shared_ptr in C++11. | ||
* | ||
* Usage: SharedPtr<class> POINTER(new class()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mention that it is not a full feature shared_ptr:
- no weak ptr
- no make_shared
- no custom deleter
- no cast operator
- no enable_shared_from_this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!
platform/SharedPtr.h
Outdated
* | ||
* When POINTER is passed around by value the copy constructor and | ||
* destructor counts the number of references to the original object. | ||
* If the counter reaches zero, delete is called on the object pointed to. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange text?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it looks like many more 'L's were accidentally capitalized.
I've cleaned up the class and its doc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 👍
I just had a bunch of nits
platform/SharedPtr.h
Outdated
SharedPtr(T* ptr): _ptr(ptr), _counter(NULL) { | ||
// allocate counter on the heap so it can be shared | ||
if(_ptr != NULL) { | ||
_counter = (uint32_t*) malloc(sizeof(uint32_t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be new uint32_t
so OOM asserts?
platform/SharedPtr.h
Outdated
if (_ptr != NULL) { | ||
core_util_critical_section_enter(); | ||
(*_counter)++; | ||
core_util_critical_section_exit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, is core_util_atomic_incr_u32 applicable here?
Not a blocker, adopting atomic increments could be an optimization for later
platform/SharedPtr.h
Outdated
void decrement_counter() { | ||
if (_counter) { | ||
core_util_critical_section_enter(); | ||
if (*_counter == 1) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tiny tiny tiny micro-optimization, so feel free to ignore. But comparing with zero is slightly better in terms of code size. Once you hit zero there's also no race conditions possible.
Thanks @geky - should be a bit more optimised now :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@donatieng - Good work. Should any tests be added for SharedPtr class?
Definitely. And documentation (I'll add docs team to review). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having Shared pointer +1
Can you check astyle travis job and fix the styling?
Is there any reason this API shouldn't have a user API page added to the handbook? |
Copy edit for consistent capitalization and minor grammar nits.
@AnotherButler created this PR for the handbook: ARMmbed/mbed-os-5-docs#671 |
platform/SharedPtr.h
Outdated
{ | ||
if (_ptr != NULL) { | ||
core_util_critical_section_enter(); | ||
return *_counter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you return prior exiting from critical section (is there an intention?)
Urgh! Thanks for spotting this @0xc0170! |
LittleFS test failure seems quite unrelated to this PR's content |
@donatieng Correct. We found something external that appears to have affected Mbed OS CI. Breaking change has been reverted, and Travis CI jobs are being restarted. |
Thanks @cmonr - unless anyone has any objections, I think this one is good for morphing :) |
/morph build |
@studavekar @cmonr /morph build |
Build : SUCCESSBuild number : 2862 Triggering tests/morph test |
Build : SUCCESSBuild number : 2863 Triggering tests/morph test |
Exporter Build : SUCCESSBuild number : 2492 |
Exporter Build : FAILUREBuild number : 2493 |
Test : SUCCESSBuild number : 2627 |
/morph export-build |
Test : SUCCESSBuild number : 2628 |
Exporter Build : SUCCESSBuild number : 2501 |
Description
This PR re-adds a shared pointer class to mbed OS (similar to the
std::shared_ptr
class introduced in C++11). This class (or a variation of) is used by different projects and used to be part of the "core-util" classes. The only change from this previous revision is the name:SharedPtr
(to avoid any clashes with existing projects and be consistent with theSingletonPtr
class).Pull request type