annotate src/alloc.c @ 32100:fcc497515cce v9.0.1381

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