-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc2.c
6380 lines (5791 loc) · 150 KB
/
misc2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* misc2.c: Various functions.
*/
#include "vim.h"
static char_u *username = NULL; /* cached result of mch_get_user_name() */
static char_u *ff_expand_buffer = NULL; /* used for expanding filenames */
#if defined(FEAT_VIRTUALEDIT) || defined(PROTO)
static int coladvance2(pos_T *pos, int addspaces, int finetune, colnr_T wcol);
/*
* Return TRUE if in the current mode we need to use virtual.
*/
int
virtual_active(void)
{
/* While an operator is being executed we return "virtual_op", because
* VIsual_active has already been reset, thus we can't check for "block"
* being used. */
if (virtual_op != MAYBE)
return virtual_op;
return (ve_flags == VE_ALL
|| ((ve_flags & VE_BLOCK) && VIsual_active && VIsual_mode == Ctrl_V)
|| ((ve_flags & VE_INSERT) && (State & INSERT)));
}
/*
* Get the screen position of the cursor.
*/
int
getviscol(void)
{
colnr_T x;
getvvcol(curwin, &curwin->w_cursor, &x, NULL, NULL);
return (int)x;
}
/*
* Get the screen position of character col with a coladd in the cursor line.
*/
int
getviscol2(colnr_T col, colnr_T coladd)
{
colnr_T x;
pos_T pos;
pos.lnum = curwin->w_cursor.lnum;
pos.col = col;
pos.coladd = coladd;
getvvcol(curwin, &pos, &x, NULL, NULL);
return (int)x;
}
/*
* Go to column "wcol", and add/insert white space as necessary to get the
* cursor in that column.
* The caller must have saved the cursor line for undo!
*/
int
coladvance_force(colnr_T wcol)
{
int rc = coladvance2(&curwin->w_cursor, TRUE, FALSE, wcol);
if (wcol == MAXCOL)
curwin->w_valid &= ~VALID_VIRTCOL;
else
{
/* Virtcol is valid */
curwin->w_valid |= VALID_VIRTCOL;
curwin->w_virtcol = wcol;
}
return rc;
}
#endif
/*
* Try to advance the Cursor to the specified screen column.
* If virtual editing: fine tune the cursor position.
* Note that all virtual positions off the end of a line should share
* a curwin->w_cursor.col value (n.b. this is equal to STRLEN(line)),
* beginning at coladd 0.
*
* return OK if desired column is reached, FAIL if not
*/
int
coladvance(colnr_T wcol)
{
int rc = getvpos(&curwin->w_cursor, wcol);
if (wcol == MAXCOL || rc == FAIL)
curwin->w_valid &= ~VALID_VIRTCOL;
else if (*ml_get_cursor() != TAB)
{
/* Virtcol is valid when not on a TAB */
curwin->w_valid |= VALID_VIRTCOL;
curwin->w_virtcol = wcol;
}
return rc;
}
/*
* Return in "pos" the position of the cursor advanced to screen column "wcol".
* return OK if desired column is reached, FAIL if not
*/
int
getvpos(pos_T *pos, colnr_T wcol)
{
#ifdef FEAT_VIRTUALEDIT
return coladvance2(pos, FALSE, virtual_active(), wcol);
}
static int
coladvance2(
pos_T *pos,
int addspaces, /* change the text to achieve our goal? */
int finetune, /* change char offset for the exact column */
colnr_T wcol) /* column to move to */
{
#endif
int idx;
char_u *ptr;
char_u *line;
colnr_T col = 0;
int csize = 0;
int one_more;
#ifdef FEAT_LINEBREAK
int head = 0;
#endif
one_more = (State & INSERT)
|| restart_edit != NUL
|| (VIsual_active && *p_sel != 'o')
#ifdef FEAT_VIRTUALEDIT
|| ((ve_flags & VE_ONEMORE) && wcol < MAXCOL)
#endif
;
line = ml_get_buf(curbuf, pos->lnum, FALSE);
if (wcol >= MAXCOL)
{
idx = (int)STRLEN(line) - 1 + one_more;
col = wcol;
#ifdef FEAT_VIRTUALEDIT
if ((addspaces || finetune) && !VIsual_active)
{
curwin->w_curswant = linetabsize(line) + one_more;
if (curwin->w_curswant > 0)
--curwin->w_curswant;
}
#endif
}
else
{
#ifdef FEAT_VIRTUALEDIT
int width = W_WIDTH(curwin) - win_col_off(curwin);
if (finetune
&& curwin->w_p_wrap
# ifdef FEAT_WINDOWS
&& curwin->w_width != 0
# endif
&& wcol >= (colnr_T)width)
{
csize = linetabsize(line);
if (csize > 0)
csize--;
if (wcol / width > (colnr_T)csize / width
&& ((State & INSERT) == 0 || (int)wcol > csize + 1))
{
/* In case of line wrapping don't move the cursor beyond the
* right screen edge. In Insert mode allow going just beyond
* the last character (like what happens when typing and
* reaching the right window edge). */
wcol = (csize / width + 1) * width - 1;
}
}
#endif
ptr = line;
while (col <= wcol && *ptr != NUL)
{
/* Count a tab for what it's worth (if list mode not on) */
#ifdef FEAT_LINEBREAK
csize = win_lbr_chartabsize(curwin, line, ptr, col, &head);
MB_PTR_ADV(ptr);
#else
csize = lbr_chartabsize_adv(line, &ptr, col);
#endif
col += csize;
}
idx = (int)(ptr - line);
/*
* Handle all the special cases. The virtual_active() check
* is needed to ensure that a virtual position off the end of
* a line has the correct indexing. The one_more comparison
* replaces an explicit add of one_more later on.
*/
if (col > wcol || (!virtual_active() && one_more == 0))
{
idx -= 1;
# ifdef FEAT_LINEBREAK
/* Don't count the chars from 'showbreak'. */
csize -= head;
# endif
col -= csize;
}
#ifdef FEAT_VIRTUALEDIT
if (virtual_active()
&& addspaces
&& ((col != wcol && col != wcol + 1) || csize > 1))
{
/* 'virtualedit' is set: The difference between wcol and col is
* filled with spaces. */
if (line[idx] == NUL)
{
/* Append spaces */
int correct = wcol - col;
char_u *newline = alloc(idx + correct + 1);
int t;
if (newline == NULL)
return FAIL;
for (t = 0; t < idx; ++t)
newline[t] = line[t];
for (t = 0; t < correct; ++t)
newline[t + idx] = ' ';
newline[idx + correct] = NUL;
ml_replace(pos->lnum, newline, FALSE);
changed_bytes(pos->lnum, (colnr_T)idx);
idx += correct;
col = wcol;
}
else
{
/* Break a tab */
int linelen = (int)STRLEN(line);
int correct = wcol - col - csize + 1; /* negative!! */
char_u *newline;
int t, s = 0;
int v;
if (-correct > csize)
return FAIL;
newline = alloc(linelen + csize);
if (newline == NULL)
return FAIL;
for (t = 0; t < linelen; t++)
{
if (t != idx)
newline[s++] = line[t];
else
for (v = 0; v < csize; v++)
newline[s++] = ' ';
}
newline[linelen + csize - 1] = NUL;
ml_replace(pos->lnum, newline, FALSE);
changed_bytes(pos->lnum, idx);
idx += (csize - 1 + correct);
col += correct;
}
}
#endif
}
if (idx < 0)
pos->col = 0;
else
pos->col = idx;
#ifdef FEAT_VIRTUALEDIT
pos->coladd = 0;
if (finetune)
{
if (wcol == MAXCOL)
{
/* The width of the last character is used to set coladd. */
if (!one_more)
{
colnr_T scol, ecol;
getvcol(curwin, pos, &scol, NULL, &ecol);
pos->coladd = ecol - scol;
}
}
else
{
int b = (int)wcol - (int)col;
/* The difference between wcol and col is used to set coladd. */
if (b > 0 && b < (MAXCOL - 2 * W_WIDTH(curwin)))
pos->coladd = b;
col += b;
}
}
#endif
#ifdef FEAT_MBYTE
/* prevent from moving onto a trail byte */
if (has_mbyte)
mb_adjustpos(curbuf, pos);
#endif
if (col < wcol)
return FAIL;
return OK;
}
/*
* Increment the cursor position. See inc() for return values.
*/
int
inc_cursor(void)
{
return inc(&curwin->w_cursor);
}
/*
* Increment the line pointer "lp" crossing line boundaries as necessary.
* Return 1 when going to the next line.
* Return 2 when moving forward onto a NUL at the end of the line).
* Return -1 when at the end of file.
* Return 0 otherwise.
*/
int
inc(pos_T *lp)
{
char_u *p = ml_get_pos(lp);
if (*p != NUL) /* still within line, move to next char (may be NUL) */
{
#ifdef FEAT_MBYTE
if (has_mbyte)
{
int l = (*mb_ptr2len)(p);
lp->col += l;
return ((p[l] != NUL) ? 0 : 2);
}
#endif
lp->col++;
#ifdef FEAT_VIRTUALEDIT
lp->coladd = 0;
#endif
return ((p[1] != NUL) ? 0 : 2);
}
if (lp->lnum != curbuf->b_ml.ml_line_count) /* there is a next line */
{
lp->col = 0;
lp->lnum++;
#ifdef FEAT_VIRTUALEDIT
lp->coladd = 0;
#endif
return 1;
}
return -1;
}
/*
* incl(lp): same as inc(), but skip the NUL at the end of non-empty lines
*/
int
incl(pos_T *lp)
{
int r;
if ((r = inc(lp)) >= 1 && lp->col)
r = inc(lp);
return r;
}
/*
* dec(p)
*
* Decrement the line pointer 'p' crossing line boundaries as necessary.
* Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
*/
int
dec_cursor(void)
{
return dec(&curwin->w_cursor);
}
int
dec(pos_T *lp)
{
char_u *p;
#ifdef FEAT_VIRTUALEDIT
lp->coladd = 0;
#endif
if (lp->col > 0) /* still within line */
{
lp->col--;
#ifdef FEAT_MBYTE
if (has_mbyte)
{
p = ml_get(lp->lnum);
lp->col -= (*mb_head_off)(p, p + lp->col);
}
#endif
return 0;
}
if (lp->lnum > 1) /* there is a prior line */
{
lp->lnum--;
p = ml_get(lp->lnum);
lp->col = (colnr_T)STRLEN(p);
#ifdef FEAT_MBYTE
if (has_mbyte)
lp->col -= (*mb_head_off)(p, p + lp->col);
#endif
return 1;
}
return -1; /* at start of file */
}
/*
* decl(lp): same as dec(), but skip the NUL at the end of non-empty lines
*/
int
decl(pos_T *lp)
{
int r;
if ((r = dec(lp)) == 1 && lp->col)
r = dec(lp);
return r;
}
/*
* Get the line number relative to the current cursor position, i.e. the
* difference between line number and cursor position. Only look for lines that
* can be visible, folded lines don't count.
*/
linenr_T
get_cursor_rel_lnum(
win_T *wp,
linenr_T lnum) /* line number to get the result for */
{
linenr_T cursor = wp->w_cursor.lnum;
linenr_T retval = 0;
#ifdef FEAT_FOLDING
if (hasAnyFolding(wp))
{
if (lnum > cursor)
{
while (lnum > cursor)
{
(void)hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
/* if lnum and cursor are in the same fold,
* now lnum <= cursor */
if (lnum > cursor)
retval++;
lnum--;
}
}
else if (lnum < cursor)
{
while (lnum < cursor)
{
(void)hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
/* if lnum and cursor are in the same fold,
* now lnum >= cursor */
if (lnum < cursor)
retval--;
lnum++;
}
}
/* else if (lnum == cursor)
* retval = 0;
*/
}
else
#endif
retval = lnum - cursor;
return retval;
}
/*
* Make sure "pos.lnum" and "pos.col" are valid in "buf".
* This allows for the col to be on the NUL byte.
*/
void
check_pos(buf_T *buf, pos_T *pos)
{
char_u *line;
colnr_T len;
if (pos->lnum > buf->b_ml.ml_line_count)
pos->lnum = buf->b_ml.ml_line_count;
if (pos->col > 0)
{
line = ml_get_buf(buf, pos->lnum, FALSE);
len = (colnr_T)STRLEN(line);
if (pos->col > len)
pos->col = len;
}
}
/*
* Make sure curwin->w_cursor.lnum is valid.
*/
void
check_cursor_lnum(void)
{
if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
{
#ifdef FEAT_FOLDING
/* If there is a closed fold at the end of the file, put the cursor in
* its first line. Otherwise in the last line. */
if (!hasFolding(curbuf->b_ml.ml_line_count,
&curwin->w_cursor.lnum, NULL))
#endif
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
}
if (curwin->w_cursor.lnum <= 0)
curwin->w_cursor.lnum = 1;
}
/*
* Make sure curwin->w_cursor.col is valid.
*/
void
check_cursor_col(void)
{
check_cursor_col_win(curwin);
}
/*
* Make sure win->w_cursor.col is valid.
*/
void
check_cursor_col_win(win_T *win)
{
colnr_T len;
#ifdef FEAT_VIRTUALEDIT
colnr_T oldcol = win->w_cursor.col;
colnr_T oldcoladd = win->w_cursor.col + win->w_cursor.coladd;
#endif
len = (colnr_T)STRLEN(ml_get_buf(win->w_buffer, win->w_cursor.lnum, FALSE));
if (len == 0)
win->w_cursor.col = 0;
else if (win->w_cursor.col >= len)
{
/* Allow cursor past end-of-line when:
* - in Insert mode or restarting Insert mode
* - in Visual mode and 'selection' isn't "old"
* - 'virtualedit' is set */
if ((State & INSERT) || restart_edit
|| (VIsual_active && *p_sel != 'o')
#ifdef FEAT_VIRTUALEDIT
|| (ve_flags & VE_ONEMORE)
#endif
|| virtual_active())
win->w_cursor.col = len;
else
{
win->w_cursor.col = len - 1;
#ifdef FEAT_MBYTE
/* Move the cursor to the head byte. */
if (has_mbyte)
mb_adjustpos(win->w_buffer, &win->w_cursor);
#endif
}
}
else if (win->w_cursor.col < 0)
win->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
/* If virtual editing is on, we can leave the cursor on the old position,
* only we must set it to virtual. But don't do it when at the end of the
* line. */
if (oldcol == MAXCOL)
win->w_cursor.coladd = 0;
else if (ve_flags == VE_ALL)
{
if (oldcoladd > win->w_cursor.col)
{
win->w_cursor.coladd = oldcoladd - win->w_cursor.col;
/* Make sure that coladd is not more than the char width.
* Not for the last character, coladd is then used when the cursor
* is actually after the last character. */
if (win->w_cursor.col + 1 < len && win->w_cursor.coladd > 0)
{
int cs, ce;
getvcol(win, &win->w_cursor, &cs, NULL, &ce);
if (win->w_cursor.coladd > ce - cs)
win->w_cursor.coladd = ce - cs;
}
}
else
/* avoid weird number when there is a miscalculation or overflow */
win->w_cursor.coladd = 0;
}
#endif
}
/*
* make sure curwin->w_cursor in on a valid character
*/
void
check_cursor(void)
{
check_cursor_lnum();
check_cursor_col();
}
#if defined(FEAT_TEXTOBJ) || defined(PROTO)
/*
* Make sure curwin->w_cursor is not on the NUL at the end of the line.
* Allow it when in Visual mode and 'selection' is not "old".
*/
void
adjust_cursor_col(void)
{
if (curwin->w_cursor.col > 0
&& (!VIsual_active || *p_sel == 'o')
&& gchar_cursor() == NUL)
--curwin->w_cursor.col;
}
#endif
/*
* When curwin->w_leftcol has changed, adjust the cursor position.
* Return TRUE if the cursor was moved.
*/
int
leftcol_changed(void)
{
long lastcol;
colnr_T s, e;
int retval = FALSE;
changed_cline_bef_curs();
lastcol = curwin->w_leftcol + W_WIDTH(curwin) - curwin_col_off() - 1;
validate_virtcol();
/*
* If the cursor is right or left of the screen, move it to last or first
* character.
*/
if (curwin->w_virtcol > (colnr_T)(lastcol - p_siso))
{
retval = TRUE;
coladvance((colnr_T)(lastcol - p_siso));
}
else if (curwin->w_virtcol < curwin->w_leftcol + p_siso)
{
retval = TRUE;
(void)coladvance((colnr_T)(curwin->w_leftcol + p_siso));
}
/*
* If the start of the character under the cursor is not on the screen,
* advance the cursor one more char. If this fails (last char of the
* line) adjust the scrolling.
*/
getvvcol(curwin, &curwin->w_cursor, &s, NULL, &e);
if (e > (colnr_T)lastcol)
{
retval = TRUE;
coladvance(s - 1);
}
else if (s < curwin->w_leftcol)
{
retval = TRUE;
if (coladvance(e + 1) == FAIL) /* there isn't another character */
{
curwin->w_leftcol = s; /* adjust w_leftcol instead */
changed_cline_bef_curs();
}
}
if (retval)
curwin->w_set_curswant = TRUE;
redraw_later(NOT_VALID);
return retval;
}
/**********************************************************************
* Various routines dealing with allocation and deallocation of memory.
*/
#if defined(MEM_PROFILE) || defined(PROTO)
# define MEM_SIZES 8200
static long_u mem_allocs[MEM_SIZES];
static long_u mem_frees[MEM_SIZES];
static long_u mem_allocated;
static long_u mem_freed;
static long_u mem_peak;
static long_u num_alloc;
static long_u num_freed;
static void mem_pre_alloc_s(size_t *sizep);
static void mem_pre_alloc_l(long_u *sizep);
static void mem_post_alloc(void **pp, size_t size);
static void mem_pre_free(void **pp);
static void
mem_pre_alloc_s(size_t *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_pre_alloc_l(long_u *sizep)
{
*sizep += sizeof(size_t);
}
static void
mem_post_alloc(
void **pp,
size_t size)
{
if (*pp == NULL)
return;
size -= sizeof(size_t);
*(long_u *)*pp = size;
if (size <= MEM_SIZES-1)
mem_allocs[size-1]++;
else
mem_allocs[MEM_SIZES-1]++;
mem_allocated += size;
if (mem_allocated - mem_freed > mem_peak)
mem_peak = mem_allocated - mem_freed;
num_alloc++;
*pp = (void *)((char *)*pp + sizeof(size_t));
}
static void
mem_pre_free(void **pp)
{
long_u size;
*pp = (void *)((char *)*pp - sizeof(size_t));
size = *(size_t *)*pp;
if (size <= MEM_SIZES-1)
mem_frees[size-1]++;
else
mem_frees[MEM_SIZES-1]++;
mem_freed += size;
num_freed++;
}
/*
* called on exit via atexit()
*/
void
vim_mem_profile_dump(void)
{
int i, j;
printf("\r\n");
j = 0;
for (i = 0; i < MEM_SIZES - 1; i++)
{
if (mem_allocs[i] || mem_frees[i])
{
if (mem_frees[i] > mem_allocs[i])
printf("\r\n%s", _("ERROR: "));
printf("[%4d / %4lu-%-4lu] ", i + 1, mem_allocs[i], mem_frees[i]);
j++;
if (j > 3)
{
j = 0;
printf("\r\n");
}
}
}
i = MEM_SIZES - 1;
if (mem_allocs[i])
{
printf("\r\n");
if (mem_frees[i] > mem_allocs[i])
puts(_("ERROR: "));
printf("[>%d / %4lu-%-4lu]", i, mem_allocs[i], mem_frees[i]);
}
printf(_("\n[bytes] total alloc-freed %lu-%lu, in use %lu, peak use %lu\n"),
mem_allocated, mem_freed, mem_allocated - mem_freed, mem_peak);
printf(_("[calls] total re/malloc()'s %lu, total free()'s %lu\n\n"),
num_alloc, num_freed);
}
#endif /* MEM_PROFILE */
#ifdef FEAT_EVAL
static int alloc_does_fail(long_u size);
static int
alloc_does_fail(long_u size)
{
if (alloc_fail_countdown == 0)
{
if (--alloc_fail_repeat <= 0)
alloc_fail_id = 0;
do_outofmem_msg(size);
return TRUE;
}
--alloc_fail_countdown;
return FALSE;
}
#endif
/*
* Some memory is reserved for error messages and for being able to
* call mf_release_all(), which needs some memory for mf_trans_add().
*/
#define KEEP_ROOM (2 * 8192L)
#define KEEP_ROOM_KB (KEEP_ROOM / 1024L)
/*
* Note: if unsigned is 16 bits we can only allocate up to 64K with alloc().
* Use lalloc for larger blocks.
*/
char_u *
alloc(unsigned size)
{
return (lalloc((long_u)size, TRUE));
}
/*
* alloc() with an ID for alloc_fail().
*/
char_u *
alloc_id(unsigned size, alloc_id_T id UNUSED)
{
#ifdef FEAT_EVAL
if (alloc_fail_id == id && alloc_does_fail((long_u)size))
return NULL;
#endif
return (lalloc((long_u)size, TRUE));
}
/*
* Allocate memory and set all bytes to zero.
*/
char_u *
alloc_clear(unsigned size)
{
char_u *p;
p = lalloc((long_u)size, TRUE);
if (p != NULL)
(void)vim_memset(p, 0, (size_t)size);
return p;
}
/*
* alloc() with check for maximum line length
*/
char_u *
alloc_check(unsigned size)
{
#if !defined(UNIX)
if (sizeof(int) == 2 && size > 0x7fff)
{
/* Don't hide this message */
emsg_silent = 0;
EMSG(_("E340: Line is becoming too long"));
return NULL;
}
#endif
return (lalloc((long_u)size, TRUE));
}
/*
* Allocate memory like lalloc() and set all bytes to zero.
*/
char_u *
lalloc_clear(long_u size, int message)
{
char_u *p;
p = (lalloc(size, message));
if (p != NULL)
(void)vim_memset(p, 0, (size_t)size);
return p;
}
/*
* Low level memory allocation function.
* This is used often, KEEP IT FAST!
*/
char_u *
lalloc(long_u size, int message)
{
char_u *p; /* pointer to new storage space */
static int releasing = FALSE; /* don't do mf_release_all() recursive */
int try_again;
#if defined(HAVE_AVAIL_MEM)
static long_u allocated = 0; /* allocated since last avail check */
#endif
/* Safety check for allocating zero bytes */
if (size == 0)
{
/* Don't hide this message */
emsg_silent = 0;
IEMSGN(_("E341: Internal error: lalloc(%ld, )"), size);
return NULL;
}
#ifdef MEM_PROFILE
mem_pre_alloc_l(&size);
#endif
/*
* Loop when out of memory: Try to release some memfile blocks and
* if some blocks are released call malloc again.
*/
for (;;)
{
/*
* Handle three kind of systems:
* 1. No check for available memory: Just return.
* 2. Slow check for available memory: call mch_avail_mem() after
* allocating KEEP_ROOM amount of memory.
* 3. Strict check for available memory: call mch_avail_mem()
*/
if ((p = (char_u *)malloc((size_t)size)) != NULL)
{
#ifndef HAVE_AVAIL_MEM
/* 1. No check for available memory: Just return. */
goto theend;
#else
/* 2. Slow check for available memory: call mch_avail_mem() after
* allocating (KEEP_ROOM / 2) amount of memory. */
allocated += size;
if (allocated < KEEP_ROOM / 2)
goto theend;
allocated = 0;
/* 3. check for available memory: call mch_avail_mem() */
if (mch_avail_mem(TRUE) < KEEP_ROOM_KB && !releasing)
{
free((char *)p); /* System is low... no go! */
p = NULL;
}
else
goto theend;
#endif
}
/*
* Remember that mf_release_all() is being called to avoid an endless
* loop, because mf_release_all() may call alloc() recursively.
*/
if (releasing)
break;
releasing = TRUE;
clear_sb_text(TRUE); /* free any scrollback text */
try_again = mf_release_all(); /* release as many blocks as possible */
releasing = FALSE;
if (!try_again)
break;
}
if (message && p == NULL)
do_outofmem_msg(size);
theend:
#ifdef MEM_PROFILE
mem_post_alloc((void **)&p, (size_t)size);