add en*alloc functions

This commit is contained in:
Jakob Kramer 2015-02-11 01:40:52 +01:00 committed by sin
parent 51680535ce
commit 08e93dd4f5
2 changed files with 46 additions and 18 deletions

View File

@ -7,41 +7,65 @@
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
p = calloc(nmemb, size);
if (!p)
eprintf("calloc: out of memory\n");
return p;
return encalloc(1, nmemb, size);
}
void *
emalloc(size_t size)
{
void *p;
p = malloc(size);
if (!p)
eprintf("malloc: out of memory\n");
return p;
return enmalloc(1, size);
}
void *
erealloc(void *p, size_t size)
{
p = realloc(p, size);
if (!p)
eprintf("realloc: out of memory\n");
return p;
return enrealloc(1, p, size);
}
char *
estrdup(const char *s)
{
return enstrdup(1, s);
}
void *
encalloc(int status, size_t nmemb, size_t size)
{
void *p;
p = calloc(nmemb, size);
if (!p)
enprintf(status, "calloc: out of memory\n");
return p;
}
void *
enmalloc(int status, size_t size)
{
void *p;
p = malloc(size);
if (!p)
enprintf(status, "malloc: out of memory\n");
return p;
}
void *
enrealloc(int status, void *p, size_t size)
{
p = realloc(p, size);
if (!p)
enprintf(status, "realloc: out of memory\n");
return p;
}
char *
enstrdup(int status, const char *s)
{
char *p;
p = strdup(s);
if (!p)
eprintf("strdup: out of memory\n");
enprintf(status, "strdup: out of memory\n");
return p;
}

6
util.h
View File

@ -22,9 +22,13 @@ char *agetcwd(void);
void apathmax(char **, long *);
void *ecalloc(size_t, size_t);
void *emalloc(size_t size);
void *emalloc(size_t);
void *erealloc(void *, size_t);
char *estrdup(const char *);
void *encalloc(int, size_t, size_t);
void *enmalloc(int, size_t);
void *enrealloc(int, void *, size_t);
char *enstrdup(int, const char *);
void enprintf(int, const char *, ...);
void eprintf(const char *, ...);