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.
diff --git a/src/misc2.c b/src/misc2.c
index 9f8d7ad..65ac886 100644
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -797,6 +797,21 @@
#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() 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 @@
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.