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:
parent
ad2fd952e4
commit
0b6afea352
11
funcs.c
11
funcs.c
|
@ -38,7 +38,7 @@
|
||||||
#endif
|
#endif
|
||||||
#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
|
#if HAVE_KILLPG
|
||||||
/*
|
/*
|
||||||
|
@ -364,7 +364,7 @@ do_realpath(const char *upath)
|
||||||
return (Xclose(xs, xp));
|
return (Xclose(xs, xp));
|
||||||
|
|
||||||
notfound:
|
notfound:
|
||||||
/* save; free(3) might trash it */
|
/* save; freeing memory might trash it */
|
||||||
llen = errno;
|
llen = errno;
|
||||||
if (ldest != NULL)
|
if (ldest != NULL)
|
||||||
afree(ldest, ATEMP);
|
afree(ldest, ATEMP);
|
||||||
|
@ -2744,7 +2744,7 @@ c_mknod(const char **wp)
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
mode = getmode(set, (mode_t)(DEFFILEMODE));
|
mode = getmode(set, (mode_t)(DEFFILEMODE));
|
||||||
free(set);
|
free_ossetmode(set);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
goto c_mknod_usage;
|
goto c_mknod_usage;
|
||||||
|
@ -3605,8 +3605,7 @@ c_cat(const char **wp)
|
||||||
char *buf, *cp;
|
char *buf, *cp;
|
||||||
#define MKSH_CAT_BUFSIZ 4096
|
#define MKSH_CAT_BUFSIZ 4096
|
||||||
|
|
||||||
/* XXX uses malloc instead of lalloc (for alignment/speed) */
|
if ((buf = malloc_os(MKSH_CAT_BUFSIZ)) == NULL) {
|
||||||
if ((buf = malloc(MKSH_CAT_BUFSIZ)) == NULL) {
|
|
||||||
bi_errorf(T_oomem, (unsigned long)MKSH_CAT_BUFSIZ);
|
bi_errorf(T_oomem, (unsigned long)MKSH_CAT_BUFSIZ);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
@ -3675,7 +3674,7 @@ c_cat(const char **wp)
|
||||||
} while (*wp);
|
} while (*wp);
|
||||||
|
|
||||||
out:
|
out:
|
||||||
free(buf);
|
free_osmalloc(buf);
|
||||||
return (rv);
|
return (rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
lalloc.c
10
lalloc.c
|
@ -20,13 +20,13 @@
|
||||||
|
|
||||||
#include "sh.h"
|
#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 */
|
/* build with CPPFLAGS+= -DUSE_REALLOC_MALLOC=0 on ancient systems */
|
||||||
#if defined(USE_REALLOC_MALLOC) && (USE_REALLOC_MALLOC == 0)
|
#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
|
#else
|
||||||
#define remalloc(p,n) realloc((p), (n))
|
#define remalloc(p,n) realloc_os((p), (n))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define ALLOC_ISUNALIGNED(p) (((ptrdiff_t)(p)) % ALLOC_SIZE)
|
#define ALLOC_ISUNALIGNED(p) (((ptrdiff_t)(p)) % ALLOC_SIZE)
|
||||||
|
@ -113,7 +113,7 @@ afree(void *ptr, Area *ap)
|
||||||
/* unhook */
|
/* unhook */
|
||||||
pp->next = lp->next;
|
pp->next = lp->next;
|
||||||
/* now free ALLOC_ITEM */
|
/* now free ALLOC_ITEM */
|
||||||
free(lp);
|
free_osmalloc(lp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,6 +127,6 @@ afreeall(Area *ap)
|
||||||
/* make next ALLOC_ITEM head of list */
|
/* make next ALLOC_ITEM head of list */
|
||||||
ap->next = lp->next;
|
ap->next = lp->next;
|
||||||
/* free old head */
|
/* free old head */
|
||||||
free(lp);
|
free_osmalloc(lp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
main.c
4
main.c
|
@ -33,7 +33,7 @@
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#endif
|
#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;
|
extern char **environ;
|
||||||
|
|
||||||
|
@ -1402,7 +1402,7 @@ maketemp(Area *ap, Temp_type type, struct temp **tlist)
|
||||||
tp->name[0] = '\0';
|
tp->name[0] = '\0';
|
||||||
else {
|
else {
|
||||||
memcpy(tp->name, pathname, len);
|
memcpy(tp->name, pathname, len);
|
||||||
free(pathname);
|
free_ostempnam(pathname);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
pathname = tp->name;
|
pathname = tp->name;
|
||||||
|
|
4
misc.c
4
misc.c
|
@ -29,7 +29,7 @@
|
||||||
#include <grp.h>
|
#include <grp.h>
|
||||||
#endif
|
#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 */
|
/* type bits for unsigned char */
|
||||||
unsigned char chtypes[UCHAR_MAX + 1];
|
unsigned char chtypes[UCHAR_MAX + 1];
|
||||||
|
@ -1130,7 +1130,7 @@ ksh_get_wd(size_t *dlen)
|
||||||
if ((b = get_current_dir_name())) {
|
if ((b = get_current_dir_name())) {
|
||||||
len = strlen(b) + 1;
|
len = strlen(b) + 1;
|
||||||
strndupx(ret, b, len - 1, ATEMP);
|
strndupx(ret, b, len - 1, ATEMP);
|
||||||
free(b);
|
free_gnu_gcdn(b);
|
||||||
} else
|
} else
|
||||||
ret = NULL;
|
ret = NULL;
|
||||||
#else
|
#else
|
||||||
|
|
26
sh.h
26
sh.h
|
@ -154,7 +154,7 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef EXTERN
|
#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
|
#endif
|
||||||
#define MKSH_VERSION "R39 2011/02/18"
|
#define MKSH_VERSION "R39 2011/02/18"
|
||||||
|
|
||||||
|
@ -497,6 +497,30 @@ char *ucstrstr(char *, const char *);
|
||||||
* simple grouping allocator
|
* 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 */
|
/* 1. internal structure */
|
||||||
struct lalloc {
|
struct lalloc {
|
||||||
struct lalloc *next;
|
struct lalloc *next;
|
||||||
|
|
Loading…
Reference in New Issue