[PATCH weston 03/10] tests: Add an xmalloc helper function

Bryce Harrington bryce at osg.samsung.com
Mon May 11 21:49:05 PDT 2015


On Mon, May 11, 2015 at 01:39:29PM +0300, Pekka Paalanen wrote:
> On Wed,  6 May 2015 17:44:22 -0700
> Bryce Harrington <bryce at osg.samsung.com> wrote:
> 
> > Signed-off-by: Bryce Harrington <bryce at osg.samsung.com>
> > ---
> >  tests/weston-test-client-helper.c | 11 +++++++++++
> >  tests/weston-test-client-helper.h | 16 ++++++++++------
> >  2 files changed, 21 insertions(+), 6 deletions(-)
> > 
> > diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c
> > index b05be83..54b6c95 100644
> > --- a/tests/weston-test-client-helper.c
> > +++ b/tests/weston-test-client-helper.c
> > @@ -32,6 +32,17 @@
> >  #include "../shared/os-compatibility.h"
> >  #include "weston-test-client-helper.h"
> >  
> > +void *
> > +fail_on_null(void *p)
> > +{
> > +	if (p == NULL) {
> > +		fprintf(stderr, "out of memory\n");
> > +		exit(EXIT_FAILURE);
> > +	}
> > +	return p;
> > +}
> > +
> > +
> >  int
> >  surface_contains(struct surface *surface, int x, int y)
> >  {
> > diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h
> > index 429f088..0378804 100644
> > --- a/tests/weston-test-client-helper.h
> > +++ b/tests/weston-test-client-helper.h
> > @@ -132,15 +132,19 @@ struct surface {
> >  	void *data;
> >  };
> >  
> > +void *
> > +fail_on_null(void *p);
> > +
> >  static inline void *
> > -xzalloc(size_t size)
> > +xzalloc(size_t s)
> >  {
> > -        void *p;
> > -
> > -        p = calloc(1, size);
> > -        assert(p);
> > +	return fail_on_null(calloc(1, s));
> > +}
> >  
> > -        return p;
> > +static inline void *
> > +xmalloc(size_t s)
> > +{
> > +	return fail_on_null(malloc(s));
> >  }
> >  
> >  struct client *
> 
> Hi,
> 
> so the motivation for this patch is to make alloc failures just exit
> the process with EXIT_FAILURE than calling abort()? Or is to stop
> relying on assert()? Or to add the printf? Or just code consistency?
> 
> Would be nice to know why... maybe I'll learn in the later patches.

Actually, the original motivation was that I needed an xmalloc.  I
noticed there was xzalloc already defined but it was done differently
from the one in clients/window.c.  So this patch is making the
definitions (and behavior) consistent across the codebase.

I considered moving the functions to the shared directory but felt like
they were not yet being used widely enough to justify that.  window.c
actually has a set of alloc routines that would be handy to make more
convenient for test writers.

The assert-vs-exit difference didn't seem like a big concern here.
Either way the test harness will count it as a failure.
 
Bryce


More information about the wayland-devel mailing list