From 0b6afea3525c31f5dfbc54f128540912fb871689 Mon Sep 17 00:00:00 2001 From: tg Date: Sat, 5 Mar 2011 21:43:18 +0000 Subject: [PATCH] introduce macros for malloc, realloc, free to hide them from mksh no code may henceforth use memory (de-)allocation functions directly use these macros, porters can change them for abstraction --- funcs.c | 11 +++++------ lalloc.c | 10 +++++----- main.c | 4 ++-- misc.c | 4 ++-- sh.h | 26 +++++++++++++++++++++++++- 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/funcs.c b/funcs.c index 35dba6e..5f05cd2 100644 --- a/funcs.c +++ b/funcs.c @@ -38,7 +38,7 @@ #endif #endif -__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.172 2011/02/18 22:26:08 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.173 2011/03/05 21:43:15 tg Exp $"); #if HAVE_KILLPG /* @@ -364,7 +364,7 @@ do_realpath(const char *upath) return (Xclose(xs, xp)); notfound: - /* save; free(3) might trash it */ + /* save; freeing memory might trash it */ llen = errno; if (ldest != NULL) afree(ldest, ATEMP); @@ -2744,7 +2744,7 @@ c_mknod(const char **wp) return (1); } mode = getmode(set, (mode_t)(DEFFILEMODE)); - free(set); + free_ossetmode(set); break; default: goto c_mknod_usage; @@ -3605,8 +3605,7 @@ c_cat(const char **wp) char *buf, *cp; #define MKSH_CAT_BUFSIZ 4096 - /* XXX uses malloc instead of lalloc (for alignment/speed) */ - if ((buf = malloc(MKSH_CAT_BUFSIZ)) == NULL) { + if ((buf = malloc_os(MKSH_CAT_BUFSIZ)) == NULL) { bi_errorf(T_oomem, (unsigned long)MKSH_CAT_BUFSIZ); return (1); } @@ -3675,7 +3674,7 @@ c_cat(const char **wp) } while (*wp); out: - free(buf); + free_osmalloc(buf); return (rv); } diff --git a/lalloc.c b/lalloc.c index c3dc23a..9aaaf27 100644 --- a/lalloc.c +++ b/lalloc.c @@ -20,13 +20,13 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.14 2011/01/09 21:57:27 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/lalloc.c,v 1.15 2011/03/05 21:43:16 tg Exp $"); /* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */ #if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0) -#define remalloc(p,n) ((p) == NULL ? malloc(n) : realloc((p), (n))) +#define remalloc(p,n) ((p) == NULL ? malloc_os(n) : realloc_os((p), (n))) #else -#define remalloc(p,n) realloc((p), (n)) +#define remalloc(p,n) realloc_os((p), (n)) #endif #define ALLOC_ISUNALIGNED(p) (((ptrdiff_t)(p)) % ALLOC_SIZE) @@ -113,7 +113,7 @@ afree(void *ptr, Area *ap) /* unhook */ pp->next = lp->next; /* now free ALLOC_ITEM */ - free(lp); + free_osmalloc(lp); } } @@ -127,6 +127,6 @@ afreeall(Area *ap) /* make next ALLOC_ITEM head of list */ ap->next = lp->next; /* free old head */ - free(lp); + free_osmalloc(lp); } } diff --git a/main.c b/main.c index 90ce2b8..aa4487f 100644 --- a/main.c +++ b/main.c @@ -33,7 +33,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/main.c,v 1.178 2011/02/27 19:29:31 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/main.c,v 1.179 2011/03/05 21:43:17 tg Exp $"); extern char **environ; @@ -1402,7 +1402,7 @@ maketemp(Area *ap, Temp_type type, struct temp **tlist) tp->name[0] = '\0'; else { memcpy(tp->name, pathname, len); - free(pathname); + free_ostempnam(pathname); } #endif pathname = tp->name; diff --git a/misc.c b/misc.c index e03c17d..6561b24 100644 --- a/misc.c +++ b/misc.c @@ -29,7 +29,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.151 2011/02/11 01:18:19 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.152 2011/03/05 21:43:17 tg Exp $"); /* type bits for unsigned char */ unsigned char chtypes[UCHAR_MAX + 1]; @@ -1130,7 +1130,7 @@ ksh_get_wd(size_t *dlen) if ((b = get_current_dir_name())) { len = strlen(b) + 1; strndupx(ret, b, len - 1, ATEMP); - free(b); + free_gnu_gcdn(b); } else ret = NULL; #else diff --git a/sh.h b/sh.h index b71061c..ae2c4d2 100644 --- a/sh.h +++ b/sh.h @@ -154,7 +154,7 @@ #endif #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.433 2011/02/27 19:29:32 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.434 2011/03/05 21:43:18 tg Exp $"); #endif #define MKSH_VERSION "R39 2011/02/18" @@ -497,6 +497,30 @@ char *ucstrstr(char *, const char *); * simple grouping allocator */ + +/* 0. OS API: where to get memory from and how to free it (grouped) */ + +/* malloc(3)/realloc(3) -> free(3) */ +#define malloc_os(sz) malloc(sz) +#define realloc_os(p,sz) realloc((p), (sz)) +#define free_osmalloc(p) free(p) + +#if HAVE_MKNOD +/* setmode(3) -> free(3) */ +#define free_ossetmode(p) free(p) +#endif + +#if !HAVE_MKSTEMP +/* tempnam(3) -> free(3) */ +#define free_ostempnam(p) free(p) +#endif + +#ifdef NO_PATH_MAX +/* GNU libc: get_current_dir_name(3) -> free(3) */ +#define free_gnu_gcdn(p) free(p) +#endif + + /* 1. internal structure */ struct lalloc { struct lalloc *next;