diff options
Diffstat (limited to 'src/polyp')
-rw-r--r-- | src/polyp/browser.c | 3 | ||||
-rw-r--r-- | src/polyp/channelmap.c | 4 | ||||
-rw-r--r-- | src/polyp/client-conf-x11.c | 3 | ||||
-rw-r--r-- | src/polyp/client-conf.c | 3 | ||||
-rw-r--r-- | src/polyp/context.c | 2 | ||||
-rw-r--r-- | src/polyp/glib-mainloop.c | 3 | ||||
-rw-r--r-- | src/polyp/glib12-mainloop.c | 3 | ||||
-rw-r--r-- | src/polyp/mainloop-api.c | 3 | ||||
-rw-r--r-- | src/polyp/mainloop-signal.c | 3 | ||||
-rw-r--r-- | src/polyp/mainloop.c | 3 | ||||
-rw-r--r-- | src/polyp/operation.c | 2 | ||||
-rw-r--r-- | src/polyp/simple.c | 2 | ||||
-rw-r--r-- | src/polyp/stream.c | 3 | ||||
-rw-r--r-- | src/polyp/thread-mainloop.c | 3 | ||||
-rw-r--r-- | src/polyp/xmalloc.c | 123 | ||||
-rw-r--r-- | src/polyp/xmalloc.h | 58 |
16 files changed, 207 insertions, 14 deletions
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 <assert.h> #include <howl.h> -#include <polypcore/xmalloc.h> +#include <polyp/xmalloc.h> + #include <polypcore/log.h> #include <polypcore/util.h> 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 <stdio.h> #include <string.h> +#include <polyp/xmalloc.h> + #include <polypcore/util.h> -#include <polypcore/xmalloc.h> + #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 <X11/Xlib.h> #include <X11/Xatom.h> +#include <polyp/xmalloc.h> + #include <polypcore/x11prop.h> #include <polypcore/log.h> -#include <polypcore/xmalloc.h> #include <polypcore/util.h> #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 <errno.h> #include <string.h> -#include <polypcore/xmalloc.h> +#include <polyp/xmalloc.h> + #include <polypcore/log.h> #include <polypcore/conf-parser.h> #include <polypcore/util.h> 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 <polyp/version.h> +#include <polyp/xmalloc.h> #include <polypcore/native-common.h> #include <polypcore/pdispatch.h> @@ -56,7 +57,6 @@ #include <polypcore/socket-client.h> #include <polypcore/pstream-util.h> #include <polypcore/util.h> -#include <polypcore/xmalloc.h> #include <polypcore/log.h> #include <polypcore/socket-util.h> 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 <assert.h> +#include <polyp/xmalloc.h> + #include <polypcore/idxset.h> -#include <polypcore/xmalloc.h> #include <polypcore/util.h> #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 <assert.h> +#include <polyp/xmalloc.h> + #include <polypcore/idxset.h> -#include <polypcore/xmalloc.h> #include <polypcore/util.h> #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 <assert.h> #include <stdlib.h> +#include <polyp/xmalloc.h> + #include <polypcore/gccmacro.h> -#include <polypcore/xmalloc.h> #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 <windows.h> #endif +#include <polyp/xmalloc.h> + #include <polypcore/util.h> -#include <polypcore/xmalloc.h> #include <polypcore/log.h> #include <polypcore/gccmacro.h> 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 <polyp/xmalloc.h> + #include <polypcore/util.h> #include <polypcore/idxset.h> -#include <polypcore/xmalloc.h> #include <polypcore/log.h> #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 <assert.h> -#include <polypcore/xmalloc.h> +#include <polyp/xmalloc.h> #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 <polyp/polypaudio.h> #include <polyp/thread-mainloop.h> +#include <polyp/xmalloc.h> #include <polypcore/native-common.h> -#include <polypcore/xmalloc.h> #include <polypcore/log.h> #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 <string.h> #include <polyp/def.h> -#include <polypcore/xmalloc.h> +#include <polyp/xmalloc.h> + #include <polypcore/pstream-util.h> #include <polypcore/util.h> #include <polypcore/log.h> 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 <windows.h> #endif +#include <polyp/xmalloc.h> + #include <polypcore/log.h> -#include <polypcore/xmalloc.h> #include <polypcore/hashmap.h> #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 <config.h> +#endif + +#include <stdlib.h> +#include <signal.h> +#include <assert.h> +#include <unistd.h> +#include <string.h> + +#include <polypcore/util.h> +#include <polypcore/gccmacro.h> + +#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 <sys/types.h> +#include <stdlib.h> +#include <limits.h> +#include <assert.h> + +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 |