pg_malloc0() instead of pg_malloc()+memset()

Started by Daniel Gustafssonabout 7 years ago4 messageshackers
Jump to latest
#1Daniel Gustafsson
daniel@yesql.se

When reading another codepath, I happened to notice a few codepaths where we do
pg_malloc() immediately followed by a memset( .. 0, ..), without there being a
justification (that I can see) for not using pg_malloc0() instead. The attached
patch changes to pg_malloc0(), and passes make check.

cheers ./daniel

Attachments:

pg_malloc0.patchapplication/octet-stream; name=pg_malloc0.patchDownload+3-7
#2Michael Paquier
michael@paquier.xyz
In reply to: Daniel Gustafsson (#1)
Re: pg_malloc0() instead of pg_malloc()+memset()

On Mon, Mar 25, 2019 at 01:18:05PM +0000, Daniel Gustafsson wrote:

When reading another codepath, I happened to notice a few codepaths where we do
pg_malloc() immediately followed by a memset( .. 0, ..), without there being a
justification (that I can see) for not using pg_malloc0() instead. The attached
patch changes to pg_malloc0(), and passes make check.

If we simplify all of them (and that's not really a big deal), I have
spotted two extra places on top of what you noticed, one in gist.c
where ROTATEDIST is defined and a second one in tablefunc.c.

Thoughts?
--
Michael

Attachments:

alloc-memset-v2.patchtext/x-diff; charset=us-asciiDownload+6-13
#3Daniel Gustafsson
daniel@yesql.se
In reply to: Michael Paquier (#2)
Re: pg_malloc0() instead of pg_malloc()+memset()

On Tuesday, March 26, 2019 9:00 AM, Michael Paquier <michael@paquier.xyz> wrote:

On Mon, Mar 25, 2019 at 01:18:05PM +0000, Daniel Gustafsson wrote:

When reading another codepath, I happened to notice a few codepaths where we do
pg_malloc() immediately followed by a memset( .. 0, ..), without there being a
justification (that I can see) for not using pg_malloc0() instead. The attached
patch changes to pg_malloc0(), and passes make check.

If we simplify all of them (and that's not really a big deal), I have
spotted two extra places on top of what you noticed, one in gist.c
where ROTATEDIST is defined and a second one in tablefunc.c.

Nice, I had missed them as I my eyes set on pg_malloc(). I've done another pass over
the codebase and I can't spot any other on top of the additional ones you found where
MemSet() in palloc0 is preferrable over memset().

cheers ./daniel

#4Michael Paquier
michael@paquier.xyz
In reply to: Daniel Gustafsson (#3)
Re: pg_malloc0() instead of pg_malloc()+memset()

On Tue, Mar 26, 2019 at 09:14:46AM +0000, Daniel Gustafsson wrote:

Nice, I had missed them as I my eyes set on pg_malloc(). I've done another pass over
the codebase and I can't spot any other on top of the additional ones you found where
MemSet() in palloc0 is preferrable over memset().

Same here, committed. Just for the note, I have been just using git
grep -A to look at the follow-up lines of any allocation calls.
--
Michael