diff options
| author | peter <peter@pmikkelsen.com> | 2026-09-04 20:11:09 +0000 |
|---|---|---|
| committer | peter <peter@pmikkelsen.com> | 2026-09-04 20:11:09 +0000 |
| commit | d7c80ae3c76f2dd2e2ce3a39602fa7f04776d9c2 (patch) | |
| tree | fd37dc587f786c22cf69ace481fd8271a4c69449 | |
| parent | fa72ab029ddfcd050f79824bdbea95f77d8f1f60 (diff) | |
Use the same memory-failure mechanism for mallocs
FossilOrigin-Name: fc4f944f1ef599321fac8aa6af72bbe55fb438b4f43800ba37dcfdfa2e6b4c4e
| -rw-r--r-- | CMakeLists.txt | 15 | ||||
| -rw-r--r-- | src/bin/aplwc_repl.h | 7 | ||||
| -rw-r--r-- | src/bin/main.c | 34 | ||||
| -rw-r--r-- | src/bin/util.c | 16 | ||||
| -rw-r--r-- | src/bin/wrappers.c | 59 |
5 files changed, 21 insertions, 110 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 9631df4..3466f60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,6 @@ add_custom_command( add_executable(aplwc-repl src/bin/main.c src/bin/util.c - src/bin/wrappers.c ) target_link_libraries(aplwc-repl PRIVATE common aplwc) @@ -150,17 +149,3 @@ foreach(script ${TEST_SCRIPTS_BAD}) endforeach() add_test(NAME "aplwc-repl" COMMAND "aplwc-repl" "-x") - -set(REQUIRED_MALLOCS 3) -foreach(i RANGE 0 ${REQUIRED_MALLOCS}) - set(name "aplwc-repl-${i}-${REQUIRED_MALLOCS}") - add_test(NAME "${name}" COMMAND "aplwc-repl" "-x") - set_tests_properties("${name}" PROPERTIES - ENVIRONMENT "APLWC_MALLOC_FAIL_AFTER=${i}" - ) - if(NOT i EQUAL ${REQUIRED_MALLOCS} AND NOT CMAKE_BUILD_TYPE STREQUAL "Release") - set_tests_properties("${name}" PROPERTIES - WILL_FAIL TRUE - ) - endif() -endforeach() diff --git a/src/bin/aplwc_repl.h b/src/bin/aplwc_repl.h index 31a0516..4e594bc 100644 --- a/src/bin/aplwc_repl.h +++ b/src/bin/aplwc_repl.h @@ -31,13 +31,6 @@ #include <aplwc_macros.h> /* util.c */ -unsigned long getenv_ulong(const char *, unsigned long); long getenv_long(const char *, long); -/* wrappers.c */ -void *wrapped_malloc(size_t); -void wrapped_free(void *); -#define malloc(size) wrapped_malloc(size) -#define free(ptr) wrapped_free(ptr) - #endif /* APLWC_REPL_H */ diff --git a/src/bin/main.c b/src/bin/main.c index 481874c..a7acc77 100644 --- a/src/bin/main.c +++ b/src/bin/main.c @@ -26,13 +26,31 @@ static uint64_t alloc_count; static long memdbg; +static int +hook_alloc(void) +{ + alloc_count++; + if((memdbg >= 0) && (alloc_count > ((uint64_t)memdbg))) + return 0; + else + return 1; +} + +static void * +my_malloc(size_t size) +{ + if(!hook_alloc()) + return NULL; + return malloc(size); +} + static void * my_alloc(size_t min, size_t max, size_t *actual) { size_t target = min; void *p; - if(target == 0) + if(NEVER(target == 0)) target = 1; else{ target--; @@ -48,10 +66,10 @@ my_alloc(size_t min, size_t max, size_t *actual) if(max && (target > max)) target = max; - p = malloc(target); + p = my_malloc(target); if((p == NULL) && (min < target)){ target = min; - p = malloc(target); + p = my_malloc(target); } *actual = target; @@ -64,16 +82,6 @@ my_dealloc(void *p) free(p); } -static int -hook_alloc(void) -{ - alloc_count++; - if((memdbg >= 0) && (alloc_count > ((uint64_t)memdbg))) - return 0; - else - return 1; -} - int main(int argc, char *argv[]) { diff --git a/src/bin/util.c b/src/bin/util.c index c609e48..40892b0 100644 --- a/src/bin/util.c +++ b/src/bin/util.c @@ -19,22 +19,6 @@ #include "aplwc_repl.h" -unsigned long -getenv_ulong(const char *name, unsigned long def) -{ - unsigned long ret, parsed; - char *env, *p; - - ret = def; - env = getenv(name); - if((env != NULL) && (strlen(env) > 0)){ - parsed = strtoul(env, &p, 10); - if((*p) == 0) - ret = parsed; - } - return ret; -} - long getenv_long(const char *name, long def) { diff --git a/src/bin/wrappers.c b/src/bin/wrappers.c deleted file mode 100644 index e1e6de7..0000000 --- a/src/bin/wrappers.c +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com> - * - * This file is part of aplwc. - * - * Aplwc is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Aplwc is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with aplwc. If not, see <https://www.gnu.org/licenses/>. - */ - -#include "aplwc_repl.h" - -static uint64_t malloc_count = 0; - -static uint64_t -malloc_fail_after(void) -{ -#if APLWC_BUILD_TYPE_RELEASE - return UINT64_MAX; -#else - static int setup; - static uint64_t count, count_rec; - - if(!setup){ - setup = 1; - count = getenv_ulong("APLWC_MALLOC_FAIL_AFTER", ULONG_MAX); - count_rec = malloc_fail_after(); - assert(ALWAYS(count == count_rec)); - } - - return count; -#endif -} - -#undef malloc -void * -wrapped_malloc(size_t size) -{ - if(malloc_count++ >= malloc_fail_after()) - return NULL; - else - return malloc(size); -} - -#undef free -void -wrapped_free(void *ptr) -{ - free(ptr); -} |