Introduce QEMU_NEW()
PATCH 1/1 Introduce QEMU_NEW()
qemu_malloc() is type-unsafe as it returns a void pointer. Introduce QEMU_NEW() (and QEMU_NEWZ()), which return the correct type.
---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)
qemu-common.h +3 -0
@@ -186,6 +186,9 @@ void qemu_free(void *ptr);char *qemu_strdup (const char *str);char *qemu_strndup (const char *str,size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type)))) +void qemu_mutex_lock_iothread (void );void qemu_mutex_unlock_iothread (void );
Signature · 1 line
1.7.5.3
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.
What does this buy you over
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
Alex
Quoted 10 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.What does this buy you over
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
It'll warn when you do silly things such as:
struct some_struct *k;
k = qemu_malloc(sizeof(k));
Signature · 1 line
Sasha.
Quoted 10 lines · Sasha Levin
Quoted 9 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.What does this buy you over
type *x = qemu_malloc(sizeof(type));
Quoted 7 lines
Quoted 1 line
? I find the non-C++ version easier to read even.
It'll warn when you do silly things such as:
struct
some_struct*k;k = qemu_malloc(sizeof(k));
Hm - is there any way to get this without adding upper case C++'ish macros?
Alex
Quoted 9 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.What does this buy you over
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
Yeah, while we're writing in C we should just stick to the C-like APIs, it's less confusing IMHO than wrapping it up in something else. I assume Anthony's new object model stuff will have a "create me a new foo object" API anyway, so QEMU_NEW() is possibly a bit of a namespace grab.
-- PMM
Quoted 1 line · Alexander Graf
Hm - is there any way to get this without adding upper case C++'ish macros?
Switch to C++.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 5 lines · Peter Maydell
Quoted 4 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.
Quoted 8 lines
Quoted 5 lines
What does this buy you over
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
Yeah, while we're writing in C we should just stick to the C-like APIs, it's less confusing IMHO than wrapping it up in something else.
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
Quoted 3 lines
I assume Anthony's new object model stuff will have a "create me a new foo object" API anyway, so
QEMU_NEW()is possibly a bit of a namespace grab.
Anthony's stuff is at a much higher level, hopefully he'll come back to the ground one day.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Peter Maydell
Quoted 6 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.What does this buy you over
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
Yeah, while we're writing in C we should just stick to the C-like APIs, it's less confusing IMHO than wrapping it up in something else.
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
I sympathize with Peter on the rationale that keeping interfaces aligned with how C APIs usually look like helps making the code more readable.
Alex
Quoted 4 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for QEMU_NEW (despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to remove qemu_malloc, declare malloc() as unusable and convert all users of qemu_malloc() to qemu_new().
Alex
Quoted 16 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Signed-off-by Avi Kivity <[email protected]>---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)qemu-common.h +0
-0
qemu-common.h void qemu_free(void *ptr); · line 190
Quoted 5 lines
qemu-common.h +2
-0@@ -186,6 +186,9 @@ void qemu_free(void *ptr); char *qemu_strdup(const char *str); char *qemu_strndup(const char *str, size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type))))
Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));
Becomes:
struct foo *f;
...
f = QEMU_NEW(struct foo);
If you ever change the name of the type you have to search-replace these instances. The idomatic C way works well, I don't see a reason to use QEMU_NEW().
Stefan
Quoted 11 lines · Alexander Graf
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for
QEMU_NEW(despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to removeqemu_malloc, declaremalloc()as unusable and convert all users ofqemu_malloc()toqemu_new().
Some qemu_mallocs() will remain (allocating a byte array or something variable sized).
I agree qemu_new() will be nicer, but that will have to wait until Blue is several light-days away from Earth.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 14 lines · Stefan Hajnoczi
Quoted 4 lines
char *qemu_strndup(const char *str, size_t size);+#define
QEMU_NEW(type)((type *)(qemu_malloc(sizeof(type)))) +#defineQEMU_NEWZ(type)((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));Becomes:
Quoted 8 lines
struct foo *f;
...
f =QEMU_NEW(struct foo);If you ever change the name of the type you have to search-replace these instances. The idomatic C way works well, I don't see a reason to use
QEMU_NEW().
It works as long as you don't make any mistakes:
f = qemu_malloc(sizeof(*g)); f = qemu_malloc(sizeof(f));
qemu_malloc() returns a void pointer, these are poisonous.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Alexander Graf
Quoted 4 lines · Avi Kivity
Quoted 3 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for
QEMU_NEW(despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to removeqemu_malloc, declaremalloc()as unusable and convert all users ofqemu_malloc()toqemu_new().Some
qemu_mallocs()will remain (allocating a byte array or something variable sized).
Right. But then we really do need a check in checkpatch.pl to see if someone's using qemu_new for simple structs.
Quoted 1 line
I agree
qemu_new()will be nicer, but that will have to wait until Blue is several light-days away from Earth.
Blue, any disagreement on adding qemu_new() as a macro? Something used that often in upper case would seriously disturb the reading flow :)
Alex
Am 25.07.2011 12:06, schrieb Stefan Hajnoczi:
Quoted 30 lines · Avi Kivity
Quoted 16 lines
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Signed-off-by Avi Kivity <[email protected]>---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)qemu-common.h +0
-0qemu-common.h void qemu_free(void *ptr); · line 190
Quoted 5 lines
qemu-common.h +2
-0@@ -186,6 +186,9 @@ void qemu_free(void *ptr);char *qemu_strdup (const char *str);char *qemu_strndup (const char *str,size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));
Quoted 6 lines
Becomes:
struct foo *f;
...
f =QEMU_NEW(struct foo);
Maybe we should allow this and make it the usual pattern:
f = qemu_new(typeof(*f));
It's gcc specific, but we already don't care about portability to other compilers in more places.
On the other hand, how many bugs did we have recently that were caused by a wrong sizeof for qemu_malloc? As far as I can say, there's no real reason to do it. I think it's the same kind of discussion as with forbidding qemu_malloc(0) (except that this time it just won't improve things much instead of being really stupid).
Kevin
Quoted 42 lines · Kevin Wolf
Am 25.07.2011 12:06, schrieb Stefan Hajnoczi:
Quoted 30 lines · Avi Kivity
Quoted 16 lines
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Signed-off-by Avi Kivity <[email protected]>---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)qemu-common.h +0
-0qemu-common.h void qemu_free(void *ptr); · line 190
Quoted 5 lines
qemu-common.h +2
-0@@ -186,6 +186,9 @@ void qemu_free(void *ptr); char *qemu_strdup(const char *str); char *qemu_strndup(const char *str, size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));Quoted 6 lines
Becomes:
struct foo *f;
...
f =QEMU_NEW(struct foo);Maybe we should allow this and make it the usual pattern:
f = qemu_new(typeof(*f));
Quoted 8 lines
It's gcc specific, but we already don't care about portability to other compilers in more places.
On the other hand, how many bugs did we have recently that were caused by a wrong sizeof for
qemu_malloc? As far as I can say, there's no real reason to do it. I think it's the same kind of discussion as with forbiddingqemu_malloc(0)(except that this time it just won't improve things much instead of being really stupid).
Totally agree. In theory you can add stuff on top to prevent known bad uses but in practice it's not worth obfuscating the code.
Stefan
Quoted 16 lines · Alexander Graf
Quoted 13 lines · Avi Kivity
Quoted 11 lines · Alexander Graf
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for
QEMU_NEW(despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to removeqemu_malloc, declaremalloc()as unusable and convert all users ofqemu_malloc()toqemu_new().
Quoted 8 lines
Quoted 1 line
Some
qemu_mallocs()will remain (allocating a byte array or something variable sized).Right. But then we really do need a check in checkpatch.pl to see if someone's using
qemu_newfor simple structs.Quoted 1 line
I agree
qemu_new()will be nicer, but that will have to wait until Blue is several light-days away from Earth.Blue, any disagreement on adding
qemu_new()as a macro? Something used that often in upper case would seriously disturb the reading flow :)
So do not use it then, macros should be uppercase.
Signature · 1 line
mailto:[email protected]
Quoted 7 lines · Avi Kivity
Quoted 6 lines · Alexander Graf
Quoted 4 lines · Avi Kivity
Quoted 3 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for
QEMU_NEW(despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to removeqemu_malloc, declaremalloc()as unusable and convert all users ofqemu_malloc()toqemu_new().Some
qemu_mallocs()will remain (allocating a byte array or something variable sized).
Byte array: add the obvious type-safe allocator for a variable-sized array T[N], then use it with unsigned char for T.
In fact, I find QEMU_NEW() pretty pointless without a buddy for arrays.
Still not covered: allocating a struct with a variable-size array as final member. I guess a solution for that can be found if we care enough.
[...]
Quoted 42 lines · Kevin Wolf
Am 25.07.2011 12:06, schrieb Stefan Hajnoczi:
Quoted 30 lines · Avi Kivity
Quoted 16 lines
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Signed-off-by Avi Kivity <[email protected]>---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)qemu-common.h +0
-0qemu-common.h void qemu_free(void *ptr); · line 190
Quoted 5 lines
qemu-common.h +2
-0@@ -186,6 +186,9 @@ void qemu_free(void *ptr);char *qemu_strdup (const char *str);char *qemu_strndup (const char *str,size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));Quoted 6 lines
Becomes:
struct foo *f;
...
f =QEMU_NEW(struct foo);Maybe we should allow this and make it the usual pattern:
f = qemu_new(typeof(*f));
Quoted 8 lines
It's gcc specific, but we already don't care about portability to other compilers in more places.
On the other hand, how many bugs did we have recently that were caused by a wrong sizeof for
qemu_malloc? As far as I can say, there's no real reason to do it. I think it's the same kind of discussion as with forbiddingqemu_malloc(0)(except that this time it just won't improve things much instead of being really stupid).
Side-stepping the stupid "OMG malloc(0) is weird, therefore we must make qemu_malloc(0) differently weird" controversy would be useful all by itself.
Quoted 15 lines · Markus Armbruster
Quoted 13 lines · Avi Kivity
Quoted 11 lines · Alexander Graf
Quoted 7 lines · Avi Kivity
Quoted 4 lines · Alexander Graf
Quoted 2 lines
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for
QEMU_NEW(despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to removeqemu_malloc, declaremalloc()as unusable and convert all users ofqemu_malloc()toqemu_new().
Quoted 7 lines
Quoted 2 lines
Some
qemu_mallocs()will remain (allocating a byte array or something variable sized).Byte array: add the obvious type-safe allocator for a variable-sized array T[N], then use it with unsigned char for T.
In fact, I find
QEMU_NEW()pretty pointless without a buddy for arrays.
#define QEMU_NEW_MULTI(type, len) ((type *)(qemu_mallocz(sizeof(type) * len)))
char *arr = QEMU_NEW_MULTI(char, 1024);
Quoted 3 lines
Still not covered: allocating a struct with a variable-size array as final member. I guess a solution for that can be found if we care enough.
Yeah, but at the end of the day I'd assume most of us know C and can just open code this all, no?
Alex
Quoted 11 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.What does this buy you over
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
I'm using it as
MemoryRegion *phys_flash = QEMU_NEW(MemoryRegion);
instead of
MemoryRegion *phys_flash = qemu_malloc(sizeof(*phys_flash));
I find it shorter, and if I make a mistake, the compiler shouts at me instead of a runtime crash.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 3 lines · Markus Armbruster
Side-stepping the stupid "OMG malloc(0) is weird, therefore we must make
qemu_malloc(0)differently weird" controversy would be useful all by itself.
If we all work together, we can make this thread even bigger than the tools/kvm pull request.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.
Just use g_new() and g_new0()
Regards,
Anthony Liguori
Quoted 23 lines
Signed-off-by Avi Kivity<[email protected]>---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)qemu-common.h +3
-0@@ -186,6 +186,9 @@ void qemu_free(void *ptr);char *qemu_strdup (const char *str);char *qemu_strndup (const char *str,size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type)))) +void qemu_mutex_lock_iothread (void );void qemu_mutex_unlock_iothread (void );
Quoted 6 lines · Anthony Liguori
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Just use
g_new()andg_new0()
These bypass qemu_malloc(). Are we okay with that?
I suppose so, since many library functions can allocate memory and bypass qemu_malloc()?
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Alexander Graf
#define
QEMU_NEW_MULTI(type, len)((type *)(qemu_mallocz(sizeof(type) * len)))char *arr =
QEMU_NEW_MULTI(char, 1024);Quoted 3 lines
Still not covered: allocating a struct with a variable-size array as final member. I guess a solution for that can be found if we care enough.
Yeah, but at the end of the day I'd assume most of us know C and can just open code this all, no?
While it's always fun to reinvent things, glib has already solved all of this and we're already dependent on it in the build:
http://developer.gnome.org/glib/stable/glib-Memory-Allocation.html
It also has fancy ways to hook memory allocation for debugging.
Regards,
Anthony Liguori
Quoted 4 lines
Alex
Quoted 9 lines · Avi Kivity
Quoted 6 lines · Anthony Liguori
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Just use
g_new()andg_new0()These bypass
qemu_malloc(). Are we okay with that?
Yes. We can just make qemu_malloc use g_malloc.
Quoted 2 lines
I suppose so, since many library functions can allocate memory and bypass
qemu_malloc()?
Right.
Regards,
Anthony Liguori
Quoted 1 line
Quoted 18 lines · Avi Kivity
Quoted 5 lines · Peter Maydell
Quoted 4 lines · Alexander Graf
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Quoted 8 lines
Quoted 5 lines
What does this buy you over
type *x = qemu_malloc(sizeof(type));
? I find the non-C++ version easier to read even.
Yeah, while we're writing in C we should just stick to the C-like APIs, it's less confusing IMHO than wrapping it up in something else.
That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.
Quoted 6 lines
Quoted 3 lines
I assume Anthony's new object model stuff will have a "create me a new foo object" API anyway, so
QEMU_NEW()is possibly a bit of a namespace grab.Anthony's stuff is at a much higher level, hopefully he'll come back to the ground one day.
The point of introducing glib was to address things like this. We need to start making heavier use of what it provides.
Regards,
Anthony Liguori
Quoted 6 lines · Anthony Liguori
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Anthony Liguori
Quoted 2 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Just use
g_new()andg_new0()These bypass
qemu_malloc(). Are we okay with that?Yes. We can just make
qemu_mallocuseg_malloc.
Excellent. Patch withdrawn.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 20 lines · Avi Kivity
Quoted 11 lines · Alexander Graf
Quoted 8 lines · Avi Kivity
Quoted 6 lines · Alexander Graf
>
> That argument can be used to block any change. You'll get used to it in time. The question is, is the new interface better or not.I agree that it keeps you from accidently malloc'ing a struct of pointer size. But couldn't we also just add this to checkpatch.pl?
Quoted 7 lines
Quoted 1 line
Better APIs trump better patch review.
Only if you enforce them. The only sensible thing for
QEMU_NEW(despite the general rule of upper case macros, I'd actually prefer this one to be lower case though since it's so often used) would be to removeqemu_malloc, declaremalloc()as unusable and convert all users ofqemu_malloc()toqemu_new().
Quoted 5 lines
Some
qemu_mallocs()will remain (allocating a byte array or something variable sized).I agree
qemu_new()will be nicer, but that will have to wait until Blue is several light-days away from Earth.
There is no escape. Don't make me destroy you. You cannot hide forever, Luke.
Quoted 1 line · Blue Swirl
There is no escape. Don't make me destroy you. You cannot hide forever, Luke.
Touché
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 9 lines · Anthony Liguori
Quoted 8 lines · Avi Kivity
Quoted 6 lines · Anthony Liguori
Quoted 3 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Just use
g_new()andg_new0()These bypass
qemu_malloc(). Are we okay with that?Yes. We can just make
qemu_mallocuseg_malloc.
It would be also possible to make g_malloc() use qemu_malloc(). That way we could keep the tracepoints which would lose their value with g_malloc() otherwise.
Quoted 13 lines · Blue Swirl
Quoted 12 lines · Anthony Liguori
Quoted 11 lines · Avi Kivity
Quoted 8 lines · Anthony Liguori
Quoted 3 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Just use
g_new()andg_new0()
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
These bypass
qemu_malloc(). Are we okay with that?Yes. We can just make
qemu_mallocuseg_malloc.It would be also possible to make
g_malloc()useqemu_malloc(). That way we could keep the tracepoints which would lose their value withg_malloc()otherwise.
Or just add tracepoints to g_malloc()...
But yeah, the point is, we ought to unify to a standard library function instead of inventing our own version of everything.
Regards,
Anthony Liguori
Quoted 1 line
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Just use
g_new()andg_new0()These bypass
qemu_malloc(). Are we okay with that?Yes. We can just make
qemu_mallocuseg_malloc.
Quoted 8 lines
Quoted 3 lines
It would be also possible to make
g_malloc()useqemu_malloc(). That way we could keep the tracepoints which would lose their value withg_malloc()otherwise.Or just add tracepoints to
g_malloc()...But yeah, the point is, we ought to unify to a standard library function instead of inventing our own version of everything.
What about zero-size allocations for which g_malloc would return NULL?
Signature · 2 lines
Thanks. -- Max
Quoted 14 lines · Max Filippov
Quoted 14 lines
Quoted 13 lines
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Just use
g_new()andg_new0()These bypass
qemu_malloc(). Are we okay with that?Yes. We can just make
qemu_mallocuseg_malloc.It would be also possible to make
g_malloc()useqemu_malloc(). That way we could keep the tracepoints which would lose their value withg_malloc()otherwise.
Quoted 6 lines
Quoted 4 lines
Or just add tracepoints to
g_malloc()...But yeah, the point is, we ought to unify to a standard library function instead of inventing our own version of everything.
What about zero-size allocations for which
g_mallocwould return NULL?
Using a standard, well documented, rich interface trumps any arguments about the semantics of zero-sized allocation.
Regards,
Anthony Liguori
Quoted 18 lines · Anthony Liguori
Quoted 17 lines · Max Filippov
Quoted 17 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Just use
g_new()andg_new0()Quoted 7 lines
Quoted 3 lines
Quoted 1 line
These bypass
qemu_malloc(). Are we okay with that?Yes. We can just make
qemu_mallocuseg_malloc.It would be also possible to make
g_malloc()useqemu_malloc(). That way we could keep the tracepoints which would lose their value withg_malloc()otherwise.Or just add tracepoints to
g_malloc()...
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
But yeah, the point is, we ought to unify to a standard library function instead of inventing our own version of everything.
What about zero-size allocations for which
g_mallocwould return NULL?Using a standard, well documented, rich interface trumps any arguments about the semantics of zero-sized allocation.
Right right.. only g_new aborts on zero..
Signature · 1 line
mailto:[email protected]
Quoted 2 lines · malc
Right right.. only
g_newaborts on zero..
"If n_structs is 0 it returns NULL <http://developer.gnome.org/glib/2.28/glib-Standard-Macros.html#NULL:CAPS>. "
It's annoying that it takes this parameter at all, but I can live with it.
Signature · 1 line
error compiling committee.c: too many arguments to function
Paolo Bonzini
maintainerQuoted 5 lines · Blue Swirl
Quoted 1 line
Yes. We can just make
qemu_mallocuseg_malloc.It would be also possible to make
g_malloc()useqemu_malloc(). That way we could keep the tracepoints which would lose their value withg_malloc()otherwise.
qemu_malloc uses g_malloc => you keep tracepoints, you just do not trace memory allocated by glib
g_malloc uses qemu_malloc => you keep and expand tracepoints, you lose the very nicely tuned allocator
The former is much less code, however it requires qemu_malloc to be always balanced with qemu_free (patches ready and on my github tree, won't be sent before KVM Forum though...).
Paolo
Quoted 10 lines · Paolo Bonzini
Quoted 6 lines · Blue Swirl
Quoted 2 lines
Yes. We can just make
qemu_mallocuseg_malloc.It would be also possible to make
g_malloc()useqemu_malloc(). That way we could keep the tracepoints which would lose their value withg_malloc()otherwise.
qemu_mallocusesg_malloc=> you keep tracepoints, you just do not trace memory allocated by glib
Unless the plan is to replace all qemu_malloc() calls with calls to g_malloc().
Quoted 2 lines
g_mallocusesqemu_malloc=> you keep and expand tracepoints, you lose the very nicely tuned allocator
It is replaced by libc malloc() which shouldn't be so bad either.
Quoted 3 lines
The former is much less code, however it requires
qemu_mallocto be always balanced withqemu_free(patches ready and on my github tree, won't be sent before KVM Forum though...).
Freeing qemu_malloc() memory with plain free() is a bug.
Quoted 6 lines · Avi Kivity
Quoted 2 lines · malc
Right right.. only
g_newaborts on zero.."If
n_structsis 0 it returns NULL
<http://developer.gnome.org/glib/2.28/glib-Standard-Macros.html#NULL:CAPS>. "
Right you are.
Quoted 3 lines
It's annoying that it takes this parameter at all, but I can live with it.
n_structs?
Signature · 1 line
mailto:[email protected]
Quoted 5 lines · malc
Quoted 3 lines
It's annoying that it takes this parameter at all, but I can live with it.
n_structs?
Yes. It's 1 in 1-epsilon of all cases. Would have preferred g_new and G_new_array instead.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 11 lines · Stefan Hajnoczi
Quoted 2 lines
+#define
QEMU_NEW(type)((type *)(qemu_malloc(sizeof(type))))Quoted 1 line
+#define
QEMU_NEWZ(type)((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));Becomes:
Quoted 8 lines
struct foo *f;
...
f =QEMU_NEW(struct foo);If you ever change the name of the type you have to search-replace these instances. The idomatic C way works well, I don't see a reason to use
QEMU_NEW().
You're right, and it will promote even more abuse of the ugly typedefs. This really makes the code less readable, especially for outsiders :(
Jes
Quoted 17 lines · Jes Sorensen
Quoted 16 lines · Stefan Hajnoczi
Quoted 2 lines
+#define
QEMU_NEW(type)((type *)(qemu_malloc(sizeof(type))))Quoted 1 line
+#define
QEMU_NEWZ(type)((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));Becomes:
struct foo *f;
...
f =QEMU_NEW(struct foo);
Quoted 6 lines
Quoted 3 lines
If you ever change the name of the type you have to search-replace these instances. The idomatic C way works well, I don't see a reason to use
QEMU_NEW().You're right, and it will promote even more abuse of the ugly typedefs. This really makes the code less readable, especially for outsiders :(
I don't think it really matters either way. If some people prefer to use g_new(struct foo, 1) vs. g_malloc(sizeof(*f)), I don't think it significantly impacts overall code readability.
But having nice, documentation for key internal APIs does which is why using the glib interfaces makes sense IMHO.
Regards,
Anthony Liguori
Quoted 5 lines
Jes
Quoted 25 lines · Anthony Liguori
Quoted 17 lines · Jes Sorensen
Quoted 16 lines · Stefan Hajnoczi
Quoted 2 lines
+#define
QEMU_NEW(type)((type *)(qemu_malloc(sizeof(type))))Quoted 1 line
+#define
QEMU_NEWZ(type)((type *)(qemu_mallocz(sizeof(type))))Does this mean we need to duplicate the type name for each allocation?
struct foo *f;
...
f = qemu_malloc(sizeof(*f));Becomes:
struct foo *f;
...
f =QEMU_NEW(struct foo);Quoted 6 lines
Quoted 3 lines
If you ever change the name of the type you have to search-replace these instances. The idomatic C way works well, I don't see a reason to use
QEMU_NEW().You're right, and it will promote even more abuse of the ugly typedefs. This really makes the code less readable, especially for outsiders :(
Quoted 6 lines
I don't think it really matters either way. If some people prefer to use
g_new(struct foo, 1)vs. g_malloc(sizeof(*f)), I don't think it significantly impacts overall code readability.But having nice, documentation for key internal APIs does which is why using the glib interfaces makes sense IMHO.
Using the commands consistently does have an impact, and at least with qemu_malloc() it is obvious what they are and how they behave. The proposed macros on the other hand requires everybody to go look up the macro to find out what it is trying to do.
Jes
Quoted 4 lines · Jes Sorensen
Using the commands consistently does have an impact, and at least with
qemu_malloc()it is obvious what they are and how they behave. The proposed macros on the other hand requires everybody to go look up the macro to find out what it is trying to do.
That's true for every single function and macro that qemu defines.
Signature · 1 line
error compiling committee.c: too many arguments to function
Paolo Bonzini
maintainerQuoted 4 lines · Blue Swirl
Quoted 2 lines
qemu_mallocusesg_malloc=> you keep tracepoints, you just do not trace memory allocated by glibUnless the plan is to replace all
qemu_malloc()calls with calls tog_malloc().
We can worry when the day comes... there is already another task blocking that replacement (balancing qemu_malloc with qemu_free).
Quoted 5 lines
Quoted 3 lines
The former is much less code, however it requires
qemu_mallocto be always balanced withqemu_free(patches ready and on my github tree, won't be sent before KVM Forum though...).Freeing
qemu_malloc()memory with plainfree()is a bug.
We have many bugs, then. :)
Paolo
Quoted 8 lines · Avi Kivity
Quoted 4 lines · Jes Sorensen
Using the commands consistently does have an impact, and at least with
qemu_malloc()it is obvious what they are and how they behave. The proposed macros on the other hand requires everybody to go look up the macro to find out what it is trying to do.That's true for every single function and macro that qemu defines.
Of course, and every time it adds complexity for reading it. In this particular case it seems to simply make the code worse for no gain.
Jes
Quoted 7 lines · Jes Sorensen
Quoted 6 lines · Avi Kivity
Quoted 4 lines · Jes Sorensen
Using the commands consistently does have an impact, and at least with
qemu_malloc()it is obvious what they are and how they behave. The proposed macros on the other hand requires everybody to go look up the macro to find out what it is trying to do.
Quoted 5 lines
Quoted 2 lines
That's true for every single function and macro that qemu defines.
Of course, and every time it adds complexity for reading it. In this particular case it seems to simply make the code worse for no gain.
I guess type safety doesn't matter to you.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Jes Sorensen
Quoted 2 lines · Avi Kivity
Quoted 1 line · Jes Sorensen
Using the commands consistently does have an impact, and at least
with
Quoted 2 lines
Quoted 2 lines
qemu_malloc()it is obvious what they are and how they behave. The proposed macros on the other hand requires everybody to go look upthe
Quoted 2 lines
Quoted 1 line
macro to find out what it is trying to do.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
That's true for every single function and macro that qemu defines.
Of course, and every time it adds complexity for reading it. In this particular case it seems to simply make the code worse for no gain.
I guess type safety doesn't matter to you.
In my experience it's one of the lesser problems in the code.
Jes
Quoted 5 lines · Jes Sorensen
Quoted 2 lines
I guess type safety doesn't matter to you.
In my experience it's one of the lesser problems in the code.
It's a big issue to someone making widespread changes in the code (like me now).
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 16 lines · Avi Kivity
qemu_malloc()is type-unsafe as it returns a void pointer. IntroduceQEMU_NEW()(andQEMU_NEWZ()), which return the correct type.Signed-off-by Avi Kivity <[email protected]>---
This is part of my memory API patchset, but doesn't really belong there.
qemu-common.h | 3 +++ 1 files changed, 3 insertions(+), 0 deletions(-)qemu-common.h +0
-0
qemu-common.h void qemu_free(void *ptr); · line 193
Quoted 8 lines
qemu-common.h +3
-0@@ -186,6 +186,9 @@ void qemu_free(void *ptr);char *qemu_strdup (const char *str);char *qemu_strndup (const char *str,size_t size); +#define QEMU_NEW (type) ((type *)(qemu_malloc(sizeof(type)))) +#define QEMU_NEWZ (type) ((type *)(qemu_mallocz(sizeof(type)))) +void qemu_mutex_lock_iothread (void );void qemu_mutex_unlock_iothread (void );
FYI libvirt have been doing something similar, perhaps even more far-reaching:
http://libvirt.org/hacking.html#memalloc
http://libvirt.org/git/?p=libvirt.git;a=blob;f=src/util/memory.h;hb=HEAD
The libvirt versions are designed to catch errors in situations such as:
- trying to allocate zero-sized objects when the underlying malloc returns NULL for zero-sized objects
- trying to allocate N * M-sized objects when N * M overflows
- realloc fails, don't forget the original pointer
Rich.
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/