Mercurial > vim
diff src/misc2.c @ 7513:37e061ec063c v7.4.1058
commit https://github.com/vim/vim/commit/75bdf6aa30a5c99d67c42886cf7a4a000bbaa422
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jan 7 21:25:08 2016 +0100
patch 7.4.1058
Problem: It is not possible to test code that is only reached when memory
allocation fails.
Solution: Add the alloc_fail() function. Try it out with :vimgrep.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Thu, 07 Jan 2016 21:30:05 +0100 |
parents | ad432f8f68fb |
children | 98fede2c9574 |
line wrap: on
line diff
--- a/src/misc2.c +++ b/src/misc2.c @@ -797,6 +797,21 @@ vim_mem_profile_dump() #endif /* MEM_PROFILE */ +#ifdef FEAT_EVAL + static int +alloc_does_fail() +{ + if (alloc_fail_countdown == 0) + { + if (--alloc_fail_repeat <= 0) + alloc_fail_id = 0; + return TRUE; + } + --alloc_fail_countdown; + return FALSE; +} +#endif + /* * Some memory is reserved for error messages and for being able to * call mf_release_all(), which needs some memory for mf_trans_add(). @@ -821,6 +836,22 @@ alloc(size) } /* + * alloc() with an ID for alloc_fail(). + * LAST_ID_USED: 5 + */ + char_u * +alloc_id(size, id) + unsigned size; + int id; +{ +#ifdef FEAT_EVAL + if (alloc_fail_id == id && alloc_does_fail()) + return NULL; +#endif + return (lalloc((long_u)size, TRUE)); +} + +/* * Allocate memory and set all bytes to zero. */ char_u * @@ -968,6 +999,23 @@ theend: return p; } +/* + * lalloc() with an ID for alloc_fail(). + * See LAST_ID_USED above. + */ + char_u * +lalloc_id(size, message, id) + long_u size; + int message; + int id; +{ +#ifdef FEAT_EVAL + if (alloc_fail_id == id && alloc_does_fail()) + return NULL; +#endif + return (lalloc((long_u)size, message)); +} + #if defined(MEM_PROFILE) || defined(PROTO) /* * realloc() with memory profiling.