[Spice-devel] Added spice allocators
Alexander Larsson
alexl at redhat.com
Thu Mar 11 03:34:43 PST 2010
I just pushed a set of changes that add custom allocators for spice and
switches all uses of raw malloc() to use these. This is based on current
best practices and has several advantages:
* All out-of-memory allocation failures are handled in a single place,
and in a consistent way. This lets us delete a lot of open-coded
checks and aborts.
* It also ensures we always abort on malloc returning NULL. This how
qemu handles OOM and in general the only sane way. See e.g.
http://article.gmane.org/gmane.comp.audio.jackit/19998
* There are special allocators for allocating something of the form
malloc(element_size * num_elements) that abort if the multiplication
overflows (which could make the result a small number and make the
allocation unexpectedly succeed).
* We now have some macros like spice_new() that makes it easier to
allocate a structure type or array of those, avoiding lots of casts
and sizeof operators.
* There are also zeroing versions of all allocators, so that you can
avoid a separate memset call.
Here is a typical change to show how this makes stuff cleaner:
- if (!(dispatcher = malloc(sizeof(RedDispatcher)))) {
- red_error("malloc failed");
- }
- memset(dispatcher, 0, sizeof(RedDispatcher));
+ dispatcher = spice_new0(RedDispatcher, 1);
--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Alexander Larsson Red Hat, Inc
alexl at redhat.com alexander.larsson at gmail.com
He's a Nobel prize-winning pirate card sharp fleeing from a secret government
programme. She's a sharp-shooting insomniac fairy princess with a
flame-thrower. They fight crime!
More information about the Spice-devel
mailing list