Mercurial > emacs
comparison src/editfns.c @ 56194:e1f0d4beb4ba
(Fformat, Ftranspose_regions): Use SAFE_ALLOCA.
author | Kim F. Storm <storm@cua.dk> |
---|---|
date | Mon, 21 Jun 2004 21:54:30 +0000 |
parents | db2480202623 |
children | 4f00fbfe3c1e 59dcbfe97385 |
comparison
equal
deleted
inserted
replaced
56193:3310f9505840 | 56194:e1f0d4beb4ba |
---|---|
3223 occur after the final format specifier. */ | 3223 occur after the final format specifier. */ |
3224 int *precision = (int *) (alloca((nargs + 1) * sizeof (int))); | 3224 int *precision = (int *) (alloca((nargs + 1) * sizeof (int))); |
3225 int longest_format; | 3225 int longest_format; |
3226 Lisp_Object val; | 3226 Lisp_Object val; |
3227 int arg_intervals = 0; | 3227 int arg_intervals = 0; |
3228 USE_SAFE_ALLOCA; | |
3228 | 3229 |
3229 /* discarded[I] is 1 if byte I of the format | 3230 /* discarded[I] is 1 if byte I of the format |
3230 string was not copied into the output. | 3231 string was not copied into the output. |
3231 It is 2 if byte I was not the first byte of its character. */ | 3232 It is 2 if byte I was not the first byte of its character. */ |
3232 char *discarded; | 3233 char *discarded; |
3271 format_start = format; | 3272 format_start = format; |
3272 end = format + SBYTES (args[0]); | 3273 end = format + SBYTES (args[0]); |
3273 longest_format = 0; | 3274 longest_format = 0; |
3274 | 3275 |
3275 /* Make room in result for all the non-%-codes in the control string. */ | 3276 /* Make room in result for all the non-%-codes in the control string. */ |
3276 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]); | 3277 total = 5 + CONVERTED_BYTE_SIZE (multibyte, args[0]) + 1; |
3277 | 3278 |
3278 /* Allocate the info and discarded tables. */ | 3279 /* Allocate the info and discarded tables. */ |
3279 { | 3280 { |
3280 int nbytes = nargs * sizeof *info; | 3281 int nbytes = nargs * sizeof *info; |
3281 int i; | 3282 int i; |
3464 | 3465 |
3465 this_format = (unsigned char *) alloca (longest_format + 1); | 3466 this_format = (unsigned char *) alloca (longest_format + 1); |
3466 | 3467 |
3467 /* Allocate the space for the result. | 3468 /* Allocate the space for the result. |
3468 Note that TOTAL is an overestimate. */ | 3469 Note that TOTAL is an overestimate. */ |
3469 if (total < 1000) | 3470 SAFE_ALLOCA (buf, char *, total); |
3470 buf = (char *) alloca (total + 1); | |
3471 else | |
3472 buf = (char *) xmalloc (total + 1); | |
3473 | 3471 |
3474 p = buf; | 3472 p = buf; |
3475 nchars = 0; | 3473 nchars = 0; |
3476 n = 0; | 3474 n = 0; |
3477 | 3475 |
3600 && !ASCII_BYTE_P (*((unsigned char *) p - 1)) | 3598 && !ASCII_BYTE_P (*((unsigned char *) p - 1)) |
3601 && !CHAR_HEAD_P (*((unsigned char *) p))) | 3599 && !CHAR_HEAD_P (*((unsigned char *) p))) |
3602 maybe_combine_byte = 1; | 3600 maybe_combine_byte = 1; |
3603 this_nchars = strlen (p); | 3601 this_nchars = strlen (p); |
3604 if (multibyte) | 3602 if (multibyte) |
3605 p += str_to_multibyte (p, buf + total - p, this_nchars); | 3603 p += str_to_multibyte (p, buf + total - 1 - p, this_nchars); |
3606 else | 3604 else |
3607 p += this_nchars; | 3605 p += this_nchars; |
3608 nchars += this_nchars; | 3606 nchars += this_nchars; |
3609 } | 3607 } |
3610 | 3608 |
3637 } | 3635 } |
3638 else | 3636 else |
3639 *p++ = *format++, nchars++; | 3637 *p++ = *format++, nchars++; |
3640 } | 3638 } |
3641 | 3639 |
3642 if (p > buf + total + 1) | 3640 if (p > buf + total) |
3643 abort (); | 3641 abort (); |
3644 | 3642 |
3645 if (maybe_combine_byte) | 3643 if (maybe_combine_byte) |
3646 nchars = multibyte_chars_in_text (buf, p - buf); | 3644 nchars = multibyte_chars_in_text (buf, p - buf); |
3647 val = make_specified_string (buf, nchars, p - buf, multibyte); | 3645 val = make_specified_string (buf, nchars, p - buf, multibyte); |
3648 | 3646 |
3649 /* If we allocated BUF with malloc, free it too. */ | 3647 /* If we allocated BUF with malloc, free it too. */ |
3650 if (total >= 1000) | 3648 SAFE_FREE (total); |
3651 xfree (buf); | |
3652 | 3649 |
3653 /* If the format string has text properties, or any of the string | 3650 /* If the format string has text properties, or any of the string |
3654 arguments has text properties, set up text properties of the | 3651 arguments has text properties, set up text properties of the |
3655 result string. */ | 3652 result string. */ |
3656 | 3653 |
4003 Qnil, Qnil); | 4000 Qnil, Qnil); |
4004 | 4001 |
4005 /* First region smaller than second. */ | 4002 /* First region smaller than second. */ |
4006 if (len1_byte < len2_byte) | 4003 if (len1_byte < len2_byte) |
4007 { | 4004 { |
4008 /* We use alloca only if it is small, | 4005 USE_SAFE_ALLOCA; |
4009 because we want to avoid stack overflow. */ | 4006 |
4010 if (len2_byte > 20000) | 4007 SAFE_ALLOCA (temp, unsigned char *, len2_byte); |
4011 temp = (unsigned char *) xmalloc (len2_byte); | |
4012 else | |
4013 temp = (unsigned char *) alloca (len2_byte); | |
4014 | 4008 |
4015 /* Don't precompute these addresses. We have to compute them | 4009 /* Don't precompute these addresses. We have to compute them |
4016 at the last minute, because the relocating allocator might | 4010 at the last minute, because the relocating allocator might |
4017 have moved the buffer around during the xmalloc. */ | 4011 have moved the buffer around during the xmalloc. */ |
4018 start1_addr = BYTE_POS_ADDR (start1_byte); | 4012 start1_addr = BYTE_POS_ADDR (start1_byte); |
4019 start2_addr = BYTE_POS_ADDR (start2_byte); | 4013 start2_addr = BYTE_POS_ADDR (start2_byte); |
4020 | 4014 |
4021 bcopy (start2_addr, temp, len2_byte); | 4015 bcopy (start2_addr, temp, len2_byte); |
4022 bcopy (start1_addr, start1_addr + len2_byte, len1_byte); | 4016 bcopy (start1_addr, start1_addr + len2_byte, len1_byte); |
4023 bcopy (temp, start1_addr, len2_byte); | 4017 bcopy (temp, start1_addr, len2_byte); |
4024 if (len2_byte > 20000) | 4018 SAFE_FREE (len2_byte); |
4025 xfree (temp); | |
4026 } | 4019 } |
4027 else | 4020 else |
4028 /* First region not smaller than second. */ | 4021 /* First region not smaller than second. */ |
4029 { | 4022 { |
4030 if (len1_byte > 20000) | 4023 USE_SAFE_ALLOCA; |
4031 temp = (unsigned char *) xmalloc (len1_byte); | 4024 |
4032 else | 4025 SAFE_ALLOCA (temp, unsigned char *, len1_byte); |
4033 temp = (unsigned char *) alloca (len1_byte); | |
4034 start1_addr = BYTE_POS_ADDR (start1_byte); | 4026 start1_addr = BYTE_POS_ADDR (start1_byte); |
4035 start2_addr = BYTE_POS_ADDR (start2_byte); | 4027 start2_addr = BYTE_POS_ADDR (start2_byte); |
4036 bcopy (start1_addr, temp, len1_byte); | 4028 bcopy (start1_addr, temp, len1_byte); |
4037 bcopy (start2_addr, start1_addr, len2_byte); | 4029 bcopy (start2_addr, start1_addr, len2_byte); |
4038 bcopy (temp, start1_addr + len2_byte, len1_byte); | 4030 bcopy (temp, start1_addr + len2_byte, len1_byte); |
4039 if (len1_byte > 20000) | 4031 SAFE_FREE (len1_byte); |
4040 xfree (temp); | |
4041 } | 4032 } |
4042 graft_intervals_into_buffer (tmp_interval1, start1 + len2, | 4033 graft_intervals_into_buffer (tmp_interval1, start1 + len2, |
4043 len1, current_buffer, 0); | 4034 len1, current_buffer, 0); |
4044 graft_intervals_into_buffer (tmp_interval2, start1, | 4035 graft_intervals_into_buffer (tmp_interval2, start1, |
4045 len2, current_buffer, 0); | 4036 len2, current_buffer, 0); |
4052 len_mid = start2_byte - (start1_byte + len1_byte); | 4043 len_mid = start2_byte - (start1_byte + len1_byte); |
4053 | 4044 |
4054 if (len1_byte == len2_byte) | 4045 if (len1_byte == len2_byte) |
4055 /* Regions are same size, though, how nice. */ | 4046 /* Regions are same size, though, how nice. */ |
4056 { | 4047 { |
4048 USE_SAFE_ALLOCA; | |
4049 | |
4057 modify_region (current_buffer, start1, end1); | 4050 modify_region (current_buffer, start1, end1); |
4058 modify_region (current_buffer, start2, end2); | 4051 modify_region (current_buffer, start2, end2); |
4059 record_change (start1, len1); | 4052 record_change (start1, len1); |
4060 record_change (start2, len2); | 4053 record_change (start2, len2); |
4061 tmp_interval1 = copy_intervals (cur_intv, start1, len1); | 4054 tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4063 Fset_text_properties (make_number (start1), make_number (end1), | 4056 Fset_text_properties (make_number (start1), make_number (end1), |
4064 Qnil, Qnil); | 4057 Qnil, Qnil); |
4065 Fset_text_properties (make_number (start2), make_number (end2), | 4058 Fset_text_properties (make_number (start2), make_number (end2), |
4066 Qnil, Qnil); | 4059 Qnil, Qnil); |
4067 | 4060 |
4068 if (len1_byte > 20000) | 4061 SAFE_ALLOCA (temp, unsigned char *, len1_byte); |
4069 temp = (unsigned char *) xmalloc (len1_byte); | |
4070 else | |
4071 temp = (unsigned char *) alloca (len1_byte); | |
4072 start1_addr = BYTE_POS_ADDR (start1_byte); | 4062 start1_addr = BYTE_POS_ADDR (start1_byte); |
4073 start2_addr = BYTE_POS_ADDR (start2_byte); | 4063 start2_addr = BYTE_POS_ADDR (start2_byte); |
4074 bcopy (start1_addr, temp, len1_byte); | 4064 bcopy (start1_addr, temp, len1_byte); |
4075 bcopy (start2_addr, start1_addr, len2_byte); | 4065 bcopy (start2_addr, start1_addr, len2_byte); |
4076 bcopy (temp, start2_addr, len1_byte); | 4066 bcopy (temp, start2_addr, len1_byte); |
4077 if (len1_byte > 20000) | 4067 SAFE_FREE (len1_byte); |
4078 xfree (temp); | 4068 |
4079 graft_intervals_into_buffer (tmp_interval1, start2, | 4069 graft_intervals_into_buffer (tmp_interval1, start2, |
4080 len1, current_buffer, 0); | 4070 len1, current_buffer, 0); |
4081 graft_intervals_into_buffer (tmp_interval2, start1, | 4071 graft_intervals_into_buffer (tmp_interval2, start1, |
4082 len2, current_buffer, 0); | 4072 len2, current_buffer, 0); |
4083 } | 4073 } |
4084 | 4074 |
4085 else if (len1_byte < len2_byte) /* Second region larger than first */ | 4075 else if (len1_byte < len2_byte) /* Second region larger than first */ |
4086 /* Non-adjacent & unequal size, area between must also be shifted. */ | 4076 /* Non-adjacent & unequal size, area between must also be shifted. */ |
4087 { | 4077 { |
4078 USE_SAFE_ALLOCA; | |
4079 | |
4088 modify_region (current_buffer, start1, end2); | 4080 modify_region (current_buffer, start1, end2); |
4089 record_change (start1, (end2 - start1)); | 4081 record_change (start1, (end2 - start1)); |
4090 tmp_interval1 = copy_intervals (cur_intv, start1, len1); | 4082 tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4091 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | 4083 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); |
4092 tmp_interval2 = copy_intervals (cur_intv, start2, len2); | 4084 tmp_interval2 = copy_intervals (cur_intv, start2, len2); |
4093 Fset_text_properties (make_number (start1), make_number (end2), | 4085 Fset_text_properties (make_number (start1), make_number (end2), |
4094 Qnil, Qnil); | 4086 Qnil, Qnil); |
4095 | 4087 |
4096 /* holds region 2 */ | 4088 /* holds region 2 */ |
4097 if (len2_byte > 20000) | 4089 SAFE_ALLOCA (temp, unsigned char *, len2_byte); |
4098 temp = (unsigned char *) xmalloc (len2_byte); | |
4099 else | |
4100 temp = (unsigned char *) alloca (len2_byte); | |
4101 start1_addr = BYTE_POS_ADDR (start1_byte); | 4090 start1_addr = BYTE_POS_ADDR (start1_byte); |
4102 start2_addr = BYTE_POS_ADDR (start2_byte); | 4091 start2_addr = BYTE_POS_ADDR (start2_byte); |
4103 bcopy (start2_addr, temp, len2_byte); | 4092 bcopy (start2_addr, temp, len2_byte); |
4104 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte); | 4093 bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte); |
4105 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); | 4094 safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); |
4106 bcopy (temp, start1_addr, len2_byte); | 4095 bcopy (temp, start1_addr, len2_byte); |
4107 if (len2_byte > 20000) | 4096 SAFE_FREE (len2_byte); |
4108 xfree (temp); | 4097 |
4109 graft_intervals_into_buffer (tmp_interval1, end2 - len1, | 4098 graft_intervals_into_buffer (tmp_interval1, end2 - len1, |
4110 len1, current_buffer, 0); | 4099 len1, current_buffer, 0); |
4111 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | 4100 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, |
4112 len_mid, current_buffer, 0); | 4101 len_mid, current_buffer, 0); |
4113 graft_intervals_into_buffer (tmp_interval2, start1, | 4102 graft_intervals_into_buffer (tmp_interval2, start1, |
4114 len2, current_buffer, 0); | 4103 len2, current_buffer, 0); |
4115 } | 4104 } |
4116 else | 4105 else |
4117 /* Second region smaller than first. */ | 4106 /* Second region smaller than first. */ |
4118 { | 4107 { |
4108 USE_SAFE_ALLOCA; | |
4109 | |
4119 record_change (start1, (end2 - start1)); | 4110 record_change (start1, (end2 - start1)); |
4120 modify_region (current_buffer, start1, end2); | 4111 modify_region (current_buffer, start1, end2); |
4121 | 4112 |
4122 tmp_interval1 = copy_intervals (cur_intv, start1, len1); | 4113 tmp_interval1 = copy_intervals (cur_intv, start1, len1); |
4123 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); | 4114 tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); |
4124 tmp_interval2 = copy_intervals (cur_intv, start2, len2); | 4115 tmp_interval2 = copy_intervals (cur_intv, start2, len2); |
4125 Fset_text_properties (make_number (start1), make_number (end2), | 4116 Fset_text_properties (make_number (start1), make_number (end2), |
4126 Qnil, Qnil); | 4117 Qnil, Qnil); |
4127 | 4118 |
4128 /* holds region 1 */ | 4119 /* holds region 1 */ |
4129 if (len1_byte > 20000) | 4120 SAFE_ALLOCA (temp, unsigned char *, len1_byte); |
4130 temp = (unsigned char *) xmalloc (len1_byte); | |
4131 else | |
4132 temp = (unsigned char *) alloca (len1_byte); | |
4133 start1_addr = BYTE_POS_ADDR (start1_byte); | 4121 start1_addr = BYTE_POS_ADDR (start1_byte); |
4134 start2_addr = BYTE_POS_ADDR (start2_byte); | 4122 start2_addr = BYTE_POS_ADDR (start2_byte); |
4135 bcopy (start1_addr, temp, len1_byte); | 4123 bcopy (start1_addr, temp, len1_byte); |
4136 bcopy (start2_addr, start1_addr, len2_byte); | 4124 bcopy (start2_addr, start1_addr, len2_byte); |
4137 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); | 4125 bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); |
4138 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte); | 4126 bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte); |
4139 if (len1_byte > 20000) | 4127 SAFE_FREE (len1_byte); |
4140 xfree (temp); | 4128 |
4141 graft_intervals_into_buffer (tmp_interval1, end2 - len1, | 4129 graft_intervals_into_buffer (tmp_interval1, end2 - len1, |
4142 len1, current_buffer, 0); | 4130 len1, current_buffer, 0); |
4143 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, | 4131 graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, |
4144 len_mid, current_buffer, 0); | 4132 len_mid, current_buffer, 0); |
4145 graft_intervals_into_buffer (tmp_interval2, start1, | 4133 graft_intervals_into_buffer (tmp_interval2, start1, |