diff options
author | Felipe Oliveira Carvalho <felipekde@gmail.com> | 2014-04-05 16:54:01 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-07 00:22:01 -0300 |
commit | fac85c17248ab8c371fb311c1dd4bc1b2a7520cf (patch) | |
tree | 431f4fb73eb063ac4a123c61c043a1d833b00682 /src/memory.c | |
parent | fa02ada73257336641ebb29a9ea0b626c176c011 (diff) | |
download | rneovim-fac85c17248ab8c371fb311c1dd4bc1b2a7520cf.tar.gz rneovim-fac85c17248ab8c371fb311c1dd4bc1b2a7520cf.tar.bz2 rneovim-fac85c17248ab8c371fb311c1dd4bc1b2a7520cf.zip |
Implement xcalloc and use it in klist.h (use xrealloc as well)
Bonus: implement lalloc_clear and alloc_clear using xcalloc
Diffstat (limited to 'src/memory.c')
-rw-r--r-- | src/memory.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/memory.c b/src/memory.c index a878cb63da..8706724d72 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,5 +1,6 @@ // Various routines dealing with allocation and deallocation of memory. +#include <stdlib.h> #include <string.h> #include "vim.h" @@ -68,11 +69,7 @@ char_u *alloc(unsigned size) */ char_u *alloc_clear(unsigned size) { - char_u *p; - - p = lalloc((long_u)size, TRUE); - (void)memset(p, 0, (size_t)size); - return p; + return (char_u *)xcalloc(1, (size_t)size); } /* @@ -96,9 +93,7 @@ char_u *alloc_check(unsigned size) */ char_u *lalloc_clear(long_u size, int message) { - char_u *p = lalloc(size, message); - memset(p, 0, (size_t)size); - return p; + return (char_u *)xcalloc(1, (size_t)size); } /// Try to free memory. Used when trying to recover from out of memory errors. @@ -142,6 +137,27 @@ void *xmalloc(size_t size) return ret; } +void *xcalloc(size_t count, size_t size) +{ + void *ret = calloc(count, size); + + if (!ret && (!count || !size)) + ret = calloc(1, 1); + + if (!ret) { + try_to_free_memory(); + ret = calloc(count, size); + if (!ret && (!count || !size)) + ret = calloc(1, 1); + if (!ret) { + OUT_STR("Vim: Error: Out of memory.\n"); + preserve_exit(); + } + } + + return ret; +} + void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); |