annotate src/alloc.c @ 25567:0082503ff2ff v8.2.3320

patch 8.2.3320: some local functions are not static Commit: https://github.com/vim/vim/commit/8ee52affe7fd4daa03e002bc06611f0a8c3bcd5b Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Mon Aug 9 19:59:06 2021 +0200 patch 8.2.3320: some local functions are not static Problem: Some local functions are not static. Solution: Add "static". Move snprintf() related code to strings.c. (Yegappan Lakshmanan, closes #8734)
author Bram Moolenaar <Bram@vim.org>
date Mon, 09 Aug 2021 20:00:06 +0200
parents bb1097899693
children 13e09dc59f0f
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 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 if (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
91 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 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
93 printf("\r\n%s", _("ERROR: "));
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 printf("[%4d / %4lu-%-4lu] ", i + 1, 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
95 j++;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 if (j > 3)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 j = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 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
100 }
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
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 * 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
156 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 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
159 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 #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
161 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
162 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 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
165 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 * 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
169 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 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
172 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 void *p;
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 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
176 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
177 (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
178 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180
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 * 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
183 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185 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
186 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 #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
188 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
189 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 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
192 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193
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 * 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
196 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 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
199 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200 void *p;
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 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
203 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
204 (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
205 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
206 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
207
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 * 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
210 * 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
211 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
212 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
213 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
214 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
215 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
216 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
217 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
218 #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
219 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
220 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
221
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
222 // 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
223 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
224 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
225 // 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
226 emsg_silent = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
227 iemsg(_("E341: Internal error: lalloc(0, )"));
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
228 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
229 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
230
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
231 #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
232 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
233 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
234
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
235 // 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
236 // 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
237 for (;;)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
238 {
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
239 // Handle three kind of systems:
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
240 // 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
241 // 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
242 // 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
243 // 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
244 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
245 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
246 #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
247 // 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
248 goto theend;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
249 #else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
250 // 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
251 // 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
252 allocated += size;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
253 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
254 goto theend;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
255 allocated = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
256
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
257 // 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
258 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
259 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
260 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
261 p = NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
262 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
263 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
264 goto theend;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
265 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
266 }
25567
0082503ff2ff patch 8.2.3320: some local functions are not static
Bram Moolenaar <Bram@vim.org>
parents: 25529
diff changeset
267 // 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
268 // 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
269 if (releasing)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
270 break;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
271 releasing = TRUE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
272
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
273 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
274 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
275
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
276 releasing = FALSE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
277 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
278 break;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
279 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
280
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
281 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
282 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
283
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
284 theend:
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
285 #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
286 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
287 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
288 return p;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
289 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
290
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 * 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
293 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
294 #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
295 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
296 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
297 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
298 #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
299 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
300 return NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
301 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
302 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
303 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
304 #endif
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 #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
307 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
308 * 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
309 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
310 void *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
311 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
312 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
313 void *p;
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 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
316 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
317
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
318 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
319
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
320 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
321
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
322 return p;
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 #endif
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 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
327 * 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
328 * 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
329 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
330 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
331 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
332 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
333 if (!did_outofmem_msg)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
334 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
335 // 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
336 emsg_silent = 0;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
337
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
338 // Must come first to avoid coming back here when printing the error
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
339 // message fails, e.g. when setting v:errmsg.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
340 did_outofmem_msg = TRUE;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
341
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
342 semsg(_("E342: Out of memory! (allocating %lu bytes)"), (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
343
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
344 if (starting == NO_SCREEN)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
345 // Not even finished with initializations and already out of
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
346 // memory? Then nothing is going to work, exit.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
347 mch_exit(123);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
348 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
349 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
350
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
351 #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
352
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
353 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
354 * 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
355 * 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
356 * 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
357 * 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
358 * 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
359 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
360 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
361 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
362 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
363 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
364
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
365 // 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
366 // 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
367 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
368 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
369 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
370 // 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
371 block_autocmds();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
372
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
373 // 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
374 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
375 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
376 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
377 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
378 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
379
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
380 # 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
381 // 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
382 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
383 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
384
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
385 # 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
386 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
387 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
388 # 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
389 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
390 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
391 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
392
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
393 // 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
394 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
395
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
396 // 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
397 // 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
398 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
399 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
400 # 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
401 // Clear menus.
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
402 do_cmdline_cmd((char_u *)"aunmenu *");
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
403 # 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
404 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
405 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
406 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
407 // 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
408 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
409 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
410 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
411 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
412 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
413 # 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
414 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
415 # endif
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_PROFILE)
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 *)"profdel *");
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_KEYMAP)
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 *)"set keymap=");
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 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
423
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
424 # ifdef FEAT_TITLE
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
425 free_titles();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
426 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
427 # if defined(FEAT_SEARCHPATH)
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 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
430
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
431 // 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
432 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
433 clear_termcodes();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
434 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
435 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
436 free_homedir();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
437 free_users();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
438 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
439 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
440 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
441 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
442 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
443 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
444 free_tag_stuff();
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();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
564
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
565 // 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
566 free_screenlines();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
567
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
568 # 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
569 sound_free();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
570 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
571 # 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
572 xsmp_close();
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
573 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
574 # 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
575 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
576 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
577 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
578
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
579 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
580 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
581 # 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
582 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
583 # endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
584 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
585 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
586
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
587 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
588 * 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
589 * 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
590 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
591 char_u *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
592 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
593 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
594 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
595
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
596 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
597 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
598 return ret;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
599 }
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 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
602 * 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
603 * 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
604 * 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
605 * 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
606 * 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
607 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
608 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
609 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
610 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
611 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
612 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
613 #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
614 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
615 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
616 free(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 }
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 /************************************************************************
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
621 * 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
622 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
623
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 * 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
626 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
627 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
628 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
629 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
630 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
631 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
632 }
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 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
635 * 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
636 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
637 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
638 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
639 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
640 int i;
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 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
643 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
644 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
645 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
646 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
647
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 * 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
650 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
651 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
652 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
653 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
654 int i;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
655
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
656 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
657 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
658 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
659
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
660 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
661 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
662 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
663 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
664
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
665 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
666 copy = NULL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
667 else
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
668 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
669 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
670 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
671 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
672 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
673 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
674 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
675 }
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 ((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
678 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
679 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
680 return OK;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
681 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
682
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 * 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
685 * 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
686 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
687 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
688 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
689 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
690 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
691 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
692 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
693 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
694
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
695 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
696 ga_init2(garray_T *gap, int itemsize, int growsize)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
697 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
698 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
699 gap->ga_itemsize = itemsize;
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_growsize = growsize;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
701 }
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 * 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
705 * 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
706 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
707 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
708 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
709 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
710 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
711 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
712 return OK;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
713 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
714
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
715 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
716 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
717 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
718 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
719 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
720 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
721
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
722 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
723 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
724
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
725 // 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
726 // 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
727 // 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
728 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
729 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
730
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
731 new_len = gap->ga_itemsize * (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
732 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
733 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
734 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
735 old_len = gap->ga_itemsize * gap->ga_maxlen;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
736 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
737 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
738 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
739 return OK;
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
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
742 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
743 * 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
744 * 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
745 * 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
746 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
747 char_u *
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
748 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
749 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
750 int i;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
751 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
752 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
753 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
754 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
755
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
756 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
757 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
758
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
759 s = alloc(len + 1);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
760 if (s != NULL)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
761 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
762 *s = NUL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
763 p = s;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
764 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
765 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
766 if (p != s)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
767 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
768 STRCPY(p, sep);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
769 p += sep_len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
770 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
771 STRCPY(p, ((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
772 p += STRLEN(p);
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
773 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
774 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
775 return s;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
776 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
777
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 * 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
780 * 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
781 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
782 int
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
783 ga_add_string(garray_T *gap, 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
784 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
785 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
786
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
787 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
788 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
789
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
790 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
791 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
792 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
793 return FAIL;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
794 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
795 ((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
796 return OK;
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
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 * Concatenate a string 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
801 * 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
802 * 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
803 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
804 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
805 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
806 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
807 int len;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
808
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
809 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
810 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
811 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
812 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
813 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
814 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
815 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
816 }
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
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
819 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
820 * 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
821 * 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
822 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
823 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
824 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
825 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
826 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
827 return;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
828 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
829 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
830 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
831 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
832 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
833 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
834
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
835 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
836 * 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
837 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
838 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
839 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
840 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
841 if (ga_grow(gap, 1) == OK)
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 *((char *)gap->ga_data + gap->ga_len) = c;
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
844 ++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
845 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
846 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
847
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
848 #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
849 || defined(PROTO)
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
850 /*
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
851 * 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
852 */
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
853 void
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
854 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
855 {
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
856 // 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
857 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
858 && !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
859 && ((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
860 --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
861 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
862 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
863 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
864 }
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
865 #endif
bb1097899693 patch 8.2.3301: memory allocation functions don't have their own place
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
866