Mercurial > emacs
comparison src/callproc.c @ 62982:d7793ac6df62
(Fcall_process): Don't use alloca to gradually
increase size of buf, as it effectively uses twice the necessary
space on the stack. Instead, pre-allocate buf of full size, and
gradually increase the read size.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Fri, 03 Jun 2005 23:02:40 +0000 |
parents | 8881d5e2d23f |
children | a8fa7c632ee4 01137c1fdbe9 |
comparison
equal
deleted
inserted
replaced
62981:79e0dd73c671 | 62982:d7793ac6df62 |
---|---|
216 Lisp_Object infile, buffer, current_dir, path; | 216 Lisp_Object infile, buffer, current_dir, path; |
217 int display_p; | 217 int display_p; |
218 int fd[2]; | 218 int fd[2]; |
219 int filefd; | 219 int filefd; |
220 register int pid; | 220 register int pid; |
221 char buf[16384]; | 221 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024) |
222 char *bufptr = buf; | 222 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN) |
223 int bufsize = sizeof buf; | 223 char buf[CALLPROC_BUFFER_SIZE_MAX]; |
224 int bufsize = CALLPROC_BUFFER_SIZE_MIN; | |
224 int count = SPECPDL_INDEX (); | 225 int count = SPECPDL_INDEX (); |
225 | 226 |
226 register const unsigned char **new_argv | 227 register const unsigned char **new_argv |
227 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); | 228 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); |
228 struct buffer *old = current_buffer; | 229 struct buffer *old = current_buffer; |
763 of the buffer size we have. But don't read | 764 of the buffer size we have. But don't read |
764 less than 1024--save that for the next bufferful. */ | 765 less than 1024--save that for the next bufferful. */ |
765 nread = carryover; | 766 nread = carryover; |
766 while (nread < bufsize - 1024) | 767 while (nread < bufsize - 1024) |
767 { | 768 { |
768 int this_read = emacs_read (fd[0], bufptr + nread, | 769 int this_read = emacs_read (fd[0], buf + nread, |
769 bufsize - nread); | 770 bufsize - nread); |
770 | 771 |
771 if (this_read < 0) | 772 if (this_read < 0) |
772 goto give_up; | 773 goto give_up; |
773 | 774 |
788 immediate_quit = 0; | 789 immediate_quit = 0; |
789 | 790 |
790 if (!NILP (buffer)) | 791 if (!NILP (buffer)) |
791 { | 792 { |
792 if (! CODING_MAY_REQUIRE_DECODING (&process_coding)) | 793 if (! CODING_MAY_REQUIRE_DECODING (&process_coding)) |
793 insert_1_both (bufptr, nread, nread, 0, 1, 0); | 794 insert_1_both (buf, nread, nread, 0, 1, 0); |
794 else | 795 else |
795 { /* We have to decode the input. */ | 796 { /* We have to decode the input. */ |
796 int size; | 797 int size; |
797 char *decoding_buf; | 798 char *decoding_buf; |
798 | 799 |
805 system requires EOL detection. Here, we have to | 806 system requires EOL detection. Here, we have to |
806 check only whether or not the coding system | 807 check only whether or not the coding system |
807 requires text-encoding detection. */ | 808 requires text-encoding detection. */ |
808 if (process_coding.type == coding_type_undecided) | 809 if (process_coding.type == coding_type_undecided) |
809 { | 810 { |
810 detect_coding (&process_coding, bufptr, nread); | 811 detect_coding (&process_coding, buf, nread); |
811 if (process_coding.composing != COMPOSITION_DISABLED) | 812 if (process_coding.composing != COMPOSITION_DISABLED) |
812 /* We have not yet allocated the composition | 813 /* We have not yet allocated the composition |
813 data because the coding type was undecided. */ | 814 data because the coding type was undecided. */ |
814 coding_allocate_composition_data (&process_coding, PT); | 815 coding_allocate_composition_data (&process_coding, PT); |
815 } | 816 } |
816 if (process_coding.cmp_data) | 817 if (process_coding.cmp_data) |
817 process_coding.cmp_data->char_offset = PT; | 818 process_coding.cmp_data->char_offset = PT; |
818 | 819 |
819 decode_coding (&process_coding, bufptr, decoding_buf, | 820 decode_coding (&process_coding, buf, decoding_buf, |
820 nread, size); | 821 nread, size); |
821 | 822 |
822 if (display_on_the_fly | 823 if (display_on_the_fly |
823 && saved_coding.type == coding_type_undecided | 824 && saved_coding.type == coding_type_undecided |
824 && process_coding.type != coding_type_undecided) | 825 && process_coding.type != coding_type_undecided) |
903 nread -= process_coding.consumed; | 904 nread -= process_coding.consumed; |
904 carryover = nread; | 905 carryover = nread; |
905 if (carryover > 0) | 906 if (carryover > 0) |
906 /* As CARRYOVER should not be that large, we had | 907 /* As CARRYOVER should not be that large, we had |
907 better avoid overhead of bcopy. */ | 908 better avoid overhead of bcopy. */ |
908 BCOPY_SHORT (bufptr + process_coding.consumed, bufptr, | 909 BCOPY_SHORT (buf + process_coding.consumed, buf, |
909 carryover); | 910 carryover); |
910 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP) | 911 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP) |
911 { | 912 { |
912 /* The decoding ended because of insufficient data | 913 /* The decoding ended because of insufficient data |
913 area to record information about composition. | 914 area to record information about composition. |
920 } | 921 } |
921 | 922 |
922 if (process_coding.mode & CODING_MODE_LAST_BLOCK) | 923 if (process_coding.mode & CODING_MODE_LAST_BLOCK) |
923 break; | 924 break; |
924 | 925 |
926 #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX) | |
925 /* Make the buffer bigger as we continue to read more data, | 927 /* Make the buffer bigger as we continue to read more data, |
926 but not past 64k. */ | 928 but not past CALLPROC_BUFFER_SIZE_MAX. */ |
927 if (bufsize < 64 * 1024 && total_read > 32 * bufsize) | 929 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize) |
928 { | 930 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX) |
929 char *tempptr; | 931 bufsize = CALLPROC_BUFFER_SIZE_MAX; |
930 bufsize *= 2; | 932 #endif |
931 | |
932 tempptr = (char *) alloca (bufsize); | |
933 bcopy (bufptr, tempptr, bufsize / 2); | |
934 bufptr = tempptr; | |
935 } | |
936 | 933 |
937 if (display_p) | 934 if (display_p) |
938 { | 935 { |
939 if (first) | 936 if (first) |
940 prepare_menu_bars (); | 937 prepare_menu_bars (); |