diff options
author | Vicent Marti <tanoku@gmail.com> | 2011-05-09 21:58:26 +0300 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2011-05-09 21:58:26 +0300 |
commit | 5711ca93d13b74767788c0d7d68fb948e0f9e170 (patch) | |
tree | 4406a254cd862f256dd46295ca4032bfd9f5a25b /src/util.h | |
parent | cd2cc2dc363770d03a9d77206b97d0ee70244cad (diff) | |
parent | fa59f18d0ddbbb98d45e33934fb0efc3e2bf1557 (diff) | |
download | libgit2-5711ca93d13b74767788c0d7d68fb948e0f9e170.tar.gz |
Merge branch 'error-handling' into development
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/src/util.h b/src/util.h index 3c606493f..6724e8d41 100644 --- a/src/util.h +++ b/src/util.h @@ -6,16 +6,41 @@ #define MSB(x, bits) ((x) & (~0ULL << (bitsizeof(x) - (bits)))) /* - * Don't wrap malloc/calloc. - * Use the default versions in glibc, and make - * sure that any methods that allocate memory - * return a GIT_ENOMEM error when allocation - * fails. + * Custom memory allocation wrappers + * that set error code and error message + * on allocation failure */ -#define git__malloc malloc -#define git__calloc calloc -#define git__realloc realloc -#define git__strdup strdup +GIT_INLINE(void *) git__malloc(size_t len) +{ + void *ptr = malloc(len); + if (!ptr) + git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)len); + return ptr; +} + +GIT_INLINE(void *) git__calloc(size_t nelem, size_t elsize) +{ + void *ptr = calloc(nelem, elsize); + if (!ptr) + git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)elsize); + return ptr; +} + +GIT_INLINE(char *) git__strdup(const char *str) +{ + char *ptr = strdup(str); + if (!ptr) + git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string"); + return ptr; +} + +GIT_INLINE(void *) git__realloc(void *ptr, size_t size) +{ + void *new_ptr = realloc(ptr, size); + if (!new_ptr) + git__throw(GIT_ENOMEM, "Out of memory. Failed to allocate %d bytes.", (int)size); + return new_ptr; +} extern int git__fmt(char *, size_t, const char *, ...) GIT_FORMAT_PRINTF(3, 4); |