Skip to content

Conversation

@hanno-becker
Copy link
Contributor

@hanno-becker hanno-becker commented Dec 15, 2025

This commit introduces the configuration option

MLK_CONFIG_CUSTOM_ALLOC_FREE

which allows users to provide custom macros

MLK_CUSTOM_ALLOC(v, T, N)
MLK_CUSTOM_FREE(v, T, N)

that should be used in place of the default stack-allocation
to allocate/free large internal structures. Those macros are
then wrapped into MLK_ALLOC/MLK_FREE and used in the source.
Importantly, MLK_FREE adds zeroization ahead of calling
MLK_CUSTOM_FREE, so the latter need not take care of this.

The macros are put to use in kem.c and indcpa.c, but not yet
further down the call-stack.

The option is documented as experimental and (hence) unstable
so we have freedom to adjust this ahead of v2.

A custom configuration is added which implements the macros
based on C11's aligned_alloc. We enable the leak sanitizer
in the tests for this configuration to catch any missing calls
to MLK_FREE.

Since allocation can fail, additional error paths are introduced
to the functions using MLK_ALLOC. This uniformly follows the pattern
of all pointers being initialized to NULL before allocation, and a
cleanup section calling MLK_FREE on them before returning the result.

To distinguish between out-of-memory failure and other kinds of
functional failures, we introduce named error codes MLK_ERR_FAIL
and MLK_ERR_OUT_OF_MEMORY. This will likely need further refinement,
but this can only be done in v2.

Signed-off-by: Hanno Becker [email protected]

@hanno-becker hanno-becker changed the title Alloc free config 2 Make allocation of large structures/buffers configurable (without workspaces) Dec 15, 2025
@hanno-becker hanno-becker force-pushed the alloc_free_config_2 branch 2 times, most recently from 6dba3fb to 1bf050d Compare December 15, 2025 06:46
@hanno-becker hanno-becker force-pushed the alloc_free_config_2 branch 8 times, most recently from e7ce05a to db0a93a Compare December 16, 2025 05:53
@hanno-becker hanno-becker marked this pull request as ready for review December 16, 2025 05:54
@hanno-becker hanno-becker requested a review from a team as a code owner December 16, 2025 05:54
@hanno-becker hanno-becker force-pushed the alloc_free_config_2 branch 3 times, most recently from db0a93a to 5d4bc68 Compare December 16, 2025 06:32
Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hanno-becker. I only found some minor issues. Rest looks good.

Copy link
Contributor

@mkannwischer mkannwischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @hanno-becker. LGTM now.

This commit introduces the configuration option

  MLK_CONFIG_CUSTOM_ALLOC_FREE

which allows users to provide custom macros

  MLK_CUSTOM_ALLOC(v, T, N)
  MLK_CUSTOM_FREE(v, T, N)

that should be used in place of the default stack-allocation
to allocate/free large internal structures. Those macros are
then wrapped into MLK_ALLOC/MLK_FREE and used in the source.
Importantly, MLK_FREE adds zeroization ahead of calling
MLK_CUSTOM_FREE, so the latter need not take care of this.

The macros are put to use in kem.c and indcpa.c, but not yet
further down the call-stack.

The option is documented as experimental and (hence) unstable
so we have freedom to adjust this ahead of v2.

A custom configuration is added which implements the macros
based on C11's `aligned_alloc`. We enable the leak sanitizer
in the tests for this configuration to catch any missing calls
to MLK_FREE.

Since allocation can fail, additional error paths are introduced
to the functions using MLK_ALLOC. This uniformly follows the pattern
of all pointers being initialized to NULL before allocation, and a
cleanup section calling MLK_FREE on them before returning the result.

To distinguish between out-of-memory failure and other kinds of
functional failures, we introduce named error codes MLK_ERR_FAIL
and MLK_ERR_OUT_OF_MEMORY. This will likely need further refinement,
but this can only be done in v2.

Signed-off-by: Hanno Becker <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce configurable alloc/free macros for local structures

3 participants