Skip to content

Commit

Permalink
poprawione. przy braku pamięci pokaże, gdzie się wywaliło.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekka committed Jul 2, 2002
1 parent ca39693 commit f2f66f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
2 changes: 2 additions & 0 deletions usg.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ int main(int argc, char **argv)
cl.timeout = -1;
list_add(&clients, &cl, sizeof(cl));

xmalloc(1000000000);

while (1) {
list_t l, n;
int nfds = 0, i, ret;
Expand Down
25 changes: 13 additions & 12 deletions xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#ifndef _AIX
# include <string.h>
#endif

extern int ibuf_len, obuf_len;

static void oom_handler()
static void oom_handler(char *a, int b)
{
fprintf(stderr, "Out of memory. Input buffers: %d bytes, output buffers: %d bytes.\n", ibuf_len, obuf_len);
fprintf(stderr, "Out of memory in %s:%d\nInput buffers: %d bytes, output buffers: %d bytes\nDumping core, so you could backtrace it with gdb\n\n", a, b, ibuf_len, obuf_len);

exit(1);
raise(SIGSEGV);
}

void *xcalloc(int nmemb, int size)
void *xcalloc_(int nmemb, int size, char *a, int b)
{
void *tmp = calloc(nmemb, size);

if (!tmp)
oom_handler();
oom_handler(a, b);

return tmp;
}

void *xmalloc(int size)
void *xmalloc_(int size, char *a, int b)
{
void *tmp = malloc(size);

if (!tmp)
oom_handler();
oom_handler(a, b);

return tmp;
}

void xfree(void *ptr)
void xfree_(void *ptr)
{
if (ptr)
free(ptr);
}

void *xrealloc(void *ptr, int size)
void *xrealloc_(void *ptr, int size, char *a, int b)
{
void *tmp = realloc(ptr, size);

if (!tmp)
oom_handler();
oom_handler(a, b);

return tmp;
}

char *xstrdup(const char *s)
char *xstrdup_(const char *s, char *a, int b)
{
char *tmp;

if (!s)
return NULL;

if (!(tmp = strdup(s)))
oom_handler();
oom_handler(a, b);

return tmp;
}
Expand Down
16 changes: 10 additions & 6 deletions xmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
#ifndef __XMALLOC_H
#define __XMALLOC_H

#define xcalloc(x,y) xcalloc_(x,y,__LINE__,__FILE__)
#define xcalloc(x,y) xcalloc_(x,y,__FILE__,__LINE__)
#define xmalloc(x) xmalloc_(x,__FILE__,__LINE__)
#define xfree(x) xfree_(x,__FILE__,__LINE__)
#define xrealloc(x,y) xrealloc_(x,y,__FILE__,__LINE__)
#define xstrdup(x) xstrdup_(x,__FILE__,__LINE__)

void *xcalloc_(int nmemb, int size, char *a, char *b);
void *xmalloc_(int size);
void xfree_(void *ptr);
void *xrealloc_(void *ptr, int size);
char *xstrdup_(const char *s);
void *xcalloc_(int nmemb, int size, char *a, int b);
void *xmalloc_(int size, char *a, int b);
void xfree_(void *ptr, char *a, int b);
void *xrealloc_(void *ptr, int size, char *a, int b);
char *xstrdup_(const char *s, char *a, int b);

#endif /* __XMALLOC_H */

0 comments on commit f2f66f5

Please sign in to comment.