postgresql/src/backend/utils/cache
David Rowley 439f61757f Add palloc_aligned() to allow aligned memory allocations
This introduces palloc_aligned() and MemoryContextAllocAligned() which
allow callers to obtain memory which is allocated to the given size and
also aligned to the specified alignment boundary.  The alignment
boundaries may be any power-of-2 value.  Currently, the alignment is
capped at 2^26, however, we don't expect values anything like that large.
The primary expected use case is to align allocations to perhaps CPU
cache line size or to maybe I/O page size.  Certain use cases can benefit
from having aligned memory by either having better performance or more
predictable performance.

The alignment is achieved by requesting 'alignto' additional bytes from
the underlying allocator function and then aligning the address that is
returned to the requested alignment.  This obviously does waste some
memory, so alignments should be kept as small as what is required.

It's also important to note that these alignment bytes eat into the
maximum allocation size.  So something like:

palloc_aligned(MaxAllocSize, 64, 0);

will not work as we cannot request MaxAllocSize + 64 bytes.

Additionally, because we're just requesting the requested size plus the
alignment requirements from the given MemoryContext, if that context is
the Slab allocator, then since slab can only provide chunks of the size
that's specified when the slab context is created, then this is not going
to work.  Slab will generate an error to indicate that the requested size
is not supported.

The alignment that is requested in palloc_aligned() is stored along with
the allocated memory.  This allows the alignment to remain intact through
repalloc() calls.

Author: Andres Freund, David Rowley
Reviewed-by: Maxim Orlov, Andres Freund, John Naylor
Discussion: https://postgr.es/m/CAApHDvpxLPUMV1mhxs6g7GNwCP6Cs6hfnYQL5ffJQTuFAuxt8A%40mail.gmail.com
2022-12-22 13:32:05 +13:00
..
attoptcache.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
catcache.c Add palloc_aligned() to allow aligned memory allocations 2022-12-22 13:32:05 +13:00
evtcache.c Add construct_array_builtin, deconstruct_array_builtin 2022-07-01 11:23:15 +02:00
inval.c Revert 56-bit relfilenode change and follow-up commits. 2022-09-28 09:55:28 -04:00
lsyscache.c Don't read MCV stats needlessly in eqjoinsel(). 2022-11-18 11:01:03 -05:00
Makefile Change internal RelFileNode references to RelFileNumber or RelFileLocator. 2022-07-06 11:39:09 -04:00
meson.build Add copyright notices to meson files 2022-12-20 07:54:39 -05:00
partcache.c Resolve partition strategy during early parsing 2022-11-03 16:25:54 +01:00
plancache.c Clean up some inconsistencies with GUC declarations 2022-10-31 12:44:48 +09:00
relcache.c Rework query relation permission checking 2022-12-06 16:09:24 +01:00
relfilenumbermap.c Revert 56-bit relfilenode change and follow-up commits. 2022-09-28 09:55:28 -04:00
relmapper.c Harmonize more parameter names in bulk. 2022-09-20 13:09:30 -07:00
spccache.c Update copyright for 2022 2022-01-07 19:04:57 -05:00
syscache.c Static assertions cleanup 2022-12-15 10:10:32 +01:00
ts_cache.c Store GUC data in a memory context, instead of using malloc(). 2022-10-14 12:10:48 -04:00
typcache.c Add repalloc0 and repalloc0_array 2022-11-12 20:34:44 +01:00