Re: Do we want a xmalloc or similar function in the Backend? - Mailing list pgsql-hackers

From Andres Freund
Subject Re: Do we want a xmalloc or similar function in the Backend?
Date
Msg-id 201206191950.09204.andres@2ndquadrant.com
Whole thread Raw
In response to Re: Do we want a xmalloc or similar function in the Backend?  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
On Tuesday, June 19, 2012 07:35:53 PM Robert Haas wrote:
> On Tue, Jun 19, 2012 at 10:17 AM, Andres Freund <andres@2ndquadrant.com> 
wrote:
> > There are 70+ calls of malloc in the backend in the form of
> > 
> > type* foo = malloc(sizeof(...));
> > if(!foo)
> >   elog(ERROR, "could not allocate memory");
> > 
> > which is a bit annoying to write at times. Would somebody argue against
> > introducing a function that does the above named xmalloc() or
> > malloc_or_die()?
> 
> I can't even find 70 malloc calls in the entire backend, let alone 70
> with that pattern.  Still, I don't think malloc_or_error (not die)
> would be a bad idea.
$ ack '\bmalloc\s*\(' src/backend/|wc -l
70

10-15 or so of those are comments.

The 70+ came from me running on some development branch with commitfest 
patches applied...

> But the error should definitely be written as:
> 
>                         ereport(ERROR,
>                                         (errcode(ERRCODE_OUT_OF_MEMORY),
>                                          errmsg("out of memory")));
> 
> ...not elog.
Yes, definitely. Currently some of those locations (e.g. in xlog.c) are only 
protected by Asserts... 

Andres
-- Andres Freund                       http://www.2ndQuadrant.com/PostgreSQL Development, 24x7 Support, Training &
Services


pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Do we want a xmalloc or similar function in the Backend?
Next
From: "Kevin Grittner"
Date:
Subject: Re: sortsupport for text