diff options
Diffstat (limited to 'lib/Exceptions.cc')
-rw-r--r-- | lib/Exceptions.cc | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/lib/Exceptions.cc b/lib/Exceptions.cc index 585b946..6e4ddbe 100644 --- a/lib/Exceptions.cc +++ b/lib/Exceptions.cc @@ -6,6 +6,7 @@ #include "config.h" #include "String.h" #include "Exceptions.h" +#include "General.h" char GeneralException::t[BUFSIZ]; @@ -60,35 +61,49 @@ TaskSwitch::TaskSwitch() : GeneralException(_("Switching task in a non-tasked en #endif } -char * xstrdup(const char * s) throw (GeneralException) { +char * xstrdup(const char * s) { char * r; - if (!(r = ::strdup(s))) { - throw MemoryException(strlen(s + 1)); - } - + r = (char *) xmalloc(strlen(s) + 1); + strcpy(r, s); return r; } -void * xmalloc(ssize_t s) throw (GeneralException) { - void * r; +void * xmalloc(size_t s) throw (GeneralException) { + char * r; + + if (!s) { + return 0; + } - if (!(r = ::malloc(s))) { - throw MemoryException(s); + if (!(r = (char *) ::malloc(s + sizeof(size_t)))) { + throw MemoryException(s + sizeof(size_t)); } - memset(r, 0, s); + memset(r, 0, s + sizeof(size_t)); + + *((size_t *)r) = s; - return r; + return (void *)(r + sizeof(size_t)); } -void * xrealloc(void * ptr, size_t s) throw (GeneralException) { - void * r; +void * xrealloc(void * ptr, size_t s) { + char * r; + size_t os; - if (!(r = ::realloc(ptr, s))) { - throw MemoryException(s); + if (!ptr) { + return xmalloc(s); } + os = *(((size_t *) ptr) - 1); + + r = (char *) xmalloc(s); + + if (s) { + memcpy(r, ptr, MIN(s, os)); + } + + xfree(ptr); return r; } @@ -98,7 +113,15 @@ void * xrealloc(void * ptr, size_t s) throw (GeneralException) { void xfree(void *& p) { if (p) { - ::free(p); + ::free(((char *)p) - sizeof(size_t)); p = 0; } } + +int xpipe(int * p, int flag) throw (GeneralException) { + if (pipe(p)) { + throw GeneralException(String("Error creating pipe: ") + strerror(errno)); + } + + return p[flag]; +} |