aboutsummaryrefslogtreecommitdiffstats
path: root/src/charset.c
diff options
context:
space:
mode:
authorPaul Eggert2011-08-04 19:15:35 -0700
committerPaul Eggert2011-08-04 19:15:35 -0700
commit0065d05491ce5981ea20896bb26d21dcd31e6769 (patch)
tree13240167319d4a99ab5eacae4a883258eb2d28de /src/charset.c
parent18ab493650d648ab8dca651ea2698861f926e895 (diff)
downloademacs-0065d05491ce5981ea20896bb26d21dcd31e6769.tar.gz
emacs-0065d05491ce5981ea20896bb26d21dcd31e6769.zip
Adjust in response to jan.h.d's comments.
See, for example <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9196#26>.
Diffstat (limited to 'src/charset.c')
-rw-r--r--src/charset.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/src/charset.c b/src/charset.c
index 852aeb19bcb..6967b9df611 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -61,7 +61,7 @@ Lisp_Object Vcharset_hash_table;
61/* Table of struct charset. */ 61/* Table of struct charset. */
62struct charset *charset_table; 62struct charset *charset_table;
63 63
64static int charset_table_size; 64static ptrdiff_t charset_table_size;
65static int charset_table_used; 65static int charset_table_used;
66 66
67Lisp_Object Qcharsetp; 67Lisp_Object Qcharsetp;
@@ -1150,28 +1150,25 @@ usage: (define-charset-internal ...) */)
1150 hash_code); 1150 hash_code);
1151 if (charset_table_used == charset_table_size) 1151 if (charset_table_used == charset_table_size)
1152 { 1152 {
1153 struct charset *new_table;
1154 /* Ensure that charset IDs fit into 'int' as well as into the 1153 /* Ensure that charset IDs fit into 'int' as well as into the
1155 restriction imposed by fixnums, ptrdiff_t, and size_t. 1154 restriction imposed by fixnums. Although the 'int' restriction
1156 Although the 'int' restriction could be removed, too much other 1155 could be removed, too much other code would need altering; for
1157 code would need altering; for example, the IDs are stuffed into 1156 example, the IDs are stuffed into struct
1158 struct coding_system.charbuf[i] entries, which are 'int'. */ 1157 coding_system.charbuf[i] entries, which are 'int'. */
1159 int charset_table_size_max = 1158 int old_size = charset_table_size;
1160 min (min (INT_MAX, MOST_POSITIVE_FIXNUM), 1159 struct charset *new_table =
1161 min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct charset)); 1160 xpalloc (0, &charset_table_size, 1,
1162 if (charset_table_size_max - 16 < charset_table_size) 1161 min (INT_MAX, MOST_POSITIVE_FIXNUM),
1163 memory_full (SIZE_MAX); 1162 sizeof *charset_table);
1164 new_table 1163 memcpy (new_table, charset_table, old_size * sizeof *new_table);
1165 = (struct charset *) xmalloc (sizeof (struct charset)
1166 * (charset_table_size + 16));
1167 memcpy (new_table, charset_table,
1168 sizeof (struct charset) * charset_table_size);
1169 charset_table_size += 16;
1170 charset_table = new_table; 1164 charset_table = new_table;
1171 /* FIXME: Doesn't this leak memory? The old charset_table 1165 /* FIXME: Doesn't this leak memory? The old charset_table becomes
1172 becomes unreachable. If the memory leak is intentional, 1166 unreachable. It could be that this is intentional, because the
1173 a comment should be added to explain this. If not, the 1167 old charset table may be in a dumped emacs, and reallocating such
1174 old charset_table should be freed, using xfree. */ 1168 a table may not work. If the memory leak is intentional, a
1169 comment should be added to explain this. If not, the old
1170 charset_table should be freed, by passing it as the 1st argument
1171 to xpalloc and removing the memcpy. */
1175 } 1172 }
1176 id = charset_table_used++; 1173 id = charset_table_used++;
1177 new_definition_p = 1; 1174 new_definition_p = 1;
@@ -2230,14 +2227,16 @@ struct charset_sort_data
2230{ 2227{
2231 Lisp_Object charset; 2228 Lisp_Object charset;
2232 int id; 2229 int id;
2233 int priority; 2230 ptrdiff_t priority;
2234}; 2231};
2235 2232
2236static int 2233static int
2237charset_compare (const void *d1, const void *d2) 2234charset_compare (const void *d1, const void *d2)
2238{ 2235{
2239 const struct charset_sort_data *data1 = d1, *data2 = d2; 2236 const struct charset_sort_data *data1 = d1, *data2 = d2;
2240 return (data1->priority - data2->priority); 2237 if (data1->priority != data2->priority)
2238 return data1->priority < data2->priority ? -1 : 1;
2239 return 0;
2241} 2240}
2242 2241
2243DEFUN ("sort-charsets", Fsort_charsets, Ssort_charsets, 1, 1, 0, 2242DEFUN ("sort-charsets", Fsort_charsets, Ssort_charsets, 1, 1, 0,
@@ -2247,7 +2246,8 @@ See also `charset-priority-list' and `set-charset-priority'. */)
2247 (Lisp_Object charsets) 2246 (Lisp_Object charsets)
2248{ 2247{
2249 Lisp_Object len = Flength (charsets); 2248 Lisp_Object len = Flength (charsets);
2250 int n = XFASTINT (len), i, j, done; 2249 ptrdiff_t n = XFASTINT (len), i, j;
2250 int done;
2251 Lisp_Object tail, elt, attrs; 2251 Lisp_Object tail, elt, attrs;
2252 struct charset_sort_data *sort_data; 2252 struct charset_sort_data *sort_data;
2253 int id, min_id = INT_MAX, max_id = INT_MIN; 2253 int id, min_id = INT_MAX, max_id = INT_MIN;
@@ -2255,7 +2255,7 @@ See also `charset-priority-list' and `set-charset-priority'. */)
2255 2255
2256 if (n == 0) 2256 if (n == 0)
2257 return Qnil; 2257 return Qnil;
2258 SAFE_ALLOCA (sort_data, struct charset_sort_data *, sizeof (*sort_data) * n); 2258 SAFE_NALLOCA (sort_data, 1, n);
2259 for (tail = charsets, i = 0; CONSP (tail); tail = XCDR (tail), i++) 2259 for (tail = charsets, i = 0; CONSP (tail); tail = XCDR (tail), i++)
2260 { 2260 {
2261 elt = XCAR (tail); 2261 elt = XCAR (tail);
@@ -2330,6 +2330,17 @@ init_charset_once (void)
2330void 2330void
2331syms_of_charset (void) 2331syms_of_charset (void)
2332{ 2332{
2333 /* Allocate an initial charset table that is just under 64 KiB in size.
2334 This should be large enough so that the charset table need not be
2335 reallocated during an initial bootstrap. Allocating anything larger than
2336 64 KiB in an initial run may not work, because glibc malloc might use
2337 mmap for larger allocations, and these don't work well across dumped
2338 systems. */
2339 enum {
2340 initial_malloc_max = (1 << 16) - 1,
2341 charset_table_size_init = initial_malloc_max / sizeof (struct charset)
2342 };
2343
2333 DEFSYM (Qcharsetp, "charsetp"); 2344 DEFSYM (Qcharsetp, "charsetp");
2334 2345
2335 DEFSYM (Qascii, "ascii"); 2346 DEFSYM (Qascii, "ascii");
@@ -2362,8 +2373,9 @@ syms_of_charset (void)
2362 Vcharset_hash_table = Fmake_hash_table (2, args); 2373 Vcharset_hash_table = Fmake_hash_table (2, args);
2363 } 2374 }
2364 2375
2365 charset_table = (struct charset *) xmalloc (sizeof (struct charset) * 128); 2376 charset_table = (struct charset *) xmalloc (sizeof (struct charset)
2366 charset_table_size = 128; 2377 * charset_table_size_init);
2378 charset_table_size = charset_table_size_init;
2367 charset_table_used = 0; 2379 charset_table_used = 0;
2368 2380
2369 defsubr (&Scharsetp); 2381 defsubr (&Scharsetp);