From d9cc2cfcb97c1b0449bcbfb6ab0301a58d77bd55 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Wed, 17 May 2006 16:34:18 +0000 Subject: Move xmalloc to the public side (libpolyp). git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@908 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/polyp/browser.c | 3 +- src/polyp/channelmap.c | 4 +- src/polyp/client-conf-x11.c | 3 +- src/polyp/client-conf.c | 3 +- src/polyp/context.c | 2 +- src/polyp/glib-mainloop.c | 3 +- src/polyp/glib12-mainloop.c | 3 +- src/polyp/mainloop-api.c | 3 +- src/polyp/mainloop-signal.c | 3 +- src/polyp/mainloop.c | 3 +- src/polyp/operation.c | 2 +- src/polyp/simple.c | 2 +- src/polyp/stream.c | 3 +- src/polyp/thread-mainloop.c | 3 +- src/polyp/xmalloc.c | 123 ++++++++++++++++++++++++++++++++++++++++++++ src/polyp/xmalloc.h | 58 +++++++++++++++++++++ 16 files changed, 207 insertions(+), 14 deletions(-) create mode 100644 src/polyp/xmalloc.c create mode 100644 src/polyp/xmalloc.h (limited to 'src/polyp') diff --git a/src/polyp/browser.c b/src/polyp/browser.c index cef680e4..5442fd4c 100644 --- a/src/polyp/browser.c +++ b/src/polyp/browser.c @@ -22,7 +22,8 @@ #include #include -#include +#include + #include #include diff --git a/src/polyp/channelmap.c b/src/polyp/channelmap.c index 7266a0a0..ddd7b3ce 100644 --- a/src/polyp/channelmap.c +++ b/src/polyp/channelmap.c @@ -28,8 +28,10 @@ #include #include +#include + #include -#include + #include "channelmap.h" const char *const table[] = { diff --git a/src/polyp/client-conf-x11.c b/src/polyp/client-conf-x11.c index 7187d86b..17ee2d6a 100644 --- a/src/polyp/client-conf-x11.c +++ b/src/polyp/client-conf-x11.c @@ -29,9 +29,10 @@ #include #include +#include + #include #include -#include #include #include "client-conf-x11.h" diff --git a/src/polyp/client-conf.c b/src/polyp/client-conf.c index d3ad0767..567d2ae4 100644 --- a/src/polyp/client-conf.c +++ b/src/polyp/client-conf.c @@ -29,7 +29,8 @@ #include #include -#include +#include + #include #include #include diff --git a/src/polyp/context.c b/src/polyp/context.c index d5cf90f8..3c46e2e8 100644 --- a/src/polyp/context.c +++ b/src/polyp/context.c @@ -48,6 +48,7 @@ #include "../polypcore/winsock.h" #include +#include #include #include @@ -56,7 +57,6 @@ #include #include #include -#include #include #include diff --git a/src/polyp/glib-mainloop.c b/src/polyp/glib-mainloop.c index 3937a1a9..bc5df3a9 100644 --- a/src/polyp/glib-mainloop.c +++ b/src/polyp/glib-mainloop.c @@ -25,8 +25,9 @@ #include +#include + #include -#include #include #include "glib.h" diff --git a/src/polyp/glib12-mainloop.c b/src/polyp/glib12-mainloop.c index 5ad23adb..7af21210 100644 --- a/src/polyp/glib12-mainloop.c +++ b/src/polyp/glib12-mainloop.c @@ -25,8 +25,9 @@ #include +#include + #include -#include #include #include "glib-mainloop.h" diff --git a/src/polyp/mainloop-api.c b/src/polyp/mainloop-api.c index 71f55c05..f29598dc 100644 --- a/src/polyp/mainloop-api.c +++ b/src/polyp/mainloop-api.c @@ -26,8 +26,9 @@ #include #include +#include + #include -#include #include "mainloop-api.h" diff --git a/src/polyp/mainloop-signal.c b/src/polyp/mainloop-signal.c index 4ffa00ba..a225f78b 100644 --- a/src/polyp/mainloop-signal.c +++ b/src/polyp/mainloop-signal.c @@ -36,8 +36,9 @@ #include #endif +#include + #include -#include #include #include diff --git a/src/polyp/mainloop.c b/src/polyp/mainloop.c index 589fe77e..82e789c5 100644 --- a/src/polyp/mainloop.c +++ b/src/polyp/mainloop.c @@ -44,9 +44,10 @@ #include "../polypcore/pipe.h" #endif +#include + #include #include -#include #include #include "mainloop.h" diff --git a/src/polyp/operation.c b/src/polyp/operation.c index 1c0cb99f..5af9ec0b 100644 --- a/src/polyp/operation.c +++ b/src/polyp/operation.c @@ -25,7 +25,7 @@ #include -#include +#include #include "internal.h" #include "operation.h" diff --git a/src/polyp/simple.c b/src/polyp/simple.c index 1f25869b..b56406c9 100644 --- a/src/polyp/simple.c +++ b/src/polyp/simple.c @@ -30,9 +30,9 @@ #include #include +#include #include -#include #include #include "simple.h" diff --git a/src/polyp/stream.c b/src/polyp/stream.c index b2711ce0..f188e788 100644 --- a/src/polyp/stream.c +++ b/src/polyp/stream.c @@ -29,7 +29,8 @@ #include #include -#include +#include + #include #include #include diff --git a/src/polyp/thread-mainloop.c b/src/polyp/thread-mainloop.c index 20639e9e..d036a232 100644 --- a/src/polyp/thread-mainloop.c +++ b/src/polyp/thread-mainloop.c @@ -41,8 +41,9 @@ #include #endif +#include + #include -#include #include #include "mainloop.h" diff --git a/src/polyp/xmalloc.c b/src/polyp/xmalloc.c new file mode 100644 index 00000000..4c8689a6 --- /dev/null +++ b/src/polyp/xmalloc.c @@ -0,0 +1,123 @@ +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + polypaudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include + +#include +#include + +#include "xmalloc.h" + +/* Make sure not to allocate more than this much memory. */ +#define MAX_ALLOC_SIZE (1024*1024*20) /* 20MB */ + +/* #undef malloc */ +/* #undef free */ +/* #undef realloc */ +/* #undef strndup */ +/* #undef strdup */ + +static void oom(void) PA_GCC_NORETURN; + +/** called in case of an OOM situation. Prints an error message and + * exits */ +static void oom(void) { + static const char e[] = "Not enough memory\n"; + pa_loop_write(STDERR_FILENO, e, sizeof(e)-1); +#ifdef SIGQUIT + raise(SIGQUIT); +#endif + _exit(1); +} + +void* pa_xmalloc(size_t size) { + void *p; + assert(size > 0); + assert(size < MAX_ALLOC_SIZE); + + if (!(p = malloc(size))) + oom(); + + return p; +} + +void* pa_xmalloc0(size_t size) { + void *p; + assert(size > 0); + assert(size < MAX_ALLOC_SIZE); + + if (!(p = calloc(1, size))) + oom(); + + return p; +} + +void *pa_xrealloc(void *ptr, size_t size) { + void *p; + assert(size > 0); + assert(size < MAX_ALLOC_SIZE); + + if (!(p = realloc(ptr, size))) + oom(); + return p; +} + +void* pa_xmemdup(const void *p, size_t l) { + if (!p) + return NULL; + else { + char *r = pa_xmalloc(l); + memcpy(r, p, l); + return r; + } +} + +char *pa_xstrdup(const char *s) { + if (!s) + return NULL; + + return pa_xmemdup(s, strlen(s)+1); +} + +char *pa_xstrndup(const char *s, size_t l) { + if (!s) + return NULL; + else { + char *r; + size_t t = strlen(s); + + if (t > l) + t = l; + + r = pa_xmemdup(s, t+1); + r[t] = 0; + return r; + } +} + diff --git a/src/polyp/xmalloc.h b/src/polyp/xmalloc.h new file mode 100644 index 00000000..2946011a --- /dev/null +++ b/src/polyp/xmalloc.h @@ -0,0 +1,58 @@ +#ifndef foomemoryhfoo +#define foomemoryhfoo + +/* $Id$ */ + +/*** + This file is part of polypaudio. + + polypaudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + polypaudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with polypaudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include +#include +#include +#include + +void* pa_xmalloc(size_t l); +void *pa_xmalloc0(size_t l); +void *pa_xrealloc(void *ptr, size_t size); +#define pa_xfree free + +char *pa_xstrdup(const char *s); +char *pa_xstrndup(const char *s, size_t l); + +void* pa_xmemdup(const void *p, size_t l); + +/** Internal helper for pa_xnew() */ +static inline void* pa_xnew_internal(unsigned n, size_t k) { + assert(n < INT_MAX/k); + return pa_xmalloc(n*k); +} + +/** Allocate n new structures of the specified type. */ +#define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type))) + +/** Internal helper for pa_xnew0() */ +static inline void* pa_xnew0_internal(unsigned n, size_t k) { + assert(n < INT_MAX/k); + return pa_xmalloc0(n*k); +} + +/** Same as pa_xnew() but set the memory to zero */ +#define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type))) + +#endif -- cgit