moved the xma mmgr to std.c with the new function hawk_init_xma_mmgr() and hawk_fini_xma_mmgr()

This commit is contained in:
2025-06-03 21:53:33 +09:00
parent 2a03c6f061
commit 715085c778
28 changed files with 3298 additions and 720 deletions

View File

@ -30,8 +30,8 @@
#include <hawk-utl.h>
#include <hawk-fmt.h>
#include <hawk-cli.h>
#include <hawk-xma.h>
#include <hawk-glob.h>
#include <hawk-xma.h>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
@ -75,7 +75,6 @@
#endif
static hawk_rtx_t* app_rtx = HAWK_NULL;
static int app_debug = 0;
typedef struct gv_t gv_t;
typedef struct gvm_t gvm_t;
@ -123,11 +122,9 @@ struct arg_t
unsigned int classic: 1;
int opton;
int optoff;
int debug;
hawk_uintptr_t memlimit;
#if defined(HAWK_BUILD_DEBUG)
hawk_uintptr_t failmalloc;
#endif
};
@ -547,9 +544,6 @@ static void print_usage (FILE* out, const hawk_bch_t* argv0, const hawk_bch_t* r
fprintf (out, " -m/--memory-limit number limit the memory usage (bytes)\n");
fprintf (out, " -w expand datafile wildcards\n");
#if defined(HAWK_BUILD_DEBUG)
fprintf (out, " -X number fail the number'th memory allocation\n");
#endif
#if defined(HAWK_OOCH_IS_UCH)
fprintf (out, " --script-encoding string specify script file encoding name\n");
fprintf (out, " --console-encoding string specify console encoding name\n");
@ -674,7 +668,6 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar
{ ":field-separator", 'F' },
{ ":assign", 'v' },
{ ":memory-limit", 'm' },
{ ":mode", 'M' },
{ ":script-encoding", '\0' },
{ ":console-encoding", '\0' },
@ -690,11 +683,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar
static hawk_bcli_t opt =
{
#if defined(HAWK_BUILD_DEBUG)
"hDc:f:d:t:F:v:m:I:wX:",
#else
"hDc:f:d:t:F:v:m:I:w",
#endif
lng
};
@ -726,7 +715,7 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar
goto oops;
case 'D':
app_debug = 1;
arg->debug = 1;
break;
case 'c':
@ -843,15 +832,6 @@ static int process_argv (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_ar
break;
}
#if defined(HAWK_BUILD_DEBUG)
case 'X':
{
arg->failmalloc = strtoul(opt.arg, HAWK_NULL, 10);
break;
}
#endif
case '\0':
{
/* a long option with no corresponding short option */
@ -1033,89 +1013,12 @@ static void print_hawk_rtx_error (hawk_rtx_t* rtx)
);
}
static void* xma_alloc (hawk_mmgr_t* mmgr, hawk_oow_t size)
{
return hawk_xma_alloc(mmgr->ctx, size);
}
static void* xma_realloc (hawk_mmgr_t* mmgr, void* ptr, hawk_oow_t size)
{
return hawk_xma_realloc(mmgr->ctx, ptr, size);
}
static void xma_free (hawk_mmgr_t* mmgr, void* ptr)
{
hawk_xma_free (mmgr->ctx, ptr);
}
static hawk_mmgr_t xma_mmgr =
{
xma_alloc,
xma_realloc,
xma_free,
HAWK_NULL
};
static void xma_dumper_without_hawk (void* ctx, const hawk_bch_t* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
#if defined(HAWK_BUILD_DEBUG)
static hawk_uintptr_t debug_mmgr_count = 0;
static hawk_uintptr_t debug_mmgr_alloc_count = 0;
static hawk_uintptr_t debug_mmgr_realloc_count = 0;
static hawk_uintptr_t debug_mmgr_free_count = 0;
static void* debug_mmgr_alloc (hawk_mmgr_t* mmgr, hawk_oow_t size)
{
void* ptr;
struct arg_t* arg = (struct arg_t*)mmgr->ctx;
debug_mmgr_count++;
if (debug_mmgr_count % arg->failmalloc == 0) return HAWK_NULL;
ptr = malloc (size);
if (ptr) debug_mmgr_alloc_count++;
return ptr;
}
static void* debug_mmgr_realloc (hawk_mmgr_t* mmgr, void* ptr, hawk_oow_t size)
{
void* rptr;
struct arg_t* arg = (struct arg_t*)mmgr->ctx;
debug_mmgr_count++;
if (debug_mmgr_count % arg->failmalloc == 0) return HAWK_NULL;
rptr = realloc (ptr, size);
if (rptr)
{
if (ptr) debug_mmgr_realloc_count++;
else debug_mmgr_alloc_count++;
}
return rptr;
}
static void debug_mmgr_free (hawk_mmgr_t* mmgr, void* ptr)
{
debug_mmgr_free_count++;
free (ptr);
}
static hawk_mmgr_t debug_mmgr =
{
debug_mmgr_alloc,
debug_mmgr_realloc,
debug_mmgr_free,
HAWK_NULL
};
#endif
static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0)
{
hawk_t* hawk = HAWK_NULL;
hawk_rtx_t* rtx = HAWK_NULL;
hawk_val_t* retv;
hawk_mmgr_t xma_mmgr;
int i;
struct arg_t arg;
int ret = -1;
@ -1150,18 +1053,9 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[], const hawk_bc
psout.u.fileb.cmgr = arg.script_cmgr;
}
#if defined(HAWK_BUILD_DEBUG)
if (arg.failmalloc > 0)
{
debug_mmgr.ctx = &arg;
mmgr = &debug_mmgr;
}
else
#endif
if (arg.memlimit > 0)
{
xma_mmgr.ctx = hawk_xma_open(hawk_get_sys_mmgr(), 0, HAWK_NULL, arg.memlimit);
if (xma_mmgr.ctx == HAWK_NULL)
if (hawk_init_xma_mmgr(&xma_mmgr, arg.memlimit) <= -1)
{
print_error ("cannot open memory heap\n");
goto oops;
@ -1180,7 +1074,7 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[], const hawk_bc
if (arg.modern) i = HAWK_MODERN;
else if (arg.classic) i = HAWK_CLASSIC;
else hawk_getopt (hawk, HAWK_OPT_TRAIT, &i);
else hawk_getopt(hawk, HAWK_OPT_TRAIT, &i);
if (arg.opton) i |= arg.opton;
if (arg.optoff) i &= ~arg.optoff;
hawk_setopt (hawk, HAWK_OPT_TRAIT, &i);
@ -1293,7 +1187,7 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[], const hawk_bc
hawk_int_t tmp;
hawk_rtx_refdownval (rtx, retv);
if (app_debug) dprint_return (rtx, retv);
if (arg.debug) dprint_return (rtx, retv);
ret = 0;
if (hawk_rtx_valtoint(rtx, retv, &tmp) >= 0) ret = tmp;
@ -1310,27 +1204,13 @@ oops:
unset_intr_pipe ();
if (xma_mmgr.ctx)
if (arg.memlimit > 0)
{
if (app_debug) hawk_xma_dump (xma_mmgr.ctx, xma_dumper_without_hawk, HAWK_NULL);
hawk_xma_close (xma_mmgr.ctx);
if (arg.debug) hawk_xma_dump (xma_mmgr.ctx, main_xma_dumper_without_hawk, HAWK_NULL);
hawk_fini_xma_mmgr (&xma_mmgr);
}
freearg (&arg);
#if defined(HAWK_BUILD_DEBUG)
/*
if (arg.failmalloc > 0)
{
hawk_fprintf (HAWK_STDERR, HAWK_T("\n"));
hawk_fprintf (HAWK_STDERR, HAWK_T("-[MALLOC COUNTS]---------------------------------------\n"));
hawk_fprintf (HAWK_STDERR, HAWK_T("ALLOC: %lu FREE: %lu: REALLOC: %lu\n"),
(unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count);
hawk_fprintf (HAWK_STDERR, HAWK_T("-------------------------------------------------------\n"));
}*/
#endif
return ret;
}

View File

@ -26,8 +26,19 @@
#include "main.h"
#include <hawk.h>
#include <hawk-xma.h>
#include <stdio.h>
void main_xma_dumper_without_hawk (void* ctx, const hawk_bch_t* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
/* -------------------------------------------------------- */
static int main_version(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0)
{
printf ("%s %s\n", hawk_get_base_name_bcstr(real_argv0), HAWK_PACKAGE_VERSION);

View File

@ -36,6 +36,8 @@ extern "C" {
int main_hawk(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0);
int main_sed(int argc, hawk_bch_t* argv[], const hawk_bch_t* real_argv0);
void main_xma_dumper_without_hawk (void* ctx, const hawk_bch_t* fmt, ...);
#if defined(__cplusplus)
}

174
bin/sed.c
View File

@ -23,13 +23,14 @@
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "main.h"
#include <hawk-sed.h>
#include <hawk-xma.h>
#include <hawk-cli.h>
#include <hawk-fmt.h>
#include <hawk-utl.h>
#include <hawk-std.h>
#include <hawk-xma.h>
#if !defined(_GNU_SOURCE)
# define _GNU_SOURCE
@ -87,89 +88,14 @@ struct arg_t
int inplace;
int wildcard;
int debug;
#if defined(HAWK_ENABLE_SEDTRACER)
int trace;
#endif
hawk_uintptr_t memlimit;
#if defined(HAWK_BUILD_DEBUG)
hawk_uintptr_t failmalloc;
#endif
};
/* ------------------------------------------------------------------- */
static void* xma_alloc (hawk_mmgr_t* mmgr, hawk_oow_t size)
{
return hawk_xma_alloc (mmgr->ctx, size);
}
static void* xma_realloc (hawk_mmgr_t* mmgr, void* ptr, hawk_oow_t size)
{
return hawk_xma_realloc (mmgr->ctx, ptr, size);
}
static void xma_free (hawk_mmgr_t* mmgr, void* ptr)
{
hawk_xma_free (mmgr->ctx, ptr);
}
static hawk_mmgr_t xma_mmgr =
{
xma_alloc,
xma_realloc,
xma_free,
HAWK_NULL
};
/* ------------------------------------------------------------------- */
#if defined(HAWK_BUILD_DEBUG)
static hawk_uintptr_t debug_mmgr_count = 0;
static hawk_uintptr_t debug_mmgr_alloc_count = 0;
static hawk_uintptr_t debug_mmgr_realloc_count = 0;
static hawk_uintptr_t debug_mmgr_free_count = 0;
static void* debug_mmgr_alloc (hawk_mmgr_t* mmgr, hawk_oow_t size)
{
void* ptr;
struct arg_t* arg = (struct arg_t*)mmgr->ctx;
debug_mmgr_count++;
if (debug_mmgr_count % arg->failmalloc == 0) return HAWK_NULL;
ptr = malloc(size);
if (ptr) debug_mmgr_alloc_count++;
return ptr;
}
static void* debug_mmgr_realloc (hawk_mmgr_t* mmgr, void* ptr, hawk_oow_t size)
{
void* rptr;
struct arg_t* arg = (struct arg_t*)mmgr->ctx;
debug_mmgr_count++;
if (debug_mmgr_count % arg->failmalloc == 0) return HAWK_NULL;
rptr = realloc(ptr, size);
if (rptr)
{
if (ptr) debug_mmgr_realloc_count++;
else debug_mmgr_alloc_count++;
}
return rptr;
}
static void debug_mmgr_free (hawk_mmgr_t* mmgr, void* ptr)
{
debug_mmgr_free_count++;
free (ptr);
}
static hawk_mmgr_t debug_mmgr =
{
debug_mmgr_alloc,
debug_mmgr_realloc,
debug_mmgr_free,
HAWK_NULL
};
#endif
/* ------------------------------------------------------------------- */
static void print_error (const hawk_bch_t* fmt, ...)
@ -201,33 +127,31 @@ static void print_usage (FILE* out, const hawk_bch_t* argv0, const hawk_bch_t* r
fprintf (out, " %s%s%s [options] -e script [file]\n", b1, b2, b3);
fprintf (out, "Options as follows:\n");
fprintf (out, " -h/--help show this message\n");
fprintf (out, " -n disable auto-print\n");
fprintf (out, " -e script specify a script\n");
fprintf (out, " -f file specify a script file\n");
fprintf (out, " -o file specify an output file\n");
fprintf (out, " -r use the extended regular expression\n");
fprintf (out, " -R enable non-standard extensions to the regular\n");
fprintf (out, " expression\n");
fprintf (out, " -i perform in-place editing. imply -s\n");
fprintf (out, " -s process input files separately\n");
fprintf (out, " -a perform strict address and label check\n");
fprintf (out, " -b allow extended address formats\n");
fprintf (out, " <start~step>,<start,+line>,<start,~line>,<0,/regex/>\n");
fprintf (out, " -x allow text on the same line as c, a, i\n");
fprintf (out, " -y ensure a newline at text end\n");
fprintf (out, " -m number specify the maximum amount of memory to use in bytes\n");
fprintf (out, " -w expand file wildcards\n");
fprintf (out, " -h/--help show this message\n");
fprintf (out, " -D show extra information\n");
fprintf (out, " -n disable auto-print\n");
fprintf (out, " -e script specify a script\n");
fprintf (out, " -f file specify a script file\n");
fprintf (out, " -o file specify an output file\n");
fprintf (out, " -r use the extended regular expression\n");
fprintf (out, " -R enable non-standard extensions to the regular\n");
fprintf (out, " expression\n");
fprintf (out, " -i perform in-place editing. imply -s\n");
fprintf (out, " -s process input files separately\n");
fprintf (out, " -a perform strict address and label check\n");
fprintf (out, " -b allow extended address formats\n");
fprintf (out, " <start~step>,<start,+line>,<start,~line>,<0,/regex/>\n");
fprintf (out, " -x allow text on the same line as c, a, i\n");
fprintf (out, " -y ensure a newline at text end\n");
fprintf (out, " -m/--memory-limit number specify the maximum amount of memory to use in bytes\n");
fprintf (out, " -w expand file wildcards\n");
#if defined(HAWK_ENABLE_SEDTRACER)
fprintf (out, " -t print command traces\n");
#endif
#if defined(HAWK_BUILD_DEBUG)
fprintf (out, " -X number fail the number'th memory allocation\n");
fprintf (out, " -t print command traces\n");
#endif
#if defined(HAWK_OOCH_IS_UCH)
fprintf (out, " --script-encoding string specify script file encoding name\n");
fprintf (out, " --infile-encoding string specify input file encoding name\n");
fprintf (out, " --outfile-encoding string specify output file encoding name\n");
fprintf (out, " --script-encoding string specify script file encoding name\n");
fprintf (out, " --infile-encoding string specify input file encoding name\n");
fprintf (out, " --outfile-encoding string specify output file encoding name\n");
#endif
}
@ -288,17 +212,13 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg
{ ":infile-encoding", '\0' },
{ ":outfile-encoding", '\0' },
#endif
{ ":memory-limit", 'm' },
{ "help", 'h' },
{ HAWK_NULL, '\0' }
};
static hawk_bcli_t opt =
{
#if defined(HAWK_BUILD_DEBUG)
"hne:f:o:rRisabxytm:wX:",
#else
"hne:f:o:rRisabxytm:w",
#endif
"hDne:f:o:rRisabxytm:w",
lng
};
hawk_bci_t c;
@ -325,6 +245,10 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg
print_usage (stdout, argv[0], real_argv0);
goto done;
case 'D':
arg->debug = 1;
break;
case 'n':
arg->option |= HAWK_SED_QUIET;
break;
@ -390,12 +314,6 @@ static int handle_args (int argc, hawk_bch_t* argv[], const hawk_bch_t* real_arg
arg->wildcard = 1;
break;
#if defined(HAWK_BUILD_DEBUG)
case 'X':
arg->failmalloc = strtoul(opt.arg, HAWK_NULL, 10);
break;
#endif
case '\0':
{
if (hawk_comp_bcstr(opt.lngopt, "script-encoding", 0) == 0)
@ -790,6 +708,7 @@ static HAWK_INLINE int execute_sed (int argc, hawk_bch_t* argv[], const hawk_bch
int xarg_inited = 0;
hawk_mmgr_t* mmgr = hawk_get_sys_mmgr();
hawk_cmgr_t* cmgr = hawk_get_cmgr_by_id(HAWK_CMGR_UTF8);
hawk_mmgr_t xma_mmgr;
memset (&arg, 0, HAWK_SIZEOF(arg));
ret = handle_args(argc, argv, real_argv0, &arg);
@ -798,18 +717,9 @@ static HAWK_INLINE int execute_sed (int argc, hawk_bch_t* argv[], const hawk_bch
ret = -1;
#if defined(HAWK_BUILD_DEBUG)
if (arg.failmalloc > 0)
{
debug_mmgr.ctx = &arg;
mmgr = &debug_mmgr;
}
else
#endif
if (arg.memlimit > 0)
{
xma_mmgr.ctx = hawk_xma_open(hawk_get_sys_mmgr(), 0, HAWK_NULL, arg.memlimit);
if (xma_mmgr.ctx == HAWK_NULL)
if (hawk_init_xma_mmgr(&xma_mmgr, arg.memlimit) <= -1)
{
print_error ("cannot open memory heap\n");
goto oops;
@ -1085,21 +995,13 @@ static HAWK_INLINE int execute_sed (int argc, hawk_bch_t* argv[], const hawk_bch
oops:
if (xarg_inited) purge_xarg (&xarg);
if (sed) hawk_sed_close (sed);
if (xma_mmgr.ctx) hawk_xma_close (xma_mmgr.ctx);
if (arg.memlimit > 0)
{
if (arg.debug) hawk_xma_dump (xma_mmgr.ctx, main_xma_dumper_without_hawk, HAWK_NULL);
hawk_fini_xma_mmgr (&xma_mmgr);
}
free_scripts ();
#if defined(HAWK_BUILD_DEBUG)
if (arg.failmalloc > 0)
{
fprintf (stderr, "\n");
fprintf (stderr, "-[MALLOC COUNTS]---------------------------------------\n");
fprintf (stderr, "ALLOC: %lu FREE: %lu: REALLOC: %lu\n",
(unsigned long)debug_mmgr_alloc_count,
(unsigned long)debug_mmgr_free_count,
(unsigned long)debug_mmgr_realloc_count);
fprintf (stderr, "-------------------------------------------------------\n");
}
#endif
return ret;
}