From 970c8597f6acb489ab144302d66c4f7e00f7ab1d Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Thu, 25 May 2023 21:56:55 +0200 Subject: [PATCH] First version: create_alloc_ids_windows.awk can be used with Windows GNU GAWK by running Windows batch file create_alloc_ids_windows.bat. It`s the equivalent to linux version of create_alloc_ids.awk. --- src/create_alloc_ids_windows.awk | 46 ++++++++++++++++++++++++++++++++ src/create_alloc_ids_windows.bat | 23 ++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/create_alloc_ids_windows.awk create mode 100644 src/create_alloc_ids_windows.bat diff --git a/src/create_alloc_ids_windows.awk b/src/create_alloc_ids_windows.awk new file mode 100644 index 00000000..bb93f275 --- /dev/null +++ b/src/create_alloc_ids_windows.awk @@ -0,0 +1,46 @@ +#!/usr/bin/gawk -f +# replaces _ALLOC_ID_ in all source files with unique ID for memory tracking + +BEGIN{ + if(ARGC <= 1) { + print "Usage: " ENVIRON["_"] " create|reset " + exit + } + create_id = 0 + if(ARGV[1] == "create") create_id = 1 + else if (ARGV[1] != "reset") { + print "Usage: " ENVIRON["_"] " create|reset " + exit + } + cnt = 0; + filename = ARGV[2] + if(filename ~ /expandlabel\.c/) exit + if(filename ~ /expandlabel\.h/) exit + if(filename ~ /parselabel\.c/) exit + f = "" + start = 1 + changed = 0 + while(getline < filename) { + if(create_id == 0) { + if(!start) f = f "\n" + str = gensub(/(my_(malloc|calloc|realloc|free|strcat|strncat|mstrcat|strdup|strdup2))\([0-9]+,/, "\\1(_ALLOC_ID_,", "G") + if(str != $0) changed = 1 + f = f str + } else { + if(!start) f = f "\n" + if($0 !~ /^#define *_ALLOC_ID_/) { + while(sub(/_ALLOC_ID_/, cnt)) { + changed = 1 + cnt++ + } + } + f = f $0 + } + start = 0 + } + close(filename) + if(changed) { + print f > filename + close(filename) + } +} diff --git a/src/create_alloc_ids_windows.bat b/src/create_alloc_ids_windows.bat new file mode 100644 index 00000000..b84127a2 --- /dev/null +++ b/src/create_alloc_ids_windows.bat @@ -0,0 +1,23 @@ +@echo off + +if %1. EQU . ( + echo %~0 [reset or create] FILE + goto end +) +if %1% equ create ( + for /F "usebackq delims=" %%A in (`dir /b *.c`) do gawk -f create_alloc_ids_windows.awk %1 %%A + for /F "usebackq delims=" %%A in (`dir /b *.h`) do gawk -f create_alloc_ids_windows.awk %1 %%A + for /F "usebackq delims=" %%A in (`dir /b *.y`) do gawk -f create_alloc_ids_windows.awk %1 %%A + for /F "usebackq delims=" %%A in (`dir /b *.l`) do gawk -f create_alloc_ids_windows.awk %1 %%A + goto end +) +if %1% equ reset ( + for /F "usebackq delims=" %%A in (`dir /b *.c`) do gawk -f create_alloc_ids_windows.awk %1 %%A + for /F "usebackq delims=" %%A in (`dir /b *.h`) do gawk -f create_alloc_ids_windows.awk %1 %%A + for /F "usebackq delims=" %%A in (`dir /b *.y`) do gawk -f create_alloc_ids_windows.awk %1 %%A + for /F "usebackq delims=" %%A in (`dir /b *.l`) do gawk -f create_alloc_ids_windows.awk %1 %%A + goto end +) +echo %~0 [reset or create] FILE + +:end \ No newline at end of file