-
Notifications
You must be signed in to change notification settings - Fork 176
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
Add reference count to metal_common_state #244
Conversation
to allow to invoke metal_init and metal_finish multiple times Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
lib/init.c
Outdated
@@ -11,24 +11,30 @@ int metal_init(const struct metal_init_params *params) | |||
{ | |||
int error = 0; | |||
|
|||
memset(&_metal, 0, sizeof(_metal)); | |||
if (_metal.common.ref_count++ == 0) { | |||
memset(&_metal.common + 1, 0, |
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.
&_metal.common + 1 i
s probably not good asref_count
is an integer.
More than that I wonder if creating a static variable for refcount would not be more better than having this address shift
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.
but is it good to use global reference count for the local variables at least that it isn't a global variable in libmetal level?
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.
The __metal
is a global structure that can be defined by platform. This structure can not be instantiate, in current implementation.
So we have to take into account that the only constraint of this structure is to include a struct metal_common_state
, but nothing impose that this have to be the first element of __metal
In this case, to initialize __metal, we have to implement the memset(&_metal, 0, sizeof(_metal));
Far from perfect...
That said here the only variable we want to protect is '_metal.common.ref_count++'
So if it equals to 0, no issue to use memset(&_metal, 0, sizeof(_metal));
, right?
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.
Ok, I ajdust the change, please take a look.
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.
LGTM
thanks!
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.
LGTM.
to allow to invoke metal_init and metal_finish multiple times