Re: Add palloc_aligned() to allow arbitrary power of 2 memory alignment

David Rowley <dgrowleyml@gmail.com>

From: David Rowley <dgrowleyml@gmail.com>
To: John Naylor <john.naylor@enterprisedb.com>
Cc: Andres Freund <andres@anarazel.de>, PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Date: 2022-11-15T10:46:17Z
Lists: pgsql-hackers
On Mon, 14 Nov 2022 at 15:25, John Naylor <john.naylor@enterprisedb.com> wrote:
> - Assert((char *) chunk > (char *) block);
> + Assert((char *) chunk >= (char *) block);
>
> Is this related or independent?

It's related.  Because the code is doing:

MemoryChunkSetHdrMask(alignedchunk, unaligned, alignto,
  MCTX_ALIGNED_REDIRECT_ID);

Here the blockoffset gets set to the difference between alignedchunk
and unaligned. Typically when we call MemoryChunkSetHdrMask, the
blockoffset is always the difference between the block and
MemoryChunk, which is never 0 due to the block header fields.  Here it
can be the same pointer when the redirection MemoryChunk is stored on
the first byte of the palloc'd address.  This can happen if the
address returned by palloc + sizeof(MemoryChunk) is aligned to what we
need already.

David



Commits

  1. Add palloc_aligned() to allow aligned memory allocations