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