comparison 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
comparison
equal deleted inserted replaced
7512:d9ce9088911e 7513:37e061ec063c
795 num_alloc, num_freed); 795 num_alloc, num_freed);
796 } 796 }
797 797
798 #endif /* MEM_PROFILE */ 798 #endif /* MEM_PROFILE */
799 799
800 #ifdef FEAT_EVAL
801 static int
802 alloc_does_fail()
803 {
804 if (alloc_fail_countdown == 0)
805 {
806 if (--alloc_fail_repeat <= 0)
807 alloc_fail_id = 0;
808 return TRUE;
809 }
810 --alloc_fail_countdown;
811 return FALSE;
812 }
813 #endif
814
800 /* 815 /*
801 * Some memory is reserved for error messages and for being able to 816 * Some memory is reserved for error messages and for being able to
802 * call mf_release_all(), which needs some memory for mf_trans_add(). 817 * call mf_release_all(), which needs some memory for mf_trans_add().
803 */ 818 */
804 #if defined(MSDOS) && !defined(DJGPP) 819 #if defined(MSDOS) && !defined(DJGPP)
815 */ 830 */
816 char_u * 831 char_u *
817 alloc(size) 832 alloc(size)
818 unsigned size; 833 unsigned size;
819 { 834 {
835 return (lalloc((long_u)size, TRUE));
836 }
837
838 /*
839 * alloc() with an ID for alloc_fail().
840 * LAST_ID_USED: 5
841 */
842 char_u *
843 alloc_id(size, id)
844 unsigned size;
845 int id;
846 {
847 #ifdef FEAT_EVAL
848 if (alloc_fail_id == id && alloc_does_fail())
849 return NULL;
850 #endif
820 return (lalloc((long_u)size, TRUE)); 851 return (lalloc((long_u)size, TRUE));
821 } 852 }
822 853
823 /* 854 /*
824 * Allocate memory and set all bytes to zero. 855 * Allocate memory and set all bytes to zero.
964 theend: 995 theend:
965 #ifdef MEM_PROFILE 996 #ifdef MEM_PROFILE
966 mem_post_alloc((void **)&p, (size_t)size); 997 mem_post_alloc((void **)&p, (size_t)size);
967 #endif 998 #endif
968 return p; 999 return p;
1000 }
1001
1002 /*
1003 * lalloc() with an ID for alloc_fail().
1004 * See LAST_ID_USED above.
1005 */
1006 char_u *
1007 lalloc_id(size, message, id)
1008 long_u size;
1009 int message;
1010 int id;
1011 {
1012 #ifdef FEAT_EVAL
1013 if (alloc_fail_id == id && alloc_does_fail())
1014 return NULL;
1015 #endif
1016 return (lalloc((long_u)size, message));
969 } 1017 }
970 1018
971 #if defined(MEM_PROFILE) || defined(PROTO) 1019 #if defined(MEM_PROFILE) || defined(PROTO)
972 /* 1020 /*
973 * realloc() with memory profiling. 1021 * realloc() with memory profiling.