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