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