Re: Safe memory allocation functions

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Andres Freund <andres(at)2ndquadrant(dot)com>
Cc: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Safe memory allocation functions
Date: 2015-01-28 14:34:35
Message-ID: CAB7nPqQnCkE9NqfAbDq+GMptEBFWo83dG3H00BDkfOaZ1V6HWQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jan 27, 2015 at 5:34 PM, Andres Freund <andres(at)2ndquadrant(dot)com> wrote:
> On 2015-01-27 17:27:53 +0900, Michael Paquier wrote:
>> Alvaro Herrera wrote:
>> >> So how about something like
>> >>
>> >> #define ALLOCFLAG_HUGE 0x01
>> >> #define ALLOCFLAG_NO_ERROR_ON_OOM 0x02
>> >> void *
>> >> MemoryContextAllocFlags(MemoryContext context, Size size, int flags);
>> The flag for huge allocations may be useful, but I don't actually see
>> much value in the flag ALLOC_NO_OOM if the stuff in aset.c returns
>> unconditionally NULL in case of an OOM and we let palloc complain
>> about an OOM when allocation returns NULL. Something I am missing
>> perhaps?
>
> I guess the idea is to have *user facing* MemoryContextAllocExtended()
> that can do both huge and no-oom allocations. Otherwise we need palloc
> like wrappers for all combinations.
> We're certainly not just going to ignore memory allocation failures
> generally in in MemoryContextAllocExtended()....
As a result of all the comments on this thread, here are 3 patches
implementing incrementally the different ideas from everybody:
1) 0001 modifies aset.c to return unconditionally NULL in case of an
OOM instead of reporting an error. All the OOM error reports are moved
to mcxt.c (MemoryContextAlloc* and palloc*)
2) 0002 adds the noerror routines for frontend and backend.
3) 0003 adds MemoryContextAllocExtended that can be called with the
following control flags:
#define ALLOC_HUGE 0x01 /* huge allocation */
#define ALLOC_ZERO 0x02 /* clear allocated memory */
#define ALLOC_NO_OOM 0x04 /* no failure if out-of-memory */
#define ALLOC_ALIGNED 0x08 /* request length suitable for MemSetLoop */
This groups MemoryContextAlloc, MemoryContextAllocHuge,
MemoryContextAllocZero and MemoryContextAllocZeroAligned under the
same central routine.
Regards,
--
Michael

Attachment Content-Type Size
0001-Make-allocation-return-functions-return-NULL-on-OOM.patch text/x-patch 6.6 KB
0002-Add-_noerror-routines-for-palloc-memory-allocation.patch text/x-patch 9.3 KB
0003-Create-MemoryContextAllocExtended-central-routine-fo.patch text/x-patch 5.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kevin Grittner 2015-01-28 14:43:35 Re: pg_dump with both --serializable-deferrable and -j
Previous Message Thom Brown 2015-01-28 14:12:49 Re: Parallel Seq Scan