mirror of
https://github.com/neovim/neovim.git
synced 2025-09-27 05:28:33 +00:00
(verbose_)?try_malloc() to use on buf_write()
There will be more use cases for try_malloc(): see #556. - Reimplemented xmalloc() using try_malloc(). - verbose_try_malloc() is just like try_malloc() but shows an out-of-memory error message before returning NULL. - Let the compiler generate size>>1 assembly for signed types. We're not using old compilers here. - Add proper function attributes to the new functions in memory.h
This commit is contained in:

committed by
Thiago de Arruda

parent
1befc49414
commit
15d8f702a8
30
src/memory.c
30
src/memory.c
@@ -81,24 +81,40 @@ static void try_to_free_memory()
|
||||
trying_to_free = false;
|
||||
}
|
||||
|
||||
void *xmalloc(size_t size)
|
||||
void *try_malloc(size_t size)
|
||||
{
|
||||
void *ret = malloc(size);
|
||||
|
||||
if (!ret && !size)
|
||||
if (!ret && !size) {
|
||||
ret = malloc(1);
|
||||
|
||||
}
|
||||
if (!ret) {
|
||||
try_to_free_memory();
|
||||
ret = malloc(size);
|
||||
if (!ret && !size)
|
||||
if (!ret && !size) {
|
||||
ret = malloc(1);
|
||||
if (!ret) {
|
||||
OUT_STR("Vim: Error: Out of memory.\n");
|
||||
preserve_exit();
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *verbose_try_malloc(size_t size)
|
||||
{
|
||||
void *ret = try_malloc(size);
|
||||
if (!ret) {
|
||||
do_outofmem_msg((long_u)size);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *xmalloc(size_t size)
|
||||
{
|
||||
void *ret = try_malloc(size);
|
||||
|
||||
if (!ret) {
|
||||
OUT_STR("Vim: Error: Out of memory.\n");
|
||||
preserve_exit();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user