diff --git a/src/ChangeLog b/src/ChangeLog index 2b0f6922ed6..8c0a7c0bcf1 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,14 @@ +2004-10-27 Kim F. Storm + + * editfns.c (Fformat): Allocate discarded table with SAFE_ALLOCA. + Only allocate info and discarded tables once. + + * lisp.h (USE_SAFE_ALLOCA): Add and init sa_must_free integer. + (SAFE_ALLOCA, SAFE_ALLOCA_LISP): Increment it when malloc is used. + (SAFE_FREE): Test it to determine if we need to unwind to free. + Remove size arg. All users changed. + (SAFE_FREE_LISP) Remove. All users changed to use SAFE_FREE. + 2004-10-26 Jan Dj,Ad(Brv * gtkutil.c: Put empty line between comment and function body. diff --git a/src/data.c b/src/data.c index dd26feee264..561a034b8fd 100644 --- a/src/data.c +++ b/src/data.c @@ -2060,7 +2060,7 @@ bool-vector. IDX starts at 0. */) p1 = SDATA (array) + idxval_byte; bcopy (str + idxval_byte + prev_bytes, p1 + new_bytes, nbytes - (idxval_byte + prev_bytes)); - SAFE_FREE (nbytes); + SAFE_FREE (); clear_string_char_byte_cache (); } while (new_bytes--) @@ -2102,7 +2102,7 @@ bool-vector. IDX starts at 0. */) *p1++ = *p0++; bcopy (str + idxval_byte + prev_bytes, p1, nbytes - (idxval_byte + prev_bytes)); - SAFE_FREE (nbytes); + SAFE_FREE (); clear_string_char_byte_cache (); } } diff --git a/src/editfns.c b/src/editfns.c index c3a69fc3f0c..e1482936fe5 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -2858,7 +2858,7 @@ It returns the number of characters changed. */) pos = XINT (start); pos_byte = CHAR_TO_BYTE (pos); - end_pos = XINT (end); + end_pos = XINT (end); modify_region (current_buffer, pos, end_pos); cnt = 0; @@ -3387,7 +3387,7 @@ usage: (format STRING &rest OBJECTS) */) /* discarded[I] is 1 if byte I of the format string was not copied into the output. It is 2 if byte I was not the first byte of its character. */ - char *discarded; + char *discarded = 0; /* Each element records, for one argument, the start and end bytepos in the output string, @@ -3438,11 +3438,13 @@ usage: (format STRING &rest OBJECTS) */) { int nbytes = (nargs+1) * sizeof *info; int i; - info = (struct info *) alloca (nbytes); + if (!info) + info = (struct info *) alloca (nbytes); bzero (info, nbytes); for (i = 0; i <= nargs; i++) info[i].start = -1; - discarded = (char *) alloca (SBYTES (args[0])); + if (!discarded) + SAFE_ALLOCA (discarded, char *, SBYTES (args[0])); bzero (discarded, SBYTES (args[0])); } @@ -3803,7 +3805,7 @@ usage: (format STRING &rest OBJECTS) */) val = make_specified_string (buf, nchars, p - buf, multibyte); /* If we allocated BUF with malloc, free it too. */ - SAFE_FREE (total); + SAFE_FREE (); /* If the format string has text properties, or any of the string arguments has text properties, set up text properties of the @@ -4187,7 +4189,7 @@ Transposing beyond buffer boundaries is an error. */) bcopy (start2_addr, temp, len2_byte); bcopy (start1_addr, start1_addr + len2_byte, len1_byte); bcopy (temp, start1_addr, len2_byte); - SAFE_FREE (len2_byte); + SAFE_FREE (); } else /* First region not smaller than second. */ @@ -4200,7 +4202,7 @@ Transposing beyond buffer boundaries is an error. */) bcopy (start1_addr, temp, len1_byte); bcopy (start2_addr, start1_addr, len2_byte); bcopy (temp, start1_addr + len2_byte, len1_byte); - SAFE_FREE (len1_byte); + SAFE_FREE (); } graft_intervals_into_buffer (tmp_interval1, start1 + len2, len1, current_buffer, 0); @@ -4236,7 +4238,7 @@ Transposing beyond buffer boundaries is an error. */) bcopy (start1_addr, temp, len1_byte); bcopy (start2_addr, start1_addr, len2_byte); bcopy (temp, start2_addr, len1_byte); - SAFE_FREE (len1_byte); + SAFE_FREE (); graft_intervals_into_buffer (tmp_interval1, start2, len1, current_buffer, 0); @@ -4265,7 +4267,7 @@ Transposing beyond buffer boundaries is an error. */) bcopy (start1_addr, start1_addr + len_mid + len2_byte, len1_byte); safe_bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); bcopy (temp, start1_addr, len2_byte); - SAFE_FREE (len2_byte); + SAFE_FREE (); graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); @@ -4296,7 +4298,7 @@ Transposing beyond buffer boundaries is an error. */) bcopy (start2_addr, start1_addr, len2_byte); bcopy (start1_addr + len1_byte, start1_addr + len2_byte, len_mid); bcopy (temp, start1_addr + len2_byte + len_mid, len1_byte); - SAFE_FREE (len1_byte); + SAFE_FREE (); graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); diff --git a/src/fns.c b/src/fns.c index fe59cb57793..cfbcc83fe8d 100644 --- a/src/fns.c +++ b/src/fns.c @@ -936,7 +936,7 @@ string_make_multibyte (string) 0, 1); ret = make_multibyte_string (buf, SCHARS (string), nbytes); - SAFE_FREE (nbytes); + SAFE_FREE (); return ret; } @@ -969,7 +969,7 @@ string_to_multibyte (string) str_to_multibyte (buf, nbytes, SBYTES (string)); ret = make_multibyte_string (buf, SCHARS (string), nbytes); - SAFE_FREE (nbytes); + SAFE_FREE (); return ret; } @@ -996,7 +996,7 @@ string_make_unibyte (string) 1, 0); ret = make_unibyte_string (buf, nchars); - SAFE_FREE (nchars); + SAFE_FREE (); return ret; } @@ -2489,7 +2489,7 @@ SEQUENCE may be a list, a vector, a bool-vector, or a string. */) args[i] = separator; ret = Fconcat (nargs, args); - SAFE_FREE_LISP (nargs); + SAFE_FREE (); return ret; } @@ -2517,7 +2517,7 @@ SEQUENCE may be a list, a vector, a bool-vector, or a string. */) mapcar1 (leni, args, function, sequence); ret = Flist (leni, args); - SAFE_FREE_LISP (leni); + SAFE_FREE (); return ret; } @@ -3226,7 +3226,7 @@ into shorter lines. */) if (encoded_length < 0) { /* The encoding wasn't possible. */ - SAFE_FREE (allength); + SAFE_FREE (); error ("Multibyte character in data for base64 encoding"); } @@ -3234,7 +3234,7 @@ into shorter lines. */) and delete the old. (Insert first in order to preserve markers.) */ SET_PT_BOTH (XFASTINT (beg), ibeg); insert (encoded, encoded_length); - SAFE_FREE (allength); + SAFE_FREE (); del_range_byte (ibeg + encoded_length, iend + encoded_length, 1); /* If point was outside of the region, restore it exactly; else just @@ -3283,12 +3283,12 @@ into shorter lines. */) if (encoded_length < 0) { /* The encoding wasn't possible. */ - SAFE_FREE (allength); + SAFE_FREE (); error ("Multibyte character in data for base64 encoding"); } encoded_string = make_unibyte_string (encoded, encoded_length); - SAFE_FREE (allength); + SAFE_FREE (); return encoded_string; } @@ -3431,7 +3431,7 @@ If the region can't be decoded, signal an error and don't modify the buffer. */ if (decoded_length < 0) { /* The decoding wasn't possible. */ - SAFE_FREE (allength); + SAFE_FREE (); error ("Invalid base64 data"); } @@ -3439,7 +3439,7 @@ If the region can't be decoded, signal an error and don't modify the buffer. */ and delete the old. (Insert first in order to preserve markers.) */ TEMP_SET_PT_BOTH (XFASTINT (beg), ibeg); insert_1_both (decoded, inserted_chars, decoded_length, 0, 1, 0); - SAFE_FREE (allength); + SAFE_FREE (); /* Delete the original text. */ del_range_both (PT, PT_BYTE, XFASTINT (end) + inserted_chars, @@ -3483,7 +3483,7 @@ DEFUN ("base64-decode-string", Fbase64_decode_string, Sbase64_decode_string, else decoded_string = Qnil; - SAFE_FREE (length); + SAFE_FREE (); if (!STRINGP (decoded_string)) error ("Invalid base64 data"); diff --git a/src/lisp.h b/src/lisp.h index 5e4d87595eb..0ab02941c9b 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3307,7 +3307,7 @@ extern Lisp_Object Vdirectory_sep_char; extern Lisp_Object safe_alloca_unwind (Lisp_Object); #define USE_SAFE_ALLOCA \ - int sa_count = SPECPDL_INDEX () + int sa_count = SPECPDL_INDEX (), sa_must_free = 0 /* SAFE_ALLOCA allocates a simple buffer. */ @@ -3318,6 +3318,7 @@ extern Lisp_Object safe_alloca_unwind (Lisp_Object); else \ { \ buf = (type) xmalloc (size); \ + sa_must_free++; \ record_unwind_protect (safe_alloca_unwind, \ make_save_value (buf, 0)); \ } \ @@ -3325,10 +3326,12 @@ extern Lisp_Object safe_alloca_unwind (Lisp_Object); /* SAFE_FREE frees xmalloced memory and enables GC as needed. */ -#define SAFE_FREE(size) \ +#define SAFE_FREE() \ do { \ - if ((size) >= MAX_ALLOCA) \ + if (sa_must_free) { \ + sa_must_free = 0; \ unbind_to (sa_count, Qnil); \ + } \ } while (0) @@ -3345,17 +3348,11 @@ extern Lisp_Object safe_alloca_unwind (Lisp_Object); buf = (Lisp_Object *) xmalloc (size_); \ arg_ = make_save_value (buf, nelt); \ XSAVE_VALUE (arg_)->dogc = 1; \ + sa_must_free++; \ record_unwind_protect (safe_alloca_unwind, arg_); \ } \ } while (0) -#define SAFE_FREE_LISP(nelt) \ - do { \ - if (((nelt) * sizeof (Lisp_Object)) >= MAX_ALLOCA) \ - unbind_to (sa_count, Qnil); \ - } while (0) - - #endif /* EMACS_LISP_H */ diff --git a/src/xdisp.c b/src/xdisp.c index d817e847aa3..799f435c7fb 100644 --- a/src/xdisp.c +++ b/src/xdisp.c @@ -6474,7 +6474,7 @@ add_to_log (format, arg1, arg2) bcopy (SDATA (msg), buffer, len); message_dolog (buffer, len - 1, 1, 0); - SAFE_FREE (len); + SAFE_FREE (); UNGCPRO; }