428
|
1 /* XEmacs routines to deal with range tables.
|
|
2 Copyright (C) 1995 Sun Microsystems, Inc.
|
800
|
3 Copyright (C) 1995, 2002 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* Written by Ben Wing, August 1995. */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28 #include "rangetab.h"
|
|
29
|
|
30 Lisp_Object Qrange_tablep;
|
|
31 Lisp_Object Qrange_table;
|
|
32
|
|
33
|
|
34 /************************************************************************/
|
|
35 /* Range table object */
|
|
36 /************************************************************************/
|
|
37
|
|
38 /* We use a sorted array of ranges.
|
|
39
|
|
40 #### We should be using the gap array stuff from extents.c. This
|
|
41 is not hard but just requires moving that stuff out of that file. */
|
|
42
|
|
43 static Lisp_Object
|
|
44 mark_range_table (Lisp_Object obj)
|
|
45 {
|
440
|
46 Lisp_Range_Table *rt = XRANGE_TABLE (obj);
|
428
|
47 int i;
|
|
48
|
|
49 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
50 mark_object (Dynarr_at (rt->entries, i).val);
|
|
51 return Qnil;
|
|
52 }
|
|
53
|
|
54 static void
|
2286
|
55 print_range_table (Lisp_Object obj, Lisp_Object printcharfun,
|
|
56 int UNUSED (escapeflag))
|
428
|
57 {
|
440
|
58 Lisp_Range_Table *rt = XRANGE_TABLE (obj);
|
428
|
59 int i;
|
|
60
|
826
|
61 write_c_string (printcharfun, "#s(range-table data (");
|
428
|
62 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
63 {
|
|
64 struct range_table_entry *rte = Dynarr_atp (rt->entries, i);
|
|
65 if (i > 0)
|
826
|
66 write_c_string (printcharfun, " ");
|
428
|
67 if (rte->first == rte->last)
|
800
|
68 write_fmt_string (printcharfun, "%ld ", (long) (rte->first));
|
428
|
69 else
|
800
|
70 write_fmt_string (printcharfun, "(%ld %ld) ", (long) (rte->first),
|
|
71 (long) (rte->last));
|
428
|
72 print_internal (rte->val, printcharfun, 1);
|
|
73 }
|
826
|
74 write_c_string (printcharfun, "))");
|
428
|
75 }
|
|
76
|
|
77 static int
|
|
78 range_table_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
79 {
|
440
|
80 Lisp_Range_Table *rt1 = XRANGE_TABLE (obj1);
|
|
81 Lisp_Range_Table *rt2 = XRANGE_TABLE (obj2);
|
428
|
82 int i;
|
|
83
|
|
84 if (Dynarr_length (rt1->entries) != Dynarr_length (rt2->entries))
|
|
85 return 0;
|
|
86
|
|
87 for (i = 0; i < Dynarr_length (rt1->entries); i++)
|
|
88 {
|
|
89 struct range_table_entry *rte1 = Dynarr_atp (rt1->entries, i);
|
|
90 struct range_table_entry *rte2 = Dynarr_atp (rt2->entries, i);
|
|
91
|
|
92 if (rte1->first != rte2->first
|
|
93 || rte1->last != rte2->last
|
|
94 || !internal_equal (rte1->val, rte2->val, depth + 1))
|
|
95 return 0;
|
|
96 }
|
|
97
|
|
98 return 1;
|
|
99 }
|
|
100
|
|
101 static unsigned long
|
|
102 range_table_entry_hash (struct range_table_entry *rte, int depth)
|
|
103 {
|
|
104 return HASH3 (rte->first, rte->last, internal_hash (rte->val, depth + 1));
|
|
105 }
|
|
106
|
|
107 static unsigned long
|
|
108 range_table_hash (Lisp_Object obj, int depth)
|
|
109 {
|
440
|
110 Lisp_Range_Table *rt = XRANGE_TABLE (obj);
|
428
|
111 int i;
|
|
112 int size = Dynarr_length (rt->entries);
|
|
113 unsigned long hash = size;
|
|
114
|
|
115 /* approach based on internal_array_hash(). */
|
|
116 if (size <= 5)
|
|
117 {
|
|
118 for (i = 0; i < size; i++)
|
|
119 hash = HASH2 (hash,
|
|
120 range_table_entry_hash (Dynarr_atp (rt->entries, i),
|
|
121 depth));
|
|
122 return hash;
|
|
123 }
|
|
124
|
|
125 /* just pick five elements scattered throughout the array.
|
|
126 A slightly better approach would be to offset by some
|
|
127 noise factor from the points chosen below. */
|
|
128 for (i = 0; i < 5; i++)
|
|
129 hash = HASH2 (hash, range_table_entry_hash (Dynarr_atp (rt->entries,
|
|
130 i*size/5),
|
|
131 depth));
|
|
132 return hash;
|
|
133 }
|
|
134
|
1204
|
135 static const struct memory_description rte_description_1[] = {
|
440
|
136 { XD_LISP_OBJECT, offsetof (range_table_entry, val) },
|
428
|
137 { XD_END }
|
|
138 };
|
|
139
|
1204
|
140 static const struct sized_memory_description rte_description = {
|
440
|
141 sizeof (range_table_entry),
|
428
|
142 rte_description_1
|
|
143 };
|
|
144
|
1204
|
145 static const struct memory_description rted_description_1[] = {
|
440
|
146 XD_DYNARR_DESC (range_table_entry_dynarr, &rte_description),
|
428
|
147 { XD_END }
|
|
148 };
|
|
149
|
1204
|
150 static const struct sized_memory_description rted_description = {
|
440
|
151 sizeof (range_table_entry_dynarr),
|
428
|
152 rted_description_1
|
|
153 };
|
|
154
|
1204
|
155 static const struct memory_description range_table_description[] = {
|
440
|
156 { XD_STRUCT_PTR, offsetof (Lisp_Range_Table, entries), 1, &rted_description },
|
428
|
157 { XD_END }
|
|
158 };
|
|
159
|
934
|
160 DEFINE_LRECORD_IMPLEMENTATION ("range-table", range_table,
|
|
161 1, /*dumpable-flag*/
|
|
162 mark_range_table, print_range_table, 0,
|
|
163 range_table_equal, range_table_hash,
|
|
164 range_table_description,
|
|
165 Lisp_Range_Table);
|
428
|
166
|
|
167 /************************************************************************/
|
|
168 /* Range table operations */
|
|
169 /************************************************************************/
|
|
170
|
800
|
171 #ifdef ERROR_CHECK_STRUCTURES
|
428
|
172
|
|
173 static void
|
440
|
174 verify_range_table (Lisp_Range_Table *rt)
|
428
|
175 {
|
|
176 int i;
|
|
177
|
|
178 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
179 {
|
|
180 struct range_table_entry *rte = Dynarr_atp (rt->entries, i);
|
|
181 assert (rte->last >= rte->first);
|
|
182 if (i > 0)
|
|
183 assert (Dynarr_at (rt->entries, i - 1).last < rte->first);
|
|
184 }
|
|
185 }
|
|
186
|
|
187 #else
|
|
188
|
|
189 #define verify_range_table(rt)
|
|
190
|
|
191 #endif
|
|
192
|
|
193 /* Look up in a range table without the Dynarr wrapper.
|
|
194 Used also by the unified range table format. */
|
|
195
|
|
196 static Lisp_Object
|
|
197 get_range_table (EMACS_INT pos, int nentries, struct range_table_entry *tab,
|
|
198 Lisp_Object default_)
|
|
199 {
|
|
200 int left = 0, right = nentries;
|
|
201
|
|
202 /* binary search for the entry. Based on similar code in
|
|
203 extent_list_locate(). */
|
|
204 while (left != right)
|
|
205 {
|
|
206 /* RIGHT might not point to a valid entry (i.e. it's at the end
|
|
207 of the list), so NEWPOS must round down. */
|
647
|
208 int newpos = (left + right) >> 1;
|
428
|
209 struct range_table_entry *entry = tab + newpos;
|
|
210 if (pos > entry->last)
|
|
211 left = newpos+1;
|
|
212 else if (pos < entry->first)
|
|
213 right = newpos;
|
|
214 else
|
|
215 return entry->val;
|
|
216 }
|
|
217
|
|
218 return default_;
|
|
219 }
|
|
220
|
|
221 DEFUN ("range-table-p", Frange_table_p, 1, 1, 0, /*
|
|
222 Return non-nil if OBJECT is a range table.
|
|
223 */
|
|
224 (object))
|
|
225 {
|
|
226 return RANGE_TABLEP (object) ? Qt : Qnil;
|
|
227 }
|
|
228
|
|
229 DEFUN ("make-range-table", Fmake_range_table, 0, 0, 0, /*
|
|
230 Return a new, empty range table.
|
|
231 You can manipulate it using `put-range-table', `get-range-table',
|
|
232 `remove-range-table', and `clear-range-table'.
|
|
233 */
|
|
234 ())
|
|
235 {
|
440
|
236 Lisp_Range_Table *rt = alloc_lcrecord_type (Lisp_Range_Table,
|
|
237 &lrecord_range_table);
|
428
|
238 rt->entries = Dynarr_new (range_table_entry);
|
793
|
239 return wrap_range_table (rt);
|
428
|
240 }
|
|
241
|
|
242 DEFUN ("copy-range-table", Fcopy_range_table, 1, 1, 0, /*
|
444
|
243 Return a new range table which is a copy of RANGE-TABLE.
|
|
244 It will contain the same values for the same ranges as RANGE-TABLE.
|
|
245 The values will not themselves be copied.
|
428
|
246 */
|
444
|
247 (range_table))
|
428
|
248 {
|
440
|
249 Lisp_Range_Table *rt, *rtnew;
|
428
|
250
|
444
|
251 CHECK_RANGE_TABLE (range_table);
|
|
252 rt = XRANGE_TABLE (range_table);
|
428
|
253
|
440
|
254 rtnew = alloc_lcrecord_type (Lisp_Range_Table, &lrecord_range_table);
|
428
|
255 rtnew->entries = Dynarr_new (range_table_entry);
|
|
256
|
|
257 Dynarr_add_many (rtnew->entries, Dynarr_atp (rt->entries, 0),
|
|
258 Dynarr_length (rt->entries));
|
793
|
259 return wrap_range_table (rtnew);
|
428
|
260 }
|
|
261
|
|
262 DEFUN ("get-range-table", Fget_range_table, 2, 3, 0, /*
|
444
|
263 Find value for position POS in RANGE-TABLE.
|
428
|
264 If there is no corresponding value, return DEFAULT (defaults to nil).
|
|
265 */
|
444
|
266 (pos, range_table, default_))
|
428
|
267 {
|
440
|
268 Lisp_Range_Table *rt;
|
428
|
269
|
444
|
270 CHECK_RANGE_TABLE (range_table);
|
|
271 rt = XRANGE_TABLE (range_table);
|
428
|
272
|
|
273 CHECK_INT_COERCE_CHAR (pos);
|
|
274
|
|
275 return get_range_table (XINT (pos), Dynarr_length (rt->entries),
|
|
276 Dynarr_atp (rt->entries, 0), default_);
|
|
277 }
|
|
278
|
|
279 void
|
|
280 put_range_table (Lisp_Object table, EMACS_INT first,
|
|
281 EMACS_INT last, Lisp_Object val)
|
|
282 {
|
|
283 int i;
|
|
284 int insert_me_here = -1;
|
440
|
285 Lisp_Range_Table *rt = XRANGE_TABLE (table);
|
428
|
286
|
|
287 /* Now insert in the proper place. This gets tricky because
|
|
288 we may be overlapping one or more existing ranges and need
|
|
289 to fix them up. */
|
|
290
|
|
291 /* First delete all sections of any existing ranges that overlap
|
|
292 the new range. */
|
|
293 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
294 {
|
|
295 struct range_table_entry *entry = Dynarr_atp (rt->entries, i);
|
|
296 /* We insert before the first range that begins at or after the
|
|
297 new range. */
|
|
298 if (entry->first >= first && insert_me_here < 0)
|
|
299 insert_me_here = i;
|
|
300 if (entry->last < first)
|
|
301 /* completely before the new range. */
|
|
302 continue;
|
|
303 if (entry->first > last)
|
|
304 /* completely after the new range. No more possibilities of
|
|
305 finding overlapping ranges. */
|
|
306 break;
|
|
307 if (entry->first < first && entry->last <= last)
|
|
308 {
|
|
309 /* looks like:
|
|
310
|
|
311 [ NEW ]
|
|
312 [ EXISTING ]
|
|
313
|
|
314 */
|
|
315 /* truncate the end off of it. */
|
|
316 entry->last = first - 1;
|
|
317 }
|
|
318 else if (entry->first < first && entry->last > last)
|
|
319 /* looks like:
|
|
320
|
|
321 [ NEW ]
|
|
322 [ EXISTING ]
|
|
323
|
|
324 */
|
|
325 /* need to split this one in two. */
|
|
326 {
|
|
327 struct range_table_entry insert_me_too;
|
|
328
|
|
329 insert_me_too.first = last + 1;
|
|
330 insert_me_too.last = entry->last;
|
|
331 insert_me_too.val = entry->val;
|
|
332 entry->last = first - 1;
|
|
333 Dynarr_insert_many (rt->entries, &insert_me_too, 1, i + 1);
|
|
334 }
|
|
335 else if (entry->last > last)
|
|
336 {
|
|
337 /* looks like:
|
|
338
|
|
339 [ NEW ]
|
|
340 [ EXISTING ]
|
|
341
|
|
342 */
|
|
343 /* truncate the start off of it. */
|
|
344 entry->first = last + 1;
|
|
345 }
|
|
346 else
|
|
347 {
|
|
348 /* existing is entirely within new. */
|
|
349 Dynarr_delete_many (rt->entries, i, 1);
|
|
350 i--; /* back up since everything shifted one to the left. */
|
|
351 }
|
|
352 }
|
|
353
|
|
354 /* Someone asked us to delete the range, not insert it. */
|
|
355 if (UNBOUNDP (val))
|
|
356 return;
|
|
357
|
|
358 /* Now insert the new entry, maybe at the end. */
|
|
359
|
|
360 if (insert_me_here < 0)
|
|
361 insert_me_here = i;
|
|
362
|
|
363 {
|
|
364 struct range_table_entry insert_me;
|
|
365
|
|
366 insert_me.first = first;
|
|
367 insert_me.last = last;
|
|
368 insert_me.val = val;
|
|
369
|
|
370 Dynarr_insert_many (rt->entries, &insert_me, 1, insert_me_here);
|
|
371 }
|
|
372
|
|
373 /* Now see if we can combine this entry with adjacent ones just
|
|
374 before or after. */
|
|
375
|
|
376 if (insert_me_here > 0)
|
|
377 {
|
|
378 struct range_table_entry *entry = Dynarr_atp (rt->entries,
|
|
379 insert_me_here - 1);
|
|
380 if (EQ (val, entry->val) && entry->last == first - 1)
|
|
381 {
|
|
382 entry->last = last;
|
|
383 Dynarr_delete_many (rt->entries, insert_me_here, 1);
|
|
384 insert_me_here--;
|
|
385 /* We have morphed into a larger range. Update our records
|
|
386 in case we also combine with the one after. */
|
|
387 first = entry->first;
|
|
388 }
|
|
389 }
|
|
390
|
|
391 if (insert_me_here < Dynarr_length (rt->entries) - 1)
|
|
392 {
|
|
393 struct range_table_entry *entry = Dynarr_atp (rt->entries,
|
|
394 insert_me_here + 1);
|
|
395 if (EQ (val, entry->val) && entry->first == last + 1)
|
|
396 {
|
|
397 entry->first = first;
|
|
398 Dynarr_delete_many (rt->entries, insert_me_here, 1);
|
|
399 }
|
|
400 }
|
|
401 }
|
|
402
|
|
403 DEFUN ("put-range-table", Fput_range_table, 4, 4, 0, /*
|
444
|
404 Set the value for range (START, END) to be VALUE in RANGE-TABLE.
|
428
|
405 */
|
444
|
406 (start, end, value, range_table))
|
428
|
407 {
|
|
408 EMACS_INT first, last;
|
|
409
|
444
|
410 CHECK_RANGE_TABLE (range_table);
|
428
|
411 CHECK_INT_COERCE_CHAR (start);
|
|
412 first = XINT (start);
|
|
413 CHECK_INT_COERCE_CHAR (end);
|
|
414 last = XINT (end);
|
|
415 if (first > last)
|
563
|
416 invalid_argument_2 ("start must be <= end", start, end);
|
428
|
417
|
444
|
418 put_range_table (range_table, first, last, value);
|
|
419 verify_range_table (XRANGE_TABLE (range_table));
|
428
|
420 return Qnil;
|
|
421 }
|
|
422
|
|
423 DEFUN ("remove-range-table", Fremove_range_table, 3, 3, 0, /*
|
444
|
424 Remove the value for range (START, END) in RANGE-TABLE.
|
428
|
425 */
|
444
|
426 (start, end, range_table))
|
428
|
427 {
|
444
|
428 return Fput_range_table (start, end, Qunbound, range_table);
|
428
|
429 }
|
|
430
|
|
431 DEFUN ("clear-range-table", Fclear_range_table, 1, 1, 0, /*
|
444
|
432 Flush RANGE-TABLE.
|
428
|
433 */
|
444
|
434 (range_table))
|
428
|
435 {
|
444
|
436 CHECK_RANGE_TABLE (range_table);
|
|
437 Dynarr_reset (XRANGE_TABLE (range_table)->entries);
|
428
|
438 return Qnil;
|
|
439 }
|
|
440
|
|
441 DEFUN ("map-range-table", Fmap_range_table, 2, 2, 0, /*
|
444
|
442 Map FUNCTION over entries in RANGE-TABLE, calling it with three args,
|
428
|
443 the beginning and end of the range and the corresponding value.
|
442
|
444
|
|
445 Results are guaranteed to be correct (i.e. each entry processed
|
|
446 exactly once) if FUNCTION modifies or deletes the current entry
|
444
|
447 \(i.e. passes the current range to `put-range-table' or
|
442
|
448 `remove-range-table'), but not otherwise.
|
428
|
449 */
|
444
|
450 (function, range_table))
|
428
|
451 {
|
442
|
452 Lisp_Range_Table *rt;
|
|
453 int i;
|
|
454
|
444
|
455 CHECK_RANGE_TABLE (range_table);
|
442
|
456 CHECK_FUNCTION (function);
|
|
457
|
444
|
458 rt = XRANGE_TABLE (range_table);
|
442
|
459
|
|
460 /* Do not "optimize" by pulling out the length computation below!
|
|
461 FUNCTION may have changed the table. */
|
|
462 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
463 {
|
|
464 struct range_table_entry *entry = Dynarr_atp (rt->entries, i);
|
|
465 EMACS_INT first, last;
|
|
466 Lisp_Object args[4];
|
|
467 int oldlen;
|
|
468
|
|
469 again:
|
|
470 first = entry->first;
|
|
471 last = entry->last;
|
|
472 oldlen = Dynarr_length (rt->entries);
|
|
473 args[0] = function;
|
|
474 args[1] = make_int (first);
|
|
475 args[2] = make_int (last);
|
|
476 args[3] = entry->val;
|
|
477 Ffuncall (countof (args), args);
|
|
478 /* Has FUNCTION removed the entry? */
|
|
479 if (oldlen > Dynarr_length (rt->entries)
|
|
480 && i < Dynarr_length (rt->entries)
|
|
481 && (first != entry->first || last != entry->last))
|
|
482 goto again;
|
|
483 }
|
|
484
|
428
|
485 return Qnil;
|
|
486 }
|
|
487
|
|
488
|
|
489 /************************************************************************/
|
|
490 /* Range table read syntax */
|
|
491 /************************************************************************/
|
|
492
|
|
493 static int
|
2286
|
494 rangetab_data_validate (Lisp_Object UNUSED (keyword), Lisp_Object value,
|
|
495 Error_Behavior UNUSED (errb))
|
428
|
496 {
|
|
497 Lisp_Object rest;
|
|
498
|
|
499 /* #### should deal with errb */
|
|
500 EXTERNAL_LIST_LOOP (rest, value)
|
|
501 {
|
|
502 Lisp_Object range = XCAR (rest);
|
|
503 rest = XCDR (rest);
|
|
504 if (!CONSP (rest))
|
563
|
505 sferror ("Invalid list format", value);
|
428
|
506 if (!INTP (range) && !CHARP (range)
|
|
507 && !(CONSP (range) && CONSP (XCDR (range))
|
|
508 && NILP (XCDR (XCDR (range)))
|
|
509 && (INTP (XCAR (range)) || CHARP (XCAR (range)))
|
|
510 && (INTP (XCAR (XCDR (range))) || CHARP (XCAR (XCDR (range))))))
|
563
|
511 sferror ("Invalid range format", range);
|
428
|
512 }
|
|
513
|
|
514 return 1;
|
|
515 }
|
|
516
|
|
517 static Lisp_Object
|
|
518 rangetab_instantiate (Lisp_Object data)
|
|
519 {
|
|
520 Lisp_Object rangetab = Fmake_range_table ();
|
|
521
|
|
522 if (!NILP (data))
|
|
523 {
|
|
524 data = Fcar (Fcdr (data)); /* skip over 'data keyword */
|
|
525 while (!NILP (data))
|
|
526 {
|
|
527 Lisp_Object range = Fcar (data);
|
|
528 Lisp_Object val = Fcar (Fcdr (data));
|
|
529
|
|
530 data = Fcdr (Fcdr (data));
|
|
531 if (CONSP (range))
|
|
532 Fput_range_table (Fcar (range), Fcar (Fcdr (range)), val,
|
|
533 rangetab);
|
|
534 else
|
|
535 Fput_range_table (range, range, val, rangetab);
|
|
536 }
|
|
537 }
|
|
538
|
|
539 return rangetab;
|
|
540 }
|
|
541
|
|
542
|
|
543 /************************************************************************/
|
|
544 /* Unified range tables */
|
|
545 /************************************************************************/
|
|
546
|
|
547 /* A "unified range table" is a format for storing range tables
|
|
548 as contiguous blocks of memory. This is used by the regexp
|
|
549 code, which needs to use range tables to properly handle []
|
|
550 constructs in the presence of extended characters but wants to
|
|
551 store an entire compiled pattern as a contiguous block of memory.
|
|
552
|
|
553 Unified range tables are designed so that they can be placed
|
|
554 at an arbitrary (possibly mis-aligned) place in memory.
|
|
555 (Dealing with alignment is a pain in the ass.)
|
|
556
|
|
557 WARNING: No provisions for garbage collection are currently made.
|
|
558 This means that there must not be any Lisp objects in a unified
|
|
559 range table that need to be marked for garbage collection.
|
|
560 Good candidates for objects that can go into a range table are
|
|
561
|
|
562 -- numbers and characters (do not need to be marked)
|
|
563 -- nil, t (marked elsewhere)
|
|
564 -- charsets and coding systems (automatically marked because
|
|
565 they are in a marked list,
|
|
566 and can't be removed)
|
|
567
|
|
568 Good but slightly less so:
|
|
569
|
|
570 -- symbols (could be uninterned, but that is not likely)
|
|
571
|
|
572 Somewhat less good:
|
|
573
|
|
574 -- buffers, frames, devices (could get deleted)
|
|
575
|
|
576
|
|
577 It is expected that you work with range tables in the normal
|
|
578 format and then convert to unified format when you are done
|
|
579 making modifications. As such, no functions are provided
|
|
580 for modifying a unified range table. The only operations
|
|
581 you can do to unified range tables are
|
|
582
|
|
583 -- look up a value
|
|
584 -- retrieve all the ranges in an iterative fashion
|
|
585
|
|
586 */
|
|
587
|
|
588 /* The format of a unified range table is as follows:
|
|
589
|
|
590 -- The first byte contains the number of bytes to skip to find the
|
|
591 actual start of the table. This deals with alignment constraints,
|
|
592 since the table might want to go at any arbitrary place in memory.
|
|
593 -- The next three bytes contain the number of bytes to skip (from the
|
|
594 *first* byte) to find the stuff after the table. It's stored in
|
|
595 little-endian format because that's how God intended things. We don't
|
|
596 necessarily start the stuff at the very end of the table because
|
|
597 we want to have at least ALIGNOF (EMACS_INT) extra space in case
|
|
598 we have to move the range table around. (It appears that some
|
|
599 architectures don't maintain alignment when reallocing.)
|
|
600 -- At the prescribed offset is a struct unified_range_table, containing
|
|
601 some number of `struct range_table_entry' entries. */
|
|
602
|
|
603 struct unified_range_table
|
|
604 {
|
|
605 int nentries;
|
|
606 struct range_table_entry first;
|
|
607 };
|
|
608
|
|
609 /* Return size in bytes needed to store the data in a range table. */
|
|
610
|
|
611 int
|
|
612 unified_range_table_bytes_needed (Lisp_Object rangetab)
|
|
613 {
|
|
614 return (sizeof (struct range_table_entry) *
|
|
615 (Dynarr_length (XRANGE_TABLE (rangetab)->entries) - 1) +
|
|
616 sizeof (struct unified_range_table) +
|
|
617 /* ALIGNOF a struct may be too big. */
|
|
618 /* We have four bytes for the size numbers, and an extra
|
|
619 four or eight bytes for making sure we get the alignment
|
|
620 OK. */
|
|
621 ALIGNOF (EMACS_INT) + 4);
|
|
622 }
|
|
623
|
|
624 /* Convert a range table into unified format and store in DEST,
|
|
625 which must be able to hold the number of bytes returned by
|
|
626 range_table_bytes_needed(). */
|
|
627
|
|
628 void
|
|
629 unified_range_table_copy_data (Lisp_Object rangetab, void *dest)
|
|
630 {
|
|
631 /* We cast to the above structure rather than just casting to
|
|
632 char * and adding sizeof(int), because that will lead to
|
|
633 mis-aligned data on the Alpha machines. */
|
|
634 struct unified_range_table *un;
|
|
635 range_table_entry_dynarr *rted = XRANGE_TABLE (rangetab)->entries;
|
|
636 int total_needed = unified_range_table_bytes_needed (rangetab);
|
826
|
637 void *new_dest = ALIGN_PTR ((char *) dest + 4, EMACS_INT);
|
428
|
638
|
|
639 * (char *) dest = (char) ((char *) new_dest - (char *) dest);
|
|
640 * ((unsigned char *) dest + 1) = total_needed & 0xFF;
|
|
641 total_needed >>= 8;
|
|
642 * ((unsigned char *) dest + 2) = total_needed & 0xFF;
|
|
643 total_needed >>= 8;
|
|
644 * ((unsigned char *) dest + 3) = total_needed & 0xFF;
|
|
645 un = (struct unified_range_table *) new_dest;
|
|
646 un->nentries = Dynarr_length (rted);
|
|
647 memcpy (&un->first, Dynarr_atp (rted, 0),
|
|
648 sizeof (struct range_table_entry) * Dynarr_length (rted));
|
|
649 }
|
|
650
|
|
651 /* Return number of bytes actually used by a unified range table. */
|
|
652
|
|
653 int
|
|
654 unified_range_table_bytes_used (void *unrangetab)
|
|
655 {
|
|
656 return ((* ((unsigned char *) unrangetab + 1))
|
|
657 + ((* ((unsigned char *) unrangetab + 2)) << 8)
|
|
658 + ((* ((unsigned char *) unrangetab + 3)) << 16));
|
|
659 }
|
|
660
|
|
661 /* Make sure the table is aligned, and move it around if it's not. */
|
|
662 static void
|
|
663 align_the_damn_table (void *unrangetab)
|
|
664 {
|
|
665 void *cur_dest = (char *) unrangetab + * (char *) unrangetab;
|
826
|
666 if (cur_dest != ALIGN_PTR (cur_dest, EMACS_INT))
|
428
|
667 {
|
|
668 int count = (unified_range_table_bytes_used (unrangetab) - 4
|
|
669 - ALIGNOF (EMACS_INT));
|
|
670 /* Find the proper location, just like above. */
|
826
|
671 void *new_dest = ALIGN_PTR ((char *) unrangetab + 4, EMACS_INT);
|
428
|
672 /* memmove() works in the presence of overlapping data. */
|
|
673 memmove (new_dest, cur_dest, count);
|
|
674 * (char *) unrangetab = (char) ((char *) new_dest - (char *) unrangetab);
|
|
675 }
|
|
676 }
|
|
677
|
|
678 /* Look up a value in a unified range table. */
|
|
679
|
|
680 Lisp_Object
|
|
681 unified_range_table_lookup (void *unrangetab, EMACS_INT pos,
|
|
682 Lisp_Object default_)
|
|
683 {
|
|
684 void *new_dest;
|
|
685 struct unified_range_table *un;
|
|
686
|
|
687 align_the_damn_table (unrangetab);
|
|
688 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
689 un = (struct unified_range_table *) new_dest;
|
|
690
|
|
691 return get_range_table (pos, un->nentries, &un->first, default_);
|
|
692 }
|
|
693
|
|
694 /* Return number of entries in a unified range table. */
|
|
695
|
|
696 int
|
|
697 unified_range_table_nentries (void *unrangetab)
|
|
698 {
|
|
699 void *new_dest;
|
|
700 struct unified_range_table *un;
|
|
701
|
|
702 align_the_damn_table (unrangetab);
|
|
703 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
704 un = (struct unified_range_table *) new_dest;
|
|
705 return un->nentries;
|
|
706 }
|
|
707
|
|
708 /* Return the OFFSETth range (counting from 0) in UNRANGETAB. */
|
|
709 void
|
|
710 unified_range_table_get_range (void *unrangetab, int offset,
|
|
711 EMACS_INT *min, EMACS_INT *max,
|
|
712 Lisp_Object *val)
|
|
713 {
|
|
714 void *new_dest;
|
|
715 struct unified_range_table *un;
|
|
716 struct range_table_entry *tab;
|
|
717
|
|
718 align_the_damn_table (unrangetab);
|
|
719 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
720 un = (struct unified_range_table *) new_dest;
|
|
721
|
|
722 assert (offset >= 0 && offset < un->nentries);
|
|
723 tab = (&un->first) + offset;
|
|
724 *min = tab->first;
|
|
725 *max = tab->last;
|
|
726 *val = tab->val;
|
|
727 }
|
|
728
|
|
729
|
|
730 /************************************************************************/
|
|
731 /* Initialization */
|
|
732 /************************************************************************/
|
|
733
|
|
734 void
|
|
735 syms_of_rangetab (void)
|
|
736 {
|
442
|
737 INIT_LRECORD_IMPLEMENTATION (range_table);
|
|
738
|
563
|
739 DEFSYMBOL_MULTIWORD_PREDICATE (Qrange_tablep);
|
|
740 DEFSYMBOL (Qrange_table);
|
428
|
741
|
|
742 DEFSUBR (Frange_table_p);
|
|
743 DEFSUBR (Fmake_range_table);
|
|
744 DEFSUBR (Fcopy_range_table);
|
|
745 DEFSUBR (Fget_range_table);
|
|
746 DEFSUBR (Fput_range_table);
|
|
747 DEFSUBR (Fremove_range_table);
|
|
748 DEFSUBR (Fclear_range_table);
|
|
749 DEFSUBR (Fmap_range_table);
|
|
750 }
|
|
751
|
|
752 void
|
|
753 structure_type_create_rangetab (void)
|
|
754 {
|
|
755 struct structure_type *st;
|
|
756
|
|
757 st = define_structure_type (Qrange_table, 0, rangetab_instantiate);
|
|
758
|
|
759 define_structure_type_keyword (st, Qdata, rangetab_data_validate);
|
|
760 }
|