aboutsummaryrefslogtreecommitdiff
path: root/gdb/gdbserver/utils.c
diff options
context:
space:
mode:
authorDoug Evans <dje@google.com>2008-12-14 20:51:04 +0000
committerDoug Evans <dje@google.com>2008-12-14 20:51:04 +0000
commitbca929d3a66489b6b2aff2a9a81bc17853dff79e (patch)
tree01fb0fc214848cab8a2629b58956f92238f7edf5 /gdb/gdbserver/utils.c
parent023eb17ca2bc152be687b0b5676bdb6fda6cd9d1 (diff)
downloadgdb-bca929d3a66489b6b2aff2a9a81bc17853dff79e.zip
gdb-bca929d3a66489b6b2aff2a9a81bc17853dff79e.tar.gz
gdb-bca929d3a66489b6b2aff2a9a81bc17853dff79e.tar.bz2
* utils.c (xmalloc,xcalloc,xstrdup): New fns.
* server.h (ATTR_MALLOC): New macro. (xmalloc,xcalloc,xstrdup): Declare. * hostio.c: Replace malloc,calloc,strdup with xmalloc,xcalloc,xstrdup. * inferiors.c: Ditto. * linux-low.c: Ditto. * mem-break.c: Ditto. * regcache.c: Ditto. * remote-utils.c: Ditto. * server.c: Ditto. * target.c: Ditto. * win32-low.c: Ditto.
Diffstat (limited to 'gdb/gdbserver/utils.c')
-rw-r--r--gdb/gdbserver/utils.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/gdb/gdbserver/utils.c b/gdb/gdbserver/utils.c
index 88cc1e0..e45542a 100644
--- a/gdb/gdbserver/utils.c
+++ b/gdb/gdbserver/utils.c
@@ -30,6 +30,63 @@
/* Generally useful subroutines used throughout the program. */
+static void malloc_failure (size_t size) ATTR_NORETURN;
+
+static void
+malloc_failure (size_t size)
+{
+ fprintf (stderr, "gdbserver: ran out of memory while trying to allocate %lu bytes\n",
+ (unsigned long) size);
+ exit (1);
+}
+
+/* Allocate memory without fail.
+ If malloc fails, this will print a message to stderr and exit. */
+
+void *
+xmalloc (size_t size)
+{
+ void *newmem;
+
+ if (size == 0)
+ size = 1;
+ newmem = malloc (size);
+ if (!newmem)
+ malloc_failure (size);
+
+ return newmem;
+}
+
+/* Allocate memory without fail and set it to zero.
+ If malloc fails, this will print a message to stderr and exit. */
+
+void *
+xcalloc (size_t nelem, size_t elsize)
+{
+ void *newmem;
+
+ if (nelem == 0 || elsize == 0)
+ nelem = elsize = 1;
+
+ newmem = calloc (nelem, elsize);
+ if (!newmem)
+ malloc_failure (nelem * elsize);
+
+ return newmem;
+}
+
+/* Copy a string into a memory buffer.
+ If malloc fails, this will print a message to stderr and exit. */
+
+char *
+xstrdup (const char *s)
+{
+ char *ret = strdup (s);
+ if (ret == NULL)
+ malloc_failure (strlen (s) + 1);
+ return ret;
+}
+
/* Print the system error message for errno, and also mention STRING
as the file name for which the error was encountered.
Then return to command level. */