From 440d414fa7e0396d0838d5b04b92d66c06b6e853 Mon Sep 17 00:00:00 2001 From: Phillip Johnston Date: Wed, 30 Mar 2022 11:50:11 -0700 Subject: [PATCH] Add a build option that can be used to override the free list `static` declaration. Fixes #84 --- meson_options.txt | 2 ++ src/malloc_freelist.c | 13 ++++++++++++- src/meson.build | 10 ++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/meson_options.txt b/meson_options.txt index fece376..17af243 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -8,3 +8,5 @@ option('libc-subproject', type: 'array', value: ['libc', 'libc_dep', 'libc_hosted_native_dep'], yield: true, description: 'This array is used in combination with use-libc-subproject. The first entry is the subproject name. The second is the cross-compilation dependency to use. The third value is optional. If used, it is a native dependency to use with native library targets.') +option('freelist-declared-static', type:'boolean', value: true, yield: true, + description: 'Set to false to make the freelist data structure accessible outside of the malloc_freelist.c file. By default, it has static linkage.') diff --git a/src/malloc_freelist.c b/src/malloc_freelist.c index e79136c..832e13e 100644 --- a/src/malloc_freelist.c +++ b/src/malloc_freelist.c @@ -8,6 +8,17 @@ #include #include +/// By default, the freelist is declared as static so that it cannot be accessed +/// outside of the library. Users who wish to override this default declaration +/// can define `FREELIST_DECL_SPECIFIERS` to use an alternative. +/// One option is to make it an empty definition to make it publicly visible, +/// which may be useful for capturing state or performing metadata analysis. +/// +/// Unless you have a specific use case, we recommend sticking with the default. +#ifndef FREELIST_DECL_SPECIFIERS +#define FREELIST_DECL_SPECIFIERS static +#endif + #pragma mark - Definitions - /** @@ -70,7 +81,7 @@ void malloc_unlock(); #pragma mark - Declarations - // This macro simply declares and initializes our linked list -static LIST_INIT(free_list); +FREELIST_DECL_SPECIFIERS LIST_INIT(free_list); #pragma mark - Private Functions - diff --git a/src/meson.build b/src/meson.build index 56d7fd4..3bf105c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -134,9 +134,18 @@ libmemory_assert_native_dep = declare_dependency( # Freelist # ############ +freelist_compile_args = [] +if get_option('freelist-declared-static') == false + # Remove the static declaration option from the free list struct + # The = with an empty string is needed to prevent the compiler + # from unhelpfully making it FREELIST_DECL_SPECIFIERS=1 on our behalf + freelist_compile_args += '-DFREELIST_DECL_SPECIFIERS=' +endif + libmemory_freelist = static_library( 'memory_freelist', [common_files, 'malloc_freelist.c'], + c_args: freelist_compile_args, include_directories: [libmemory_includes, dependency_includes], dependencies: libc_dep, # Do not built by default if we are a subproject @@ -146,6 +155,7 @@ libmemory_freelist = static_library( libmemory_freelist_native = static_library( 'memory_freelist_native', [common_files, 'malloc_freelist.c'], + c_args: freelist_compile_args, include_directories: [libmemory_includes, dependency_includes], dependencies: libc_native_dep, native: true,