From f26c147b7e348a8e0b91364f4a4d100dc1832467 Mon Sep 17 00:00:00 2001 From: Sebastian Hammer Date: Mon, 27 Mar 1995 08:35:16 +0000 Subject: [PATCH] Created util library Added memory debugging module. Imported options-manager --- util/Makefile | 53 ++++++++++++++++++++++++++++++++++ util/dmalloc.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ util/options.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+) create mode 100644 util/Makefile create mode 100644 util/dmalloc.c create mode 100644 util/options.c diff --git a/util/Makefile b/util/Makefile new file mode 100644 index 0000000..7b3dfd2 --- /dev/null +++ b/util/Makefile @@ -0,0 +1,53 @@ +# Copyright (C) 1994, Index Data I/S +# All rights reserved. +# Sebastian Hammer, Adam Dickmeiss +# $Id: Makefile,v 1.1 1995-03-27 08:35:16 quinn Exp $ + +SHELL=/bin/sh +INCLUDE=-I../include -I. -I../odr +LIBDIR=../../lib +LIBINCLUDE=-L$(LIBDIR) +#CFLAGS=-g -Wall -pedantic -ansi +DEFS=$(INCLUDE) +LIB=$(LIBDIR)/libutil.a +LIBS= +PO = dmalloc.o options.o +CPP=cc -E + +all: $(LIBDIR) $(LIB) + +#test: test.o $(LIB) ../odr/odr.a +# $(CC) $(CFLAGS) $(INCLUDE) -o test test.o $(LIB) ../odr/odr.a + +alll: + +$(LIB): $(PO) + rm -f $(LIB) + ar qc $(LIB) $(PO) + ranlib $(LIB) + +$(LIBDIR): + mkdir $(LIBDIR) + +.c.o: + $(CC) -c $(DEFS) $(CFLAGS) $< + +clean: + rm -f *.[oa] test core mon.out gmon.out errlist + +depend: depend2 + +depend1: + mv Makefile Makefile.tmp + sed '/^#Depend/q' Makefile + $(CPP) $(INCLUDE) -M *.c >>Makefile + -rm Makefile.tmp + +depend2: + $(CPP) $(INCLUDE) -M *.c >.depend + +ifeq (.depend,$(wildcard .depend)) +include .depend +endif + +#Depend --- DOT NOT DELETE THIS LINE diff --git a/util/dmalloc.c b/util/dmalloc.c new file mode 100644 index 0000000..b2847b4 --- /dev/null +++ b/util/dmalloc.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 1994, Index Data I/S + * All rights reserved. + * Sebastian Hammer, Adam Dickmeiss + * + * $Log: dmalloc.c,v $ + * Revision 1.1 1995-03-27 08:35:17 quinn + * Created util library + * Added memory debugging module. Imported options-manager + * + * + */ + +#include +#include +#include + +static const unsigned long head = 0xaabbccdd; +static const unsigned long tail = 0x11223344; +static const unsigned long freed = 0xffeeffee; + +void *d_malloc(char *file, int line, int nbytes) +{ + char *res; + int long len; + + if (!(res = malloc(nbytes + 3 * sizeof(long)))) + return 0; + fprintf(stderr, "---d_malloc, '%s':%d, %d->0x%p\n", + file, line, nbytes, res + 2 * sizeof(long)); + len = nbytes; + memcpy(res, &head, sizeof(long)); + memcpy(res + sizeof(long), &len, sizeof(long)); + res += 2 * sizeof(long); + memcpy(res + nbytes, &tail, sizeof(long)); + return res; +} + +void d_free(char *file, int line, char *ptr) +{ + long len; + + if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long))) + abort(); + memcpy(ptr, &freed, sizeof(long)); + memcpy(&len, ptr - sizeof(long), sizeof(long)); + if (memcmp(ptr + len, &tail, sizeof(long))) + abort(); + fprintf(stderr, "---d_free, '%s':%d, 0x%p (%d)\n", + file, line, ptr, len); + free(ptr - 2 * sizeof(long)); + return; +} + +void *d_realloc(char *file, int line, char *ptr, int nbytes) +{ + long len, nlen = nbytes; + char *p = ptr; + char *r; + + if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long))) + abort(); + memcpy(&len, ptr - sizeof(long), sizeof(long)); + if (memcmp(ptr + len, &tail, sizeof(long))) + abort(); + if (!(r = realloc(ptr, nbytes + 3 * sizeof(long)))) + return 0; + fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, 0x%p->0x%p\n", + file, line, len, nbytes, p, r + 2 * sizeof(long)); + memcpy(r, &head, sizeof(long)); + memcpy(r + sizeof(long), &nlen, sizeof(long)); + if (r != ptr - 2 * sizeof(long)) + { + memcpy(r + 2 * sizeof(long), ptr, len); + memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long)); + } + r += 2 * sizeof(long); + memcpy(r + nbytes, &tail, sizeof(long)); + return r; +} diff --git a/util/options.c b/util/options.c new file mode 100644 index 0000000..ebe1b93 --- /dev/null +++ b/util/options.c @@ -0,0 +1,86 @@ +/* + * Copyright (C) 1994, Index Data I/S + * All rights reserved. + * Sebastian Hammer, Adam Dickmeiss + * + * $Log: options.c,v $ + * Revision 1.1 1995-03-27 08:35:18 quinn + * Created util library + * Added memory debugging module. Imported options-manager + * + * Revision 1.2 1994/10/04 17:47:10 adam + * Function options now returns arg with error option. + * + * Revision 1.1 1994/08/16 15:57:22 adam + * The first utility modules. + * + */ +#include + +#include + +static int arg_no = 1; +static int arg_off = 0; + +int options (const char *desc, char **argv, int argc, char **arg) +{ + int ch, i = 0; + + if (arg_no >= argc) + return -2; + if (arg_off == 0) + { + if (argv[arg_no][0] != '-') + { + *arg = argv[arg_no++]; + return 0; + } + arg_off++; + } + ch = argv[arg_no][arg_off++]; + while (desc[i]) + { + int desc_char = desc[i++]; + int type = 0; + if (desc[i] == ':') + { /* string argument */ + type = desc[i++]; + } + if (desc_char == ch) + { /* option with argument */ + if (type) + { + if (argv[arg_no][arg_off]) + { + *arg = argv[arg_no]+arg_off; + arg_no++; + arg_off = 0; + } + else + { + arg_no++; + arg_off = 0; + if (arg_no < argc) + *arg = argv[arg_no++]; + else + *arg = ""; + } + } + else /* option with no argument */ + { + if (argv[arg_no][arg_off]) + arg_off++; + else + { + arg_off = 0; + arg_no++; + } + } + return ch; + } + } + *arg = argv[arg_no]+arg_off-1; + arg_no = arg_no + 1; + arg_off = 0; + return -1; +} -- 1.7.10.4