* file named AUTHORS, in the root directory of this distribution.
*/
/*
- * $Id: alloc.c,v 1.35 2005/12/21 19:07:49 paddy_s Exp $
+ * $Id: alloc.c,v 1.37 2006/07/05 10:41:32 martinea Exp $
*
* Memory allocators with error handling. If the allocation fails,
* errordump() is called, relieving the caller from checking the return
#include "arglist.h"
#include "queue.h"
-static char *internal_vstralloc P((const char *, va_list));
+static char *internal_vstralloc(const char *, va_list);
/*
*=====================================================================
*/
const char *
-debug_caller_loc(file, line)
- const char *file;
- int line;
+debug_caller_loc(
+ const char *file,
+ int line)
{
+ /*@keep@*/
struct loc_str {
char *str;
LIST_ENTRY(loc_str) le;
if ((p = strrchr(file, '/')) != NULL)
file = p + 1; /* just the last path element */
- snprintf(loc, sizeof(loc), "%s@%d", file, line);
+ snprintf(loc, SIZEOF(loc), "%s@%d", file, line);
for (ls = LIST_FIRST(&root); ls != NULL; ls = LIST_NEXT(ls, le)) {
if (strcmp(loc, ls->str) == 0) {
/*
* This is a new entry. Put it at the head of the list.
*/
- ls = malloc(sizeof(*ls));
+ ls = malloc(SIZEOF(*ls));
if (ls == NULL)
return ("??"); /* not much better than abort */
ls->str = malloc(strlen(loc) + 1);
static int saved_line;
int
-debug_alloc_push (s, l)
- char *s;
- int l;
+debug_alloc_push(
+ char *s,
+ int l)
{
debug_alloc_loc_info[debug_alloc_ptr].file = s;
debug_alloc_loc_info[debug_alloc_ptr].line = l;
*/
void
-debug_alloc_pop ()
+debug_alloc_pop(void)
{
debug_alloc_ptr =
(debug_alloc_ptr + DEBUG_ALLOC_SAVE_MAX - 1) % DEBUG_ALLOC_SAVE_MAX;
* alloc - a wrapper for malloc.
*/
void *
-debug_alloc(s, l, size)
- const char *s;
- int l;
- size_t size;
+debug_alloc(
+ const char *s,
+ int l,
+ size_t size)
{
void *addr;
malloc_enter(debug_caller_loc(s, l));
addr = (void *)malloc(max(size, 1));
if (addr == NULL) {
- errordump("%s@%d: memory allocation failed (%u bytes requested)",
+ errordump("%s@%d: memory allocation failed (" SIZE_T_FMT " bytes requested)",
s ? s : "(unknown)",
s ? l : -1,
- size);
+ (SIZE_T_FMT_TYPE)size);
+ /*NOTREACHED*/
}
malloc_leave(debug_caller_loc(s, l));
return addr;
* newalloc - free existing buffer and then alloc a new one.
*/
void *
-debug_newalloc(s, l, old, size)
- const char *s;
- int l;
- void *old;
- size_t size;
+debug_newalloc(
+ const char *s,
+ int l,
+ void *old,
+ size_t size)
{
char *addr;
* Just like strdup()!
*/
char *
-debug_stralloc(s, l, str)
- const char *s;
- int l;
- const char *str;
+debug_stralloc(
+ const char *s,
+ int l,
+ const char *str)
{
char *addr;
/* vstrextend -- Extends the existing string by appending the other
* arguments. */
-arglist_function(char *vstrextend,
- char **,
- oldstr)
+/*@ignore@*/
+arglist_function(
+ char *vstrextend,
+ char **, oldstr)
{
char *keep = *oldstr;
va_list ap;
arglist_end(ap);
return *oldstr;
}
+/*@end@*/
/*
* internal_vstralloc - copies up to MAX_STR_ARGS strings into newly
#define MAX_VSTRALLOC_ARGS 32
static char *
-internal_vstralloc(str, argp)
- const char *str;
- va_list argp;
+internal_vstralloc(
+ const char *str,
+ va_list argp)
{
char *next;
char *result;
- int a;
+ int a, b;
size_t total_len;
const char *arg[MAX_VSTRALLOC_ARGS+1];
size_t len[MAX_VSTRALLOC_ARGS+1];
size_t l;
- const char *s;
if (str == NULL) {
- return NULL; /* probably will not happen */
+ errordump("internal_vstralloc: str is NULL");
+ /*NOTREACHED*/
}
a = 0;
continue; /* minor optimisation */
}
if (a >= MAX_VSTRALLOC_ARGS) {
- errordump("%s@%d: more than %d arg%s to vstralloc",
+ errordump("%s@%d: more than %d args to vstralloc",
saved_file ? saved_file : "(unknown)",
saved_file ? saved_line : -1,
- MAX_VSTRALLOC_ARGS,
- (MAX_VSTRALLOC_ARGS == 1) ? "" : "s");
+ MAX_VSTRALLOC_ARGS);
+ /*NOTREACHED*/
}
arg[a] = next;
len[a] = l;
total_len += l;
a++;
}
- arg[a] = NULL;
- len[a] = 0;
- next = result = debug_alloc(saved_file, saved_line, total_len+1);
- for (a = 0; (s = arg[a]) != NULL; a++) {
- memcpy(next, s, len[a]);
- next += len[a];
+ result = debug_alloc(saved_file, saved_line, total_len+1);
+
+ next = result;
+ for (b = 0; b < a; b++) {
+ memcpy(next, arg[b], len[b]);
+ next += len[b];
}
*next = '\0';
/*
* vstralloc - copies multiple strings into newly allocated memory.
*/
-arglist_function(char *debug_vstralloc, const char *, str)
+arglist_function(
+ char *debug_vstralloc,
+ const char *, str)
{
va_list argp;
char *result;
* newstralloc - free existing string and then stralloc a new one.
*/
char *
-debug_newstralloc(s, l, oldstr, newstr)
- const char *s;
- int l;
- char *oldstr;
- const char *newstr;
+debug_newstralloc(
+ const char *s,
+ int l,
+ char *oldstr,
+ const char *newstr)
{
char *addr;
/*
* newvstralloc - free existing string and then vstralloc a new one.
*/
-arglist_function1(char *debug_newvstralloc,
- char *,
- oldstr,
- const char *,
- newstr)
+arglist_function1(
+ char *debug_newvstralloc,
+ char *, oldstr,
+ const char *, newstr)
{
va_list argp;
char *result;
* safe_env - build a "safe" environment list.
*/
char **
-safe_env()
+safe_env(void)
{
static char *safe_env_list[] = {
"TZ",
* safe_env_list so our result is always a valid, although possibly
* empty, environment list.
*/
-#define SAFE_ENV_CNT (sizeof(safe_env_list) / sizeof(*safe_env_list))
+#define SAFE_ENV_CNT (size_t)(sizeof(safe_env_list) / sizeof(*safe_env_list))
char **envp = safe_env_list + SAFE_ENV_CNT - 1;
char **p;
char *v;
size_t l1, l2;
- if ((q = (char **)malloc(sizeof(safe_env_list))) != NULL) {
+ if ((q = (char **)malloc(SIZEOF(safe_env_list))) != NULL) {
envp = q;
for (p = safe_env_list; *p != NULL; p++) {
if ((v = getenv(*p)) == NULL) {
*/
int
-debug_amtable_alloc(s, l, table, current, elsize, count, bump, init_func)
- const char *s;
- int l;
- void **table;
- int *current;
- size_t elsize;
- int count;
- int bump;
- void (*init_func)(void *);
+debug_amtable_alloc(
+ const char *s,
+ int l,
+ void **table,
+ size_t *current,
+ size_t elsize,
+ size_t count,
+ int bump,
+ void (*init_func)(void *))
{
void *table_new;
- int table_count_new;
- int i;
+ size_t table_count_new;
+ size_t i;
if (count >= *current) {
table_count_new = ((count + bump) / bump) * bump;
*/
void
-amtable_free(table, current)
- void **table;
- int *current;
+amtable_free(
+ void **table,
+ size_t *current)
{
amfree(*table);
*current = 0;