char * xstrdup(const char *str) { char *ret = strdup(str); if (!ret) { try_to_free_memory(); ret = strdup(str); if (!ret) { OUT_STR("Vim: Error: Out of memory.\n"); preserve_exit(); } } return ret; }
void *try_malloc(size_t size) { void *ret = malloc(size); if (!ret && !size) { ret = malloc(1); } if (!ret) { try_to_free_memory(); ret = malloc(size); if (!ret && !size) { ret = malloc(1); } } return ret; }
void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); if (!ret && !size) ret = realloc(ptr, 1); if (!ret) { try_to_free_memory(); ret = realloc(ptr, size); if (!ret && !size) ret = realloc(ptr, 1); if (!ret) { OUT_STR("Vim: Error: Out of memory.\n"); preserve_exit(); } } 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; }