aboutsummaryrefslogtreecommitdiff
path: root/src/memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/memory.c')
-rw-r--r--src/memory.c32
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);