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