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
This commit is contained in:
tg 2011-03-05 21:43:18 +00:00
parent ad2fd952e4
commit 0b6afea352
5 changed files with 39 additions and 16 deletions

11
funcs.c
View File

@ -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);
}

View File

@ -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);
}
}

4
main.c
View File

@ -33,7 +33,7 @@
#include <locale.h>
#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;

4
misc.c
View File

@ -29,7 +29,7 @@
#include <grp.h>
#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

26
sh.h
View File

@ -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;