Annotation of parser3/src/lib/cord/cordxtra.c, revision 1.15
1.2 paf 1: /*
2: * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
3: *
4: * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5: * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6: *
7: * Permission is hereby granted to use or copy this program
8: * for any purpose, provided the above notices are retained on all copies.
9: * Permission to modify the code and to distribute modified code is granted,
10: * provided the above notices are retained, and a notice that the code was
11: * modified is included with the above copyright notice.
12: *
13: * Author: Hans-J. Boehm (boehm@parc.xerox.com)
14: */
15: /*
16: * These are functions on cords that do not need to understand their
17: * implementation. They serve also serve as example client code for
18: * cord_basics.
19: */
20: /* Boehm, December 8, 1995 1:53 pm PST */
1.7 paf 21: #include "pa_config_includes.h"
1.2 paf 22: # include <stdio.h>
23: # include <string.h>
24: # include <stdlib.h>
25: # include <stdarg.h>
26: # include "cord.h"
27: # include "ec.h"
1.9 misha 28:
1.2 paf 29: # define I_HIDE_POINTERS /* So we get access to allocation lock. */
30: /* We use this for lazy file reading, */
31: /* so that we remain independent */
32: /* of the threads primitives. */
33: # include "gc.h"
34:
35: /* For now we assume that pointer reads and writes are atomic, */
36: /* i.e. another thread always sees the state before or after */
37: /* a write. This might be false on a Motorola M68K with */
38: /* pointers that are not 32-bit aligned. But there probably */
39: /* aren't too many threads packages running on those. */
40: # define ATOMIC_WRITE(x,y) (x) = (y)
41: # define ATOMIC_READ(x) (*(x))
42:
43: /* The standard says these are in stdio.h, but they aren't always: */
44: # ifndef SEEK_SET
45: # define SEEK_SET 0
46: # endif
47: # ifndef SEEK_END
48: # define SEEK_END 2
49: # endif
50:
51: # define BUFSZ 2048 /* Size of stack allocated buffers when */
52: /* we want large buffers. */
53:
54: typedef void (* oom_fn)(void);
55:
56: # define OUT_OF_MEMORY { if (CORD_oom_fn != (oom_fn) 0) (*CORD_oom_fn)(); \
57: ABORT("Out of memory\n"); }
58: # define ABORT(msg) { fprintf(stderr, "%s\n", msg); abort(); }
59:
60: CORD CORD_cat_char(CORD x, char c)
61: {
62: register char * string;
63:
64: if (c == '\0') return(CORD_cat(x, CORD_nul(1)));
65: string = GC_MALLOC_ATOMIC(2);
66: if (string == 0) OUT_OF_MEMORY;
67: string[0] = c;
68: string[1] = '\0';
69: return(CORD_cat_char_star(x, string, 1));
70: }
71:
72: CORD CORD_catn(int nargs, ...)
73: {
74: register CORD result = CORD_EMPTY;
75: va_list args;
76: register int i;
77:
78: va_start(args, nargs);
79: for (i = 0; i < nargs; i++) {
80: register CORD next = va_arg(args, CORD);
81: result = CORD_cat(result, next);
82: }
83: va_end(args);
84: return(result);
85: }
86:
87: typedef struct {
88: size_t len;
89: size_t count;
90: char * buf;
91: } CORD_fill_data;
92:
93: int CORD_fill_proc(char c, void * client_data)
94: {
95: register CORD_fill_data * d = (CORD_fill_data *)client_data;
96: register size_t count = d -> count;
97:
98: (d -> buf)[count] = c;
99: d -> count = ++count;
100: if (count >= d -> len) {
101: return(1);
102: } else {
103: return(0);
104: }
105: }
106:
107: int CORD_batched_fill_proc(const char* s, void * client_data)
108: {
109: register CORD_fill_data * d = (CORD_fill_data *)client_data;
110: register size_t count = d -> count;
111: register size_t max = d -> len;
112: register char * buf = d -> buf;
113: register const char* t = s;
114:
115: while((buf[count] = *t++) != '\0') {
116: count++;
117: if (count >= max) {
118: d -> count = count;
119: return(1);
120: }
121: }
122: d -> count = count;
123: return(0);
124: }
125:
126: /* Fill buf with len characters starting at i. */
127: /* Assumes len characters are available. */
128: void CORD_fill_buf(CORD x, size_t i, size_t len, char * buf)
129: {
130: CORD_fill_data fd;
131:
132: fd.len = len;
133: fd.buf = buf;
134: fd.count = 0;
135: (void)CORD_iter5(x, i, CORD_fill_proc, CORD_batched_fill_proc, &fd);
136: }
137:
138: int CORD_cmp(CORD x, CORD y)
139: {
140: CORD_pos xpos;
141: CORD_pos ypos;
142: register size_t avail, yavail;
143:
144: if (y == CORD_EMPTY) return(x != CORD_EMPTY);
145: if (x == CORD_EMPTY) return(-1);
1.15 ! misha 146: if (x == y) return (0);
1.2 paf 147: if (CORD_IS_STRING(y) && CORD_IS_STRING(x)) return(strcmp(x,y));
148: CORD_set_pos(xpos, x, 0);
149: CORD_set_pos(ypos, y, 0);
150: for(;;) {
151: if (!CORD_pos_valid(xpos)) {
152: if (CORD_pos_valid(ypos)) {
153: return(-1);
154: } else {
155: return(0);
156: }
157: }
158: if (!CORD_pos_valid(ypos)) {
159: return(1);
160: }
161: if ((avail = CORD_pos_chars_left(xpos)) <= 0
162: || (yavail = CORD_pos_chars_left(ypos)) <= 0) {
163: register char xcurrent = CORD_pos_fetch(xpos);
164: register char ycurrent = CORD_pos_fetch(ypos);
165: if (xcurrent != ycurrent) return(xcurrent - ycurrent);
166: CORD_next(xpos);
167: CORD_next(ypos);
168: } else {
169: /* process as many characters as we can */
170: register int result;
171:
172: if (avail > yavail) avail = yavail;
173: result = strncmp(CORD_pos_cur_char_addr(xpos),
174: CORD_pos_cur_char_addr(ypos), avail);
175: if (result != 0) return(result);
176: CORD_pos_advance(xpos, avail);
177: CORD_pos_advance(ypos, avail);
178: }
179: }
180: }
181:
182: int CORD_ncmp(CORD x, size_t x_start, CORD y, size_t y_start, size_t len)
183: {
184: CORD_pos xpos;
185: CORD_pos ypos;
186: register size_t count;
187: register long avail, yavail;
188:
189: CORD_set_pos(xpos, x, x_start);
190: CORD_set_pos(ypos, y, y_start);
191: for(count = 0; count < len;) {
192: if (!CORD_pos_valid(xpos)) {
193: if (CORD_pos_valid(ypos)) {
194: return(-1);
195: } else {
196: return(0);
197: }
198: }
199: if (!CORD_pos_valid(ypos)) {
200: return(1);
201: }
202: if ((avail = CORD_pos_chars_left(xpos)) <= 0
203: || (yavail = CORD_pos_chars_left(ypos)) <= 0) {
204: register char xcurrent = CORD_pos_fetch(xpos);
205: register char ycurrent = CORD_pos_fetch(ypos);
206: if (xcurrent != ycurrent) return(xcurrent - ycurrent);
207: CORD_next(xpos);
208: CORD_next(ypos);
209: count++;
210: } else {
211: /* process as many characters as we can */
212: register int result;
213:
214: if (avail > yavail) avail = yavail;
215: count += avail;
216: if (count > len) avail -= (count - len);
217: result = strncmp(CORD_pos_cur_char_addr(xpos),
218: CORD_pos_cur_char_addr(ypos), (size_t)avail);
219: if (result != 0) return(result);
220: CORD_pos_advance(xpos, (size_t)avail);
221: CORD_pos_advance(ypos, (size_t)avail);
222: }
223: }
224: return(0);
225: }
226:
1.13 misha 227: char * CORD_to_char_star(CORD x, size_t len)
1.2 paf 228: {
1.13 misha 229: char * result;
230: if(0 == len) len = CORD_len(x);
231: result = GC_MALLOC_ATOMIC(len + 1);
1.2 paf 232:
233: if (result == 0) OUT_OF_MEMORY;
234: CORD_fill_buf(x, 0, len, result);
235: result[len] = '\0';
236: return(result);
237: }
238:
239: CORD CORD_from_char_star(const char* s)
240: {
241: char * result;
242: size_t len = strlen(s);
243:
244: if (0 == len) return(CORD_EMPTY);
245: result = GC_MALLOC_ATOMIC(len + 1);
246: if (result == 0) OUT_OF_MEMORY;
247: memcpy(result, s, len+1);
248: return(result);
249: }
250:
1.13 misha 251: const char* CORD_to_const_char_star(CORD x, size_t len)
1.2 paf 252: {
253: if (x == 0) return("");
254: if (CORD_IS_STRING(x)) return((const char* )x);
1.13 misha 255: return(CORD_to_char_star(x, len));
1.2 paf 256: }
257:
258: char CORD_fetch(CORD x, size_t i)
259: {
260: CORD_pos xpos;
261:
262: CORD_set_pos(xpos, x, i);
263: if (!CORD_pos_valid(xpos)) ABORT("bad index?");
264: return(CORD_pos_fetch(xpos));
265: }
266:
267:
268: int CORD_put_proc(char c, void * client_data)
269: {
270: register FILE * f = (FILE *)client_data;
271:
272: return(putc(c, f) == EOF);
273: }
274:
275: int CORD_batched_put_proc(const char* s, void * client_data)
276: {
277: register FILE * f = (FILE *)client_data;
278:
279: return(fputs(s, f) == EOF);
280: }
281:
282:
283: int CORD_put(CORD x, FILE * f)
284: {
285: if (CORD_iter5(x, 0, CORD_put_proc, CORD_batched_put_proc, f)) {
286: return(EOF);
287: } else {
288: return(1);
289: }
290: }
291:
292: typedef struct {
293: size_t pos; /* Current position in the cord */
294: char target; /* Character we're looking for */
295: } chr_data;
296:
297: int CORD_chr_proc(char c, void * client_data)
298: {
299: register chr_data * d = (chr_data *)client_data;
300:
301: if (c == d -> target) return(1);
302: (d -> pos) ++;
303: return(0);
304: }
1.4 paf 305:
1.2 paf 306: int CORD_rchr_proc(char c, void * client_data)
307: {
308: register chr_data * d = (chr_data *)client_data;
309:
310: if (c == d -> target) return(1);
311: (d -> pos) --;
312: return(0);
313: }
314:
315: int CORD_batched_chr_proc(const char* s, void * client_data)
316: {
317: register chr_data * d = (chr_data *)client_data;
318: register char * occ = strchr(s, d -> target);
319:
320: if (occ == 0) {
321: d -> pos += strlen(s);
322: return(0);
323: } else {
324: d -> pos += occ - s;
325: return(1);
326: }
327: }
328:
329: size_t CORD_chr(CORD x, size_t i, int c)
330: {
331: chr_data d;
332:
333: d.pos = i;
334: d.target = c;
335: if (CORD_iter5(x, i, CORD_chr_proc, CORD_batched_chr_proc, &d)) {
336: return(d.pos);
337: } else {
338: return(CORD_NOT_FOUND);
339: }
340: }
341:
342: size_t CORD_rchr(CORD x, size_t i, int c)
343: {
344: chr_data d;
345:
346: d.pos = i;
347: d.target = c;
348: if (CORD_riter4(x, i, CORD_rchr_proc, &d)) {
349: return(d.pos);
350: } else {
351: return(CORD_NOT_FOUND);
352: }
353: }
354:
355: /* Find the first occurrence of s in x at position start or later. */
356: /* This uses an asymptotically poor algorithm, which should typically */
357: /* perform acceptably. We compare the first few characters directly, */
358: /* and call CORD_ncmp whenever there is a partial match. */
359: /* This has the advantage that we allocate very little, or not at all. */
360: /* It's very fast if there are few close misses. */
1.13 misha 361: size_t CORD_str(CORD x, size_t start, CORD s, size_t xlen)
1.2 paf 362: {
363: CORD_pos xpos;
364: size_t slen;
365: register size_t start_len;
366: const char* s_start;
367: unsigned long s_buf = 0; /* The first few characters of s */
368: unsigned long x_buf = 0; /* Start of candidate substring. */
369: /* Initialized only to make compilers */
370: /* happy. */
371: unsigned long mask = 0;
372: register size_t i;
373: register size_t match_pos;
374:
375: if (s == CORD_EMPTY) return(start);
376: if (CORD_IS_STRING(s)) {
377: s_start = s;
378: slen = strlen(s);
379: } else {
1.13 misha 380: s_start = CORD_to_char_star(CORD_substr(s, 0, sizeof(unsigned long), 0), 0);
1.2 paf 381: slen = CORD_len(s);
382: }
383: if (xlen < start || xlen - start < slen) return(CORD_NOT_FOUND);
384: start_len = slen;
385: if (start_len > sizeof(unsigned long)) start_len = sizeof(unsigned long);
386: CORD_set_pos(xpos, x, start);
387: for (i = 0; i < start_len; i++) {
388: mask <<= 8;
389: mask |= 0xff;
390: s_buf <<= 8;
1.3 paf 391: s_buf |= (unsigned char)s_start[i];
1.2 paf 392: x_buf <<= 8;
1.5 paf 393: x_buf |= (unsigned char)CORD_pos_fetch(xpos);
1.2 paf 394: CORD_next(xpos);
395: }
396: for (match_pos = start; ; match_pos++) {
397: if ((x_buf & mask) == s_buf) {
398: if (slen == start_len ||
399: CORD_ncmp(x, match_pos + start_len,
400: s, start_len, slen - start_len) == 0) {
401: return(match_pos);
402: }
403: }
404: if ( match_pos == xlen - slen ) {
405: return(CORD_NOT_FOUND);
406: }
407: x_buf <<= 8;
1.5 paf 408: x_buf |= (unsigned char)CORD_pos_fetch(xpos);
1.2 paf 409: CORD_next(xpos);
410: }
411: }
412:
413: void CORD_ec_flush_buf(CORD_ec x)
414: {
415: register size_t len = x[0].ec_bufptr - x[0].ec_buf;
416: char * s;
417:
418: if (len == 0) return;
419: s = GC_MALLOC_ATOMIC(len+1);
1.14 misha 420: if (s == 0) OUT_OF_MEMORY;
1.2 paf 421: memcpy(s, x[0].ec_buf, len);
422: s[len] = '\0';
423: x[0].ec_cord = CORD_cat_char_star(x[0].ec_cord, s, len);
424: x[0].ec_bufptr = x[0].ec_buf;
425: }
426:
427: void CORD_ec_append_cord(CORD_ec x, CORD s)
428: {
429: CORD_ec_flush_buf(x);
430: x[0].ec_cord = CORD_cat(x[0].ec_cord, s);
431: }
432:
433: /*ARGSUSED*/
434: char CORD_nul_func(size_t i, void * client_data)
435: {
436: return((char)(unsigned long)client_data);
1.4 paf 437: }
438:
1.9 misha 439: #ifdef CORD_CHARS_CACHE
1.11 misha 440: static char* cord_chars_cache[256][15]={0};
1.9 misha 441: #endif
442:
1.2 paf 443: CORD CORD_chars(char c, size_t i)
444: {
1.8 misha 445: if (i>0 && i<16 /* SHORT_LIMIT */) {
1.10 misha 446: register char* result;
1.9 misha 447: #ifdef CORD_CHARS_CACHE
1.11 misha 448: if(cord_chars_cache[(unsigned char)c][i])
449: return((CORD) cord_chars_cache[(unsigned char)c][i]);
1.9 misha 450: #endif
1.8 misha 451: result=GC_MALLOC_ATOMIC(i+1);
452: if(result==0) OUT_OF_MEMORY;
453: memset(result, c, i);
454: result[i] = '\0';
1.9 misha 455: #ifdef CORD_CHARS_CACHE
1.11 misha 456: cord_chars_cache[(unsigned char)c][i]=result;
1.9 misha 457: #endif
1.8 misha 458: return((CORD) result);
459: } else {
460: return(CORD_from_fn(CORD_nul_func, (void *)(unsigned long)c, i));
461: }
1.2 paf 462: }
463:
464: CORD CORD_from_file_eager(FILE * f)
465: {
466: register int c;
467: CORD_ec ecord;
468:
469: CORD_ec_init(ecord);
470: for(;;) {
471: c = getc(f);
472: if (c == 0) {
473: /* Append the right number of NULs */
474: /* Note that any string of NULs is rpresented in 4 words, */
475: /* independent of its length. */
476: register size_t count = 1;
477:
478: CORD_ec_flush_buf(ecord);
479: while ((c = getc(f)) == 0) count++;
480: ecord[0].ec_cord = CORD_cat(ecord[0].ec_cord, CORD_nul(count));
481: }
482: if (c == EOF) break;
483: CORD_ec_append(ecord, c);
484: }
485: (void) fclose(f);
486: return(CORD_balance(CORD_ec_to_cord(ecord)));
487: }
488:
489: /* The state maintained for a lazily read file consists primarily */
490: /* of a large direct-mapped cache of previously read values. */
491: /* We could rely more on stdio buffering. That would have 2 */
492: /* disadvantages: */
493: /* 1) Empirically, not all fseek implementations preserve the */
494: /* buffer whenever they could. */
495: /* 2) It would fail if 2 different sections of a long cord */
496: /* were being read alternately. */
497: /* We do use the stdio buffer for read ahead. */
498: /* To guarantee thread safety in the presence of atomic pointer */
499: /* writes, cache lines are always replaced, and never modified in */
500: /* place. */
501:
502: # define LOG_CACHE_SZ 14
503: # define CACHE_SZ (1 << LOG_CACHE_SZ)
504: # define LOG_LINE_SZ 9
505: # define LINE_SZ (1 << LOG_LINE_SZ)
506:
507: typedef struct {
508: size_t tag;
509: char data[LINE_SZ];
510: /* data[i%LINE_SZ] = ith char in file if tag = i/LINE_SZ */
511: } cache_line;
512:
513: typedef struct {
514: FILE * lf_file;
515: size_t lf_current; /* Current file pointer value */
516: cache_line * volatile lf_cache[CACHE_SZ/LINE_SZ];
517: } lf_state;
518:
519: # define MOD_CACHE_SZ(n) ((n) & (CACHE_SZ - 1))
520: # define DIV_CACHE_SZ(n) ((n) >> LOG_CACHE_SZ)
521: # define MOD_LINE_SZ(n) ((n) & (LINE_SZ - 1))
522: # define DIV_LINE_SZ(n) ((n) >> LOG_LINE_SZ)
523: # define LINE_START(n) ((n) & ~(LINE_SZ - 1))
524:
525: typedef struct {
526: lf_state * state;
527: size_t file_pos; /* Position of needed character. */
528: cache_line * new_cache;
529: } refill_data;
530:
531: /* Executed with allocation lock. */
532: static char refill_cache(client_data)
533: refill_data * client_data;
534: {
535: register lf_state * state = client_data -> state;
536: register size_t file_pos = client_data -> file_pos;
537: FILE *f = state -> lf_file;
538: size_t line_start = LINE_START(file_pos);
539: size_t line_no = DIV_LINE_SZ(MOD_CACHE_SZ(file_pos));
540: cache_line * new_cache = client_data -> new_cache;
541:
542: if (line_start != state -> lf_current
543: && fseek(f, line_start, SEEK_SET) != 0) {
544: ABORT("fseek failed");
545: }
546: if (fread(new_cache -> data, sizeof(char), LINE_SZ, f)
547: <= file_pos - line_start) {
548: ABORT("fread failed");
549: }
550: new_cache -> tag = DIV_LINE_SZ(file_pos);
551: /* Store barrier goes here. */
552: ATOMIC_WRITE(state -> lf_cache[line_no], new_cache);
553: state -> lf_current = line_start + LINE_SZ;
554: return(new_cache->data[MOD_LINE_SZ(file_pos)]);
555: }
556:
557: char CORD_lf_func(size_t i, void * client_data)
558: {
559: register lf_state * state = (lf_state *)client_data;
560: register cache_line * volatile * cl_addr =
561: &(state -> lf_cache[DIV_LINE_SZ(MOD_CACHE_SZ(i))]);
562: register cache_line * cl = (cache_line *)ATOMIC_READ(cl_addr);
563:
564: if (cl == 0 || cl -> tag != DIV_LINE_SZ(i)) {
565: /* Cache miss */
566: refill_data rd;
567:
568: rd.state = state;
569: rd.file_pos = i;
570: rd.new_cache = GC_NEW_ATOMIC(cache_line);
571: if (rd.new_cache == 0) OUT_OF_MEMORY;
572: return((char)(GC_word)
573: GC_call_with_alloc_lock((GC_fn_type) refill_cache, &rd));
574: }
575: return(cl -> data[MOD_LINE_SZ(i)]);
576: }
577:
578: /*ARGSUSED*/
579: void CORD_lf_close_proc(void * obj, void * client_data)
580: {
581: if (fclose(((lf_state *)obj) -> lf_file) != 0) {
582: ABORT("CORD_lf_close_proc: fclose failed");
583: }
584: }
585:
586: #ifndef PA_DEBUG_DISABLE_GC
587: CORD CORD_from_file_lazy_inner(FILE * f, size_t len)
588: {
589: register lf_state * state = GC_NEW(lf_state);
590: register int i;
591:
592: if (state == 0) OUT_OF_MEMORY;
593: if (len != 0) {
594: /* Dummy read to force buffer allocation. */
595: /* This greatly increases the probability */
596: /* of avoiding deadlock if buffer allocation */
597: /* is redirected to GC_malloc and the */
598: /* world is multithreaded. */
599: char buf[1];
600:
601: (void) fread(buf, 1, 1, f);
602: rewind(f);
603: }
604: state -> lf_file = f;
605: for (i = 0; i < CACHE_SZ/LINE_SZ; i++) {
606: state -> lf_cache[i] = 0;
607: }
608: state -> lf_current = 0;
609: GC_REGISTER_FINALIZER(state, CORD_lf_close_proc, 0, 0, 0);
610: return(CORD_from_fn(CORD_lf_func, state, len));
611: }
612:
613: CORD CORD_from_file_lazy(FILE * f)
614: {
615: register long len;
616:
617: if (fseek(f, 0l, SEEK_END) != 0) {
618: ABORT("Bad fd argument - fseek failed");
619: }
620: if ((len = ftell(f)) < 0) {
621: ABORT("Bad fd argument - ftell failed");
622: }
623: rewind(f);
624: return(CORD_from_file_lazy_inner(f, (size_t)len));
625: }
626:
627: # define LAZY_THRESHOLD (128*1024 + 1)
628:
629: CORD CORD_from_file(FILE * f)
630: {
631: register long len;
632:
633: if (fseek(f, 0l, SEEK_END) != 0) {
634: ABORT("Bad fd argument - fseek failed");
635: }
636: if ((len = ftell(f)) < 0) {
637: ABORT("Bad fd argument - ftell failed");
638: }
639: rewind(f);
640: if (len < LAZY_THRESHOLD) {
641: return(CORD_from_file_eager(f));
642: } else {
643: return(CORD_from_file_lazy_inner(f, (size_t)len));
644: }
645: }
646: #endif
E-mail: