annotate src/alloc.c @ 30100:84c18beec6bc v9.0.0386

patch 9.0.0386: some code blocks are nested too deep Commit: https://github.com/vim/vim/commit/b1f471ee20b0fa783ecd6e29aa69067e6c821376 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Mon Sep 5 14:33:47 2022 +0100 patch 9.0.0386: some code blocks are nested too deep Problem: Some code blocks are nested too deep. Solution: Bail out earlier. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/11058)
author Bram Moolenaar <Bram@vim.org>
date Mon, 05 Sep 2022 15:45:03 +0200
parents 31c598083364
children d7066cbac096
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2 *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 * alloc.c: functions for memory management
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 #include "vim.h"
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 /**********************************************************************
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 * Various routines dealing with allocation and deallocation of memory.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 #if defined(MEM_PROFILE) || defined(PROTO)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 # define MEM_SIZES 8200
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 static long_u mem_allocs[MEM_SIZES];
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 static long_u mem_frees[MEM_SIZES];
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 static long_u mem_allocated;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 static long_u mem_freed;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 static long_u mem_peak;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 static long_u num_alloc;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 static long_u num_freed;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 static void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 mem_pre_alloc_s(size_t *sizep)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
34 *sizep += sizeof(size_t);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 static void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 mem_pre_alloc_l(size_t *sizep)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 *sizep += sizeof(size_t);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 static void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 mem_post_alloc(
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 void **pp,
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 size_t size)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 if (*pp == NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 size -= sizeof(size_t);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 *(long_u *)*pp = size;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 if (size <= MEM_SIZES-1)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 mem_allocs[size-1]++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 mem_allocs[MEM_SIZES-1]++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 mem_allocated += size;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 if (mem_allocated - mem_freed > mem_peak)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 mem_peak = mem_allocated - mem_freed;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 num_alloc++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 *pp = (void *)((char *)*pp + sizeof(size_t));
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 static void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 mem_pre_free(void **pp)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 long_u size;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 *pp = (void *)((char *)*pp - sizeof(size_t));
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 size = *(size_t *)*pp;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70 if (size <= MEM_SIZES-1)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 mem_frees[size-1]++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 mem_frees[MEM_SIZES-1]++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 mem_freed += size;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 num_freed++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 * called on exit via atexit()
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 vim_mem_profile_dump(void)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 int i, j;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 printf("\r\n");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 j = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 for (i = 0; i < MEM_SIZES - 1; i++)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 {
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
90 if (mem_allocs[i] == 0 && mem_frees[i] == 0)
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
91 continue;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
92
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
93 if (mem_frees[i] > mem_allocs[i])
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
94 printf("\r\n%s", _("ERROR: "));
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
95 printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
96 j++;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
97 if (j > 3)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 {
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
99 j = 0;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
100 printf("\r\n");
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 i = MEM_SIZES - 1;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 if (mem_allocs[i])
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 printf("\r\n");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 if (mem_frees[i] > mem_allocs[i])
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 puts(_("ERROR: "));
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 num_alloc, num_freed);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 #endif // MEM_PROFILE
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 #ifdef FEAT_EVAL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 alloc_does_fail(size_t size)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 if (alloc_fail_countdown == 0)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 if (--alloc_fail_repeat <= 0)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128 alloc_fail_id = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 do_outofmem_msg(size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 return TRUE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 --alloc_fail_countdown;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 return FALSE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138 * Some memory is reserved for error messages and for being able to
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 * call mf_release_all(), which needs some memory for mf_trans_add().
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 #define KEEP_ROOM (2 * 8192L)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
142 #define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
143
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
144 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
145 * The normal way to allocate memory. This handles an out-of-memory situation
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
146 * as well as possible, still returns NULL when we're completely out.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
147 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
148 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
149 alloc(size_t size)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
150 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
151 return lalloc(size, TRUE);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
154 #if defined(FEAT_QUICKFIX) || defined(PROTO)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 * alloc() with an ID for alloc_fail().
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 alloc_id(size_t size, alloc_id_T id UNUSED)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 {
29853
31c598083364 patch 9.0.0265: no good reason why the "gf" command isn't in the tiny version
Bram Moolenaar <Bram@vim.org>
parents: 29344
diff changeset
161 # ifdef FEAT_EVAL
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 if (alloc_fail_id == id && alloc_does_fail(size))
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 return NULL;
29853
31c598083364 patch 9.0.0265: no good reason why the "gf" command isn't in the tiny version
Bram Moolenaar <Bram@vim.org>
parents: 29344
diff changeset
164 # endif
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165 return lalloc(size, TRUE);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 }
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
167 #endif
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 * Allocate memory and set all bytes to zero.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 alloc_clear(size_t size)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 void *p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 p = lalloc(size, TRUE);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 if (p != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 (void)vim_memset(p, 0, size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 * Same as alloc_clear() but with allocation id for testing
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 alloc_clear_id(size_t size, alloc_id_T id UNUSED)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 #ifdef FEAT_EVAL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 if (alloc_fail_id == id && alloc_does_fail(size))
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 return alloc_clear(size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 * Allocate memory like lalloc() and set all bytes to zero.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 lalloc_clear(size_t size, int message)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
202 void *p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
203
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
204 p = lalloc(size, message);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
205 if (p != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 (void)vim_memset(p, 0, size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
208 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
209
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
210 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
211 * Low level memory allocation function.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 * This is used often, KEEP IT FAST!
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
214 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 lalloc(size_t size, int message)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
216 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
217 void *p; // pointer to new storage space
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
218 static int releasing = FALSE; // don't do mf_release_all() recursive
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
219 int try_again;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
220 #if defined(HAVE_AVAIL_MEM)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221 static size_t allocated = 0; // allocated since last avail check
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
223
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
224 // Safety check for allocating zero bytes
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 if (size == 0)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
226 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 // Don't hide this message
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 emsg_silent = 0;
26863
6ee19c6ae8a2 patch 8.2.3960: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26771
diff changeset
229 iemsg(_(e_internal_error_lalloc_zero));
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
232
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
233 #ifdef MEM_PROFILE
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234 mem_pre_alloc_l(&size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
235 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
236
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
237 // Loop when out of memory: Try to release some memfile blocks and
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
238 // if some blocks are released call malloc again.
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
239 for (;;)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
240 {
26771
fc859aea8cec patch 8.2.3914: various spelling mistakes in comments
Bram Moolenaar <Bram@vim.org>
parents: 26418
diff changeset
241 // Handle three kinds of systems:
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
242 // 1. No check for available memory: Just return.
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
243 // 2. Slow check for available memory: call mch_avail_mem() after
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
244 // allocating KEEP_ROOM amount of memory.
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
245 // 3. Strict check for available memory: call mch_avail_mem()
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 if ((p = malloc(size)) != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
247 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
248 #ifndef HAVE_AVAIL_MEM
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 // 1. No check for available memory: Just return.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 goto theend;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
251 #else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
252 // 2. Slow check for available memory: call mch_avail_mem() after
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 // allocating (KEEP_ROOM / 2) amount of memory.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
254 allocated += size;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 if (allocated < KEEP_ROOM / 2)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256 goto theend;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 allocated = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
258
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
259 // 3. check for available memory: call mch_avail_mem()
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
261 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 free(p); // System is low... no go!
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 p = NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 goto theend;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
267 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
268 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
269 // Remember that mf_release_all() is being called to avoid an endless
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
270 // loop, because mf_release_all() may call alloc() recursively.
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 if (releasing)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272 break;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 releasing = TRUE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
274
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
275 clear_sb_text(TRUE); // free any scrollback text
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 try_again = mf_release_all(); // release as many blocks as possible
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
278 releasing = FALSE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 if (!try_again)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280 break;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
282
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
283 if (message && p == NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 do_outofmem_msg(size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
286 theend:
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
287 #ifdef MEM_PROFILE
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288 mem_post_alloc(&p, size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
291 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
292
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
293 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294 * lalloc() with an ID for alloc_fail().
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
295 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 #if defined(FEAT_SIGNS) || defined(PROTO)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
297 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 lalloc_id(size_t size, int message, alloc_id_T id UNUSED)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
299 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
300 #ifdef FEAT_EVAL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301 if (alloc_fail_id == id && alloc_does_fail(size))
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
303 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 return (lalloc(size, message));
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
305 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
306 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
307
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 #if defined(MEM_PROFILE) || defined(PROTO)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
309 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 * realloc() with memory profiling.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
312 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 mem_realloc(void *ptr, size_t size)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
314 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
315 void *p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
316
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
317 mem_pre_free(&ptr);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318 mem_pre_alloc_s(&size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
319
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 p = realloc(ptr, size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
321
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 mem_post_alloc(&p, size);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
323
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
324 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
325 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
326 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
328 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
329 * Avoid repeating the error message many times (they take 1 second each).
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330 * Did_outofmem_msg is reset when a character is read.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
332 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 do_outofmem_msg(size_t size)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 {
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
335 if (did_outofmem_msg)
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
336 return;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
337
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
338 // Don't hide this message
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
339 emsg_silent = 0;
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
341 // Must come first to avoid coming back here when printing the error
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
342 // message fails, e.g. when setting v:errmsg.
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
343 did_outofmem_msg = TRUE;
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
345 semsg(_(e_out_of_memory_allocating_nr_bytes), (long_u)size);
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
346
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
347 if (starting == NO_SCREEN)
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
348 // Not even finished with initializations and already out of
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
349 // memory? Then nothing is going to work, exit.
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
350 mch_exit(123);
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
352
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 #if defined(EXITFREE) || defined(PROTO)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
355 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
356 * Free everything that we allocated.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
357 * Can be used to detect memory leaks, e.g., with ccmalloc.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
358 * NOTE: This is tricky! Things are freed that functions depend on. Don't be
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
359 * surprised if Vim crashes...
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 * Some things can't be freed, esp. things local to a library function.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
362 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 free_all_mem(void)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
364 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 buf_T *buf, *nextbuf;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
366
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
367 // When we cause a crash here it is caught and Vim tries to exit cleanly.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
368 // Don't try freeing everything again.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 if (entered_free_all_mem)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
370 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
371 entered_free_all_mem = TRUE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372 // Don't want to trigger autocommands from here on.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 block_autocmds();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
374
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
375 // Close all tabs and windows. Reset 'equalalways' to avoid redraws.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
376 p_ea = FALSE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
377 if (first_tabpage != NULL && first_tabpage->tp_next != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
378 do_cmdline_cmd((char_u *)"tabonly!");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
379 if (!ONE_WINDOW)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 do_cmdline_cmd((char_u *)"only!");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
381
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
382 # if defined(FEAT_SPELL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
383 // Free all spell info.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384 spell_free_all();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
386
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
387 # if defined(FEAT_BEVAL_TERM)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388 ui_remove_balloon();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
389 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
390 # ifdef FEAT_PROP_POPUP
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
391 if (curwin != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392 close_all_popups(TRUE);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
394
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
395 // Clear user commands (before deleting buffers).
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396 ex_comclear(NULL);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
397
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
398 // When exiting from mainerr_arg_missing curbuf has not been initialized,
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
399 // and not much else.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 if (curbuf != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
401 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 # ifdef FEAT_MENU
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403 // Clear menus.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
404 do_cmdline_cmd((char_u *)"aunmenu *");
29344
5f0039383340 patch 9.0.0015: with EXITFREE defined terminal menus are not cleared
Bram Moolenaar <Bram@vim.org>
parents: 29255
diff changeset
405 do_cmdline_cmd((char_u *)"tlunmenu *");
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406 # ifdef FEAT_MULTI_LANG
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407 do_cmdline_cmd((char_u *)"menutranslate clear");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
408 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
409 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
410 // Clear mappings, abbreviations, breakpoints.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
411 do_cmdline_cmd((char_u *)"lmapclear");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
412 do_cmdline_cmd((char_u *)"xmapclear");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
413 do_cmdline_cmd((char_u *)"mapclear");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
414 do_cmdline_cmd((char_u *)"mapclear!");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
415 do_cmdline_cmd((char_u *)"abclear");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
416 # if defined(FEAT_EVAL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
417 do_cmdline_cmd((char_u *)"breakdel *");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
418 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
419 # if defined(FEAT_PROFILE)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
420 do_cmdline_cmd((char_u *)"profdel *");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
421 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
422 # if defined(FEAT_KEYMAP)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423 do_cmdline_cmd((char_u *)"set keymap=");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 free_titles();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
428 free_findfile();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
429
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430 // Obviously named calls.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 free_all_autocmds();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
432 clear_termcodes();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
433 free_all_marks();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434 alist_clear(&global_alist);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
435 free_homedir();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
436 free_users();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
437 free_search_patterns();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 free_old_sub();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
439 free_last_insert();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
440 free_insexpand_stuff();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
441 free_prev_shellcmd();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
442 free_regexp_stuff();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
443 free_tag_stuff();
26408
8f17f8f327f3 patch 8.2.3735: cannot use a lambda for 'imactivatefunc'
Bram Moolenaar <Bram@vim.org>
parents: 26336
diff changeset
444 free_xim_stuff();
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
445 free_cd_dir();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
446 # ifdef FEAT_SIGNS
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
447 free_signs();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
448 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
449 # ifdef FEAT_EVAL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
450 set_expr_line(NULL, NULL);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
451 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
452 # ifdef FEAT_DIFF
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
453 if (curtab != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
454 diff_clear(curtab);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
455 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
456 clear_sb_text(TRUE); // free any scrollback text
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
457
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
458 // Free some global vars.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
459 free_username();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
460 # ifdef FEAT_CLIPBOARD
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
461 vim_regfree(clip_exclude_prog);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
462 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
463 vim_free(last_cmdline);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
464 vim_free(new_last_cmdline);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
465 set_keep_msg(NULL, 0);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
466
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
467 // Clear cmdline history.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
468 p_hi = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
469 init_history();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
470 # ifdef FEAT_PROP_POPUP
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
471 clear_global_prop_types();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
472 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
473
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
474 # ifdef FEAT_QUICKFIX
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
475 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
476 win_T *win;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
477 tabpage_T *tab;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
478
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
479 qf_free_all(NULL);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
480 // Free all location lists
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
481 FOR_ALL_TAB_WINDOWS(tab, win)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
482 qf_free_all(win);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
483 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
484 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
485
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
486 // Close all script inputs.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
487 close_all_scripts();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
488
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
489 if (curwin != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
490 // Destroy all windows. Must come before freeing buffers.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
491 win_free_all();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
492
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
493 // Free all option values. Must come after closing windows.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
494 free_all_options();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
495
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
496 // Free all buffers. Reset 'autochdir' to avoid accessing things that
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
497 // were freed already.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
498 # ifdef FEAT_AUTOCHDIR
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
499 p_acd = FALSE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
500 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
501 for (buf = firstbuf; buf != NULL; )
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
502 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
503 bufref_T bufref;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
504
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
505 set_bufref(&bufref, buf);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
506 nextbuf = buf->b_next;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
507 close_buffer(NULL, buf, DOBUF_WIPE, FALSE, FALSE);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
508 if (bufref_valid(&bufref))
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
509 buf = nextbuf; // didn't work, try next one
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
510 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
511 buf = firstbuf;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
512 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
513
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
514 # ifdef FEAT_ARABIC
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
515 free_arshape_buf();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
516 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
517
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
518 // Clear registers.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
519 clear_registers();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
520 ResetRedobuff();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
521 ResetRedobuff();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
522
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
523 # if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
524 vim_free(serverDelayedStartName);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
525 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
526
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
527 // highlight info
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
528 free_highlight();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
530 reset_last_sourcing();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
531
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
532 if (first_tabpage != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
533 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
534 free_tabpage(first_tabpage);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
535 first_tabpage = NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
536 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
537
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
538 # ifdef UNIX
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
539 // Machine-specific free.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
540 mch_free_mem();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
541 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
542
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
543 // message history
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
544 for (;;)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
545 if (delete_first_msg() == FAIL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
546 break;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
547
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
548 # ifdef FEAT_JOB_CHANNEL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
549 channel_free_all();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
550 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
551 # ifdef FEAT_TIMERS
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
552 timer_free_all();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
553 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
554 # ifdef FEAT_EVAL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
555 // must be after channel_free_all() with unrefs partials
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
556 eval_clear();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
557 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
558 # ifdef FEAT_JOB_CHANNEL
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
559 // must be after eval_clear() with unrefs jobs
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
560 job_free_all();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
561 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
562
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
563 free_termoptions();
26177
13e09dc59f0f patch 8.2.3620: memory leak reported in libtlib
Bram Moolenaar <Bram@vim.org>
parents: 25567
diff changeset
564 free_cur_term();
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
565
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
566 // screenlines (can't display anything now!)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
567 free_screenlines();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
569 # if defined(FEAT_SOUND)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570 sound_free();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
572 # if defined(USE_XSMP)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
573 xsmp_close();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
574 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
575 # ifdef FEAT_GUI_GTK
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
576 gui_mch_free_all();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
577 # endif
26418
9a1b96ae26d1 patch 8.2.3740: memory left allocated on exit when using Tcl
Bram Moolenaar <Bram@vim.org>
parents: 26408
diff changeset
578 # ifdef FEAT_TCL
9a1b96ae26d1 patch 8.2.3740: memory left allocated on exit when using Tcl
Bram Moolenaar <Bram@vim.org>
parents: 26408
diff changeset
579 vim_tcl_finalize();
9a1b96ae26d1 patch 8.2.3740: memory left allocated on exit when using Tcl
Bram Moolenaar <Bram@vim.org>
parents: 26408
diff changeset
580 # endif
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
581 clear_hl_tables();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
582
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
583 vim_free(IObuff);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
584 vim_free(NameBuff);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
585 # ifdef FEAT_QUICKFIX
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
586 check_quickfix_busy();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587 # endif
29255
5ebc561444fe patch 8.2.5146: memory leak when substitute expression nests
Bram Moolenaar <Bram@vim.org>
parents: 28813
diff changeset
588 # ifdef FEAT_EVAL
5ebc561444fe patch 8.2.5146: memory leak when substitute expression nests
Bram Moolenaar <Bram@vim.org>
parents: 28813
diff changeset
589 free_resub_eval_result();
5ebc561444fe patch 8.2.5146: memory leak when substitute expression nests
Bram Moolenaar <Bram@vim.org>
parents: 28813
diff changeset
590 # endif
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
591 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
593
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
595 * Copy "p[len]" into allocated memory, ignoring NUL characters.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
596 * Returns NULL when out of memory.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
597 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
598 char_u *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 vim_memsave(char_u *p, size_t len)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
600 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
601 char_u *ret = alloc(len);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
602
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
603 if (ret != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
604 mch_memmove(ret, p, len);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
605 return ret;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
606 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
607
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
608 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
609 * Replacement for free() that ignores NULL pointers.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
610 * Also skip free() when exiting for sure, this helps when we caught a deadly
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
611 * signal that was caused by a crash in free().
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
612 * If you want to set NULL after calling this function, you should use
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
613 * VIM_CLEAR() instead.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
614 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
615 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
616 vim_free(void *x)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
617 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
618 if (x != NULL && !really_exiting)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
619 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
620 #ifdef MEM_PROFILE
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
621 mem_pre_free(&x);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
622 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
623 free(x);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
624 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
625 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
626
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
627 /************************************************************************
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
628 * Functions for handling growing arrays.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
629 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
630
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
631 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
632 * Clear an allocated growing array.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
633 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
634 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
635 ga_clear(garray_T *gap)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
636 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
637 vim_free(gap->ga_data);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
638 ga_init(gap);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
639 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
640
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
641 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
642 * Clear a growing array that contains a list of strings.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
643 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
644 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
645 ga_clear_strings(garray_T *gap)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
646 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
647 int i;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
648
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
649 if (gap->ga_data != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
650 for (i = 0; i < gap->ga_len; ++i)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
651 vim_free(((char_u **)(gap->ga_data))[i]);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 ga_clear(gap);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
653 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
654
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
655 #if defined(FEAT_EVAL) || defined(PROTO)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
656 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
657 * Copy a growing array that contains a list of strings.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
658 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
659 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660 ga_copy_strings(garray_T *from, garray_T *to)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
661 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
662 int i;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
663
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
664 ga_init2(to, sizeof(char_u *), 1);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
665 if (ga_grow(to, from->ga_len) == FAIL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
666 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
667
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
668 for (i = 0; i < from->ga_len; ++i)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
670 char_u *orig = ((char_u **)from->ga_data)[i];
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
671 char_u *copy;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
672
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
673 if (orig == NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
674 copy = NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
675 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
676 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
677 copy = vim_strsave(orig);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
678 if (copy == NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
679 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
680 to->ga_len = i;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
681 ga_clear_strings(to);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
682 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
683 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
684 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
685 ((char_u **)to->ga_data)[i] = copy;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
686 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
687 to->ga_len = from->ga_len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
688 return OK;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
689 }
27018
268f6a3511df patch 8.2.4038: various code not used when features are disabled
Bram Moolenaar <Bram@vim.org>
parents: 26863
diff changeset
690 #endif
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
691
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
692 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
693 * Initialize a growing array. Don't forget to set ga_itemsize and
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
694 * ga_growsize! Or use ga_init2().
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
695 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
696 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
697 ga_init(garray_T *gap)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
698 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
699 gap->ga_data = NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
700 gap->ga_maxlen = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
701 gap->ga_len = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
702 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
703
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
704 void
27022
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
705 ga_init2(garray_T *gap, size_t itemsize, int growsize)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
706 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
707 ga_init(gap);
27390
018c911eb9cf patch 8.2.4223: long/int compiler warnings; function arguments swapped
Bram Moolenaar <Bram@vim.org>
parents: 27022
diff changeset
708 gap->ga_itemsize = (int)itemsize;
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
709 gap->ga_growsize = growsize;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
710 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
711
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
712 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
713 * Make room in growing array "gap" for at least "n" items.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
714 * Return FAIL for failure, OK otherwise.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
715 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
716 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
717 ga_grow(garray_T *gap, int n)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
718 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
719 if (gap->ga_maxlen - gap->ga_len < n)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
720 return ga_grow_inner(gap, n);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
721 return OK;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
722 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
723
28382
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
724 /*
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
725 * Same as ga_grow() but uses an allocation id for testing.
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
726 */
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
727 int
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
728 ga_grow_id(garray_T *gap, int n, alloc_id_T id UNUSED)
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
729 {
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
730 #ifdef FEAT_EVAL
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
731 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
732 return FAIL;
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
733 #endif
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
734
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
735 return ga_grow(gap, n);
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
736 }
f24d4826e6bf patch 8.2.4716: memory allocation failure not tested when defining a function
Bram Moolenaar <Bram@vim.org>
parents: 28311
diff changeset
737
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
738 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
739 ga_grow_inner(garray_T *gap, int n)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
740 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
741 size_t old_len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
742 size_t new_len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
743 char_u *pp;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
744
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
745 if (n < gap->ga_growsize)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
746 n = gap->ga_growsize;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
748 // A linear growth is very inefficient when the array grows big. This
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
749 // is a compromise between allocating memory that won't be used and too
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
750 // many copy operations. A factor of 1.5 seems reasonable.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
751 if (n < gap->ga_len / 2)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
752 n = gap->ga_len / 2;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
753
27453
c7f614c9ceb3 patch 8.2.4255: theoretical computation overflow
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
754 new_len = (size_t)gap->ga_itemsize * (gap->ga_len + n);
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
755 pp = vim_realloc(gap->ga_data, new_len);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
756 if (pp == NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
757 return FAIL;
27453
c7f614c9ceb3 patch 8.2.4255: theoretical computation overflow
Bram Moolenaar <Bram@vim.org>
parents: 27390
diff changeset
758 old_len = (size_t)gap->ga_itemsize * gap->ga_maxlen;
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759 vim_memset(pp + old_len, 0, new_len - old_len);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 gap->ga_maxlen = gap->ga_len + n;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761 gap->ga_data = pp;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
762 return OK;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
763 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
764
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
765 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
766 * For a growing array that contains a list of strings: concatenate all the
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
767 * strings with a separating "sep".
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
768 * Returns NULL when out of memory.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
769 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
770 char_u *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
771 ga_concat_strings(garray_T *gap, char *sep)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
772 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
773 int i;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
774 int len = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
775 int sep_len = (int)STRLEN(sep);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
776 char_u *s;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
777 char_u *p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
778
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
779 for (i = 0; i < gap->ga_len; ++i)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
780 len += (int)STRLEN(((char_u **)(gap->ga_data))[i]) + sep_len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
781
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
782 s = alloc(len + 1);
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
783 if (s == NULL)
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
784 return NULL;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
785
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
786 *s = NUL;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
787 p = s;
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
788 for (i = 0; i < gap->ga_len; ++i)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789 {
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
790 if (p != s)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
791 {
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
792 STRCPY(p, sep);
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
793 p += sep_len;
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
794 }
30100
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
795 STRCPY(p, ((char_u **)(gap->ga_data))[i]);
84c18beec6bc patch 9.0.0386: some code blocks are nested too deep
Bram Moolenaar <Bram@vim.org>
parents: 29853
diff changeset
796 p += STRLEN(p);
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
797 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
798 return s;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
799 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
800
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
801 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
802 * Make a copy of string "p" and add it to "gap".
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
803 * When out of memory nothing changes and FAIL is returned.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
804 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 int
27022
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
806 ga_copy_string(garray_T *gap, char_u *p)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
807 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808 char_u *cp = vim_strsave(p);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
810 if (cp == NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
811 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
812
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
813 if (ga_grow(gap, 1) == FAIL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
815 vim_free(cp);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
816 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
817 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
818 ((char_u **)(gap->ga_data))[gap->ga_len++] = cp;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
819 return OK;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
820 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
821
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
822 /*
27022
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
823 * Add string "p" to "gap".
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
824 * When out of memory "p" is freed and FAIL is returned.
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
825 */
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
826 int
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
827 ga_add_string(garray_T *gap, char_u *p)
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
828 {
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
829 if (ga_grow(gap, 1) == FAIL)
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
830 return FAIL;
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
831 ((char_u **)(gap->ga_data))[gap->ga_len++] = p;
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
832 return OK;
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
833 }
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
834
eebbcc83fb75 patch 8.2.4040: keeping track of allocated lines is too complicated
Bram Moolenaar <Bram@vim.org>
parents: 27018
diff changeset
835 /*
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
836 * Concatenate a string to a growarray which contains bytes.
28813
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
837 * When "s" is NULL memory allocation fails does not do anything.
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
838 * Note: Does NOT copy the NUL at the end!
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
840 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
841 ga_concat(garray_T *gap, char_u *s)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
842 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
843 int len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
844
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
845 if (s == NULL || *s == NUL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847 len = (int)STRLEN(s);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
848 if (ga_grow(gap, len) == OK)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
849 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
850 mch_memmove((char *)gap->ga_data + gap->ga_len, s, (size_t)len);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
851 gap->ga_len += len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
852 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
853 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
854
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
855 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
856 * Concatenate 'len' bytes from string 's' to a growarray.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
857 * When "s" is NULL does not do anything.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
858 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
859 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
860 ga_concat_len(garray_T *gap, char_u *s, size_t len)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
861 {
28139
f34afadbef47 patch 8.2.4594: need to write script to a file to be able to source them
Bram Moolenaar <Bram@vim.org>
parents: 27453
diff changeset
862 if (s == NULL || *s == NUL || len == 0)
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
863 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
864 if (ga_grow(gap, (int)len) == OK)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
865 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
866 mch_memmove((char *)gap->ga_data + gap->ga_len, s, len);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
867 gap->ga_len += (int)len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
868 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
869 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
870
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
871 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
872 * Append one byte to a growarray which contains bytes.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
873 */
28813
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
874 int
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
875 ga_append(garray_T *gap, int c)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
876 {
28813
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
877 if (ga_grow(gap, 1) == FAIL)
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
878 return FAIL;
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
879 *((char *)gap->ga_data + gap->ga_len) = c;
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
880 ++gap->ga_len;
3626ca6a20ea patch 8.2.4930: interpolated string expression requires escaping
Bram Moolenaar <Bram@vim.org>
parents: 28382
diff changeset
881 return OK;
25529
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
882 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
883
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
884 #if (defined(UNIX) && !defined(USE_SYSTEM)) || defined(MSWIN) \
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
885 || defined(PROTO)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
886 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
887 * Append the text in "gap" below the cursor line and clear "gap".
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
888 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
889 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
890 append_ga_line(garray_T *gap)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
891 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
892 // Remove trailing CR.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
893 if (gap->ga_len > 0
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
894 && !curbuf->b_p_bin
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
895 && ((char_u *)gap->ga_data)[gap->ga_len - 1] == CAR)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
896 --gap->ga_len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
897 ga_append(gap, NUL);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
898 ml_append(curwin->w_cursor.lnum++, gap->ga_data, 0, FALSE);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
899 gap->ga_len = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
900 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
901 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
902