isomorfeus-ferret 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +612 -0
- data/README.md +44 -0
- data/ext/isomorfeus_ferret_ext/benchmark.c +223 -0
- data/ext/isomorfeus_ferret_ext/benchmark.h +45 -0
- data/ext/isomorfeus_ferret_ext/benchmarks_all.h +25 -0
- data/ext/isomorfeus_ferret_ext/bm_bitvector.c +123 -0
- data/ext/isomorfeus_ferret_ext/bm_hash.c +118 -0
- data/ext/isomorfeus_ferret_ext/bm_micro_string.c +40 -0
- data/ext/isomorfeus_ferret_ext/bm_store.c +93 -0
- data/ext/isomorfeus_ferret_ext/email.rl +21 -0
- data/ext/isomorfeus_ferret_ext/extconf.rb +5 -0
- data/ext/isomorfeus_ferret_ext/fio_tmpfile.h +53 -0
- data/ext/isomorfeus_ferret_ext/frb_analysis.c +2577 -0
- data/ext/isomorfeus_ferret_ext/frb_index.c +3457 -0
- data/ext/isomorfeus_ferret_ext/frb_lang.c +9 -0
- data/ext/isomorfeus_ferret_ext/frb_lang.h +17 -0
- data/ext/isomorfeus_ferret_ext/frb_qparser.c +629 -0
- data/ext/isomorfeus_ferret_ext/frb_search.c +4460 -0
- data/ext/isomorfeus_ferret_ext/frb_store.c +515 -0
- data/ext/isomorfeus_ferret_ext/frb_threading.h +30 -0
- data/ext/isomorfeus_ferret_ext/frb_utils.c +1127 -0
- data/ext/isomorfeus_ferret_ext/frt_analysis.c +1644 -0
- data/ext/isomorfeus_ferret_ext/frt_analysis.h +247 -0
- data/ext/isomorfeus_ferret_ext/frt_array.c +124 -0
- data/ext/isomorfeus_ferret_ext/frt_array.h +54 -0
- data/ext/isomorfeus_ferret_ext/frt_bitvector.c +95 -0
- data/ext/isomorfeus_ferret_ext/frt_bitvector.h +586 -0
- data/ext/isomorfeus_ferret_ext/frt_compound_io.c +374 -0
- data/ext/isomorfeus_ferret_ext/frt_config.h +44 -0
- data/ext/isomorfeus_ferret_ext/frt_document.c +134 -0
- data/ext/isomorfeus_ferret_ext/frt_document.h +52 -0
- data/ext/isomorfeus_ferret_ext/frt_except.c +95 -0
- data/ext/isomorfeus_ferret_ext/frt_except.h +188 -0
- data/ext/isomorfeus_ferret_ext/frt_field_index.c +233 -0
- data/ext/isomorfeus_ferret_ext/frt_field_index.h +42 -0
- data/ext/isomorfeus_ferret_ext/frt_filter.c +157 -0
- data/ext/isomorfeus_ferret_ext/frt_fs_store.c +502 -0
- data/ext/isomorfeus_ferret_ext/frt_global.c +427 -0
- data/ext/isomorfeus_ferret_ext/frt_global.h +290 -0
- data/ext/isomorfeus_ferret_ext/frt_hash.c +518 -0
- data/ext/isomorfeus_ferret_ext/frt_hash.h +466 -0
- data/ext/isomorfeus_ferret_ext/frt_hashset.c +191 -0
- data/ext/isomorfeus_ferret_ext/frt_hashset.h +206 -0
- data/ext/isomorfeus_ferret_ext/frt_helper.c +62 -0
- data/ext/isomorfeus_ferret_ext/frt_helper.h +13 -0
- data/ext/isomorfeus_ferret_ext/frt_ind.c +353 -0
- data/ext/isomorfeus_ferret_ext/frt_ind.h +54 -0
- data/ext/isomorfeus_ferret_ext/frt_index.c +6377 -0
- data/ext/isomorfeus_ferret_ext/frt_index.h +880 -0
- data/ext/isomorfeus_ferret_ext/frt_lang.c +104 -0
- data/ext/isomorfeus_ferret_ext/frt_lang.h +44 -0
- data/ext/isomorfeus_ferret_ext/frt_mempool.c +87 -0
- data/ext/isomorfeus_ferret_ext/frt_mempool.h +33 -0
- data/ext/isomorfeus_ferret_ext/frt_multimapper.c +349 -0
- data/ext/isomorfeus_ferret_ext/frt_multimapper.h +52 -0
- data/ext/isomorfeus_ferret_ext/frt_posh.c +1006 -0
- data/ext/isomorfeus_ferret_ext/frt_posh.h +973 -0
- data/ext/isomorfeus_ferret_ext/frt_priorityqueue.c +147 -0
- data/ext/isomorfeus_ferret_ext/frt_priorityqueue.h +147 -0
- data/ext/isomorfeus_ferret_ext/frt_q_boolean.c +1612 -0
- data/ext/isomorfeus_ferret_ext/frt_q_const_score.c +157 -0
- data/ext/isomorfeus_ferret_ext/frt_q_filtered_query.c +209 -0
- data/ext/isomorfeus_ferret_ext/frt_q_fuzzy.c +281 -0
- data/ext/isomorfeus_ferret_ext/frt_q_match_all.c +147 -0
- data/ext/isomorfeus_ferret_ext/frt_q_multi_term.c +672 -0
- data/ext/isomorfeus_ferret_ext/frt_q_parser.c +3084 -0
- data/ext/isomorfeus_ferret_ext/frt_q_phrase.c +1182 -0
- data/ext/isomorfeus_ferret_ext/frt_q_prefix.c +98 -0
- data/ext/isomorfeus_ferret_ext/frt_q_range.c +665 -0
- data/ext/isomorfeus_ferret_ext/frt_q_span.c +2386 -0
- data/ext/isomorfeus_ferret_ext/frt_q_term.c +311 -0
- data/ext/isomorfeus_ferret_ext/frt_q_wildcard.c +166 -0
- data/ext/isomorfeus_ferret_ext/frt_ram_store.c +460 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner.c +899 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner.h +28 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner_mb.c +6705 -0
- data/ext/isomorfeus_ferret_ext/frt_scanner_utf8.c +4419 -0
- data/ext/isomorfeus_ferret_ext/frt_search.c +1824 -0
- data/ext/isomorfeus_ferret_ext/frt_search.h +924 -0
- data/ext/isomorfeus_ferret_ext/frt_similarity.c +150 -0
- data/ext/isomorfeus_ferret_ext/frt_similarity.h +79 -0
- data/ext/isomorfeus_ferret_ext/frt_sort.c +796 -0
- data/ext/isomorfeus_ferret_ext/frt_stopwords.c +395 -0
- data/ext/isomorfeus_ferret_ext/frt_store.c +680 -0
- data/ext/isomorfeus_ferret_ext/frt_store.h +789 -0
- data/ext/isomorfeus_ferret_ext/frt_term_vectors.c +72 -0
- data/ext/isomorfeus_ferret_ext/frt_threading.h +23 -0
- data/ext/isomorfeus_ferret_ext/frt_win32.h +54 -0
- data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.c +409 -0
- data/ext/isomorfeus_ferret_ext/isomorfeus_ferret.h +95 -0
- data/ext/isomorfeus_ferret_ext/libstemmer.c +93 -0
- data/ext/isomorfeus_ferret_ext/libstemmer.h +73 -0
- data/ext/isomorfeus_ferret_ext/q_parser.y +1366 -0
- data/ext/isomorfeus_ferret_ext/scanner.h +28 -0
- data/ext/isomorfeus_ferret_ext/scanner.in +43 -0
- data/ext/isomorfeus_ferret_ext/scanner.rl +84 -0
- data/ext/isomorfeus_ferret_ext/scanner_mb.rl +200 -0
- data/ext/isomorfeus_ferret_ext/scanner_utf8.rl +85 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.c +324 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_danish.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.c +610 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_dutch.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.c +1104 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_english.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.c +749 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_finnish.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.c +1233 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_french.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.c +490 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_german.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.c +1217 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_hungarian.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.c +1052 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_italian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.c +283 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_norwegian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.c +735 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_porter.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.c +1003 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_portuguese.h +7 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.c +1079 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_spanish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.c +293 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_1_swedish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.c +984 -0
- data/ext/isomorfeus_ferret_ext/stem_ISO_8859_2_romanian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.c +686 -0
- data/ext/isomorfeus_ferret_ext/stem_KOI8_R_russian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.c +325 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_danish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.c +620 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_dutch.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.c +1111 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_english.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.c +754 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_finnish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.c +1242 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_french.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.c +495 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_german.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.c +1220 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_hungarian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.c +1059 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_italian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.c +285 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_norwegian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.c +741 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_porter.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.c +1009 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_portuguese.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.c +990 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_romanian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.c +680 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_russian.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.c +1083 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_spanish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.c +294 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_swedish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.c +2191 -0
- data/ext/isomorfeus_ferret_ext/stem_UTF_8_turkish.h +6 -0
- data/ext/isomorfeus_ferret_ext/stem_api.c +66 -0
- data/ext/isomorfeus_ferret_ext/stem_api.h +26 -0
- data/ext/isomorfeus_ferret_ext/stem_header.h +57 -0
- data/ext/isomorfeus_ferret_ext/stem_modules.h +190 -0
- data/ext/isomorfeus_ferret_ext/stem_modules.txt +50 -0
- data/ext/isomorfeus_ferret_ext/stem_utilities.c +478 -0
- data/ext/isomorfeus_ferret_ext/test.c +850 -0
- data/ext/isomorfeus_ferret_ext/test.h +416 -0
- data/ext/isomorfeus_ferret_ext/test_1710.c +63 -0
- data/ext/isomorfeus_ferret_ext/test_analysis.c +1221 -0
- data/ext/isomorfeus_ferret_ext/test_array.c +272 -0
- data/ext/isomorfeus_ferret_ext/test_bitvector.c +600 -0
- data/ext/isomorfeus_ferret_ext/test_compound_io.c +170 -0
- data/ext/isomorfeus_ferret_ext/test_document.c +156 -0
- data/ext/isomorfeus_ferret_ext/test_except.c +244 -0
- data/ext/isomorfeus_ferret_ext/test_fields.c +522 -0
- data/ext/isomorfeus_ferret_ext/test_file_deleter.c +185 -0
- data/ext/isomorfeus_ferret_ext/test_filter.c +331 -0
- data/ext/isomorfeus_ferret_ext/test_fs_store.c +25 -0
- data/ext/isomorfeus_ferret_ext/test_global.c +299 -0
- data/ext/isomorfeus_ferret_ext/test_hash.c +485 -0
- data/ext/isomorfeus_ferret_ext/test_hashset.c +288 -0
- data/ext/isomorfeus_ferret_ext/test_helper.c +47 -0
- data/ext/isomorfeus_ferret_ext/test_highlighter.c +548 -0
- data/ext/isomorfeus_ferret_ext/test_index.c +2323 -0
- data/ext/isomorfeus_ferret_ext/test_lang.c +74 -0
- data/ext/isomorfeus_ferret_ext/test_mempool.c +102 -0
- data/ext/isomorfeus_ferret_ext/test_multimapper.c +64 -0
- data/ext/isomorfeus_ferret_ext/test_priorityqueue.c +213 -0
- data/ext/isomorfeus_ferret_ext/test_q_const_score.c +84 -0
- data/ext/isomorfeus_ferret_ext/test_q_filtered.c +61 -0
- data/ext/isomorfeus_ferret_ext/test_q_fuzzy.c +241 -0
- data/ext/isomorfeus_ferret_ext/test_q_parser.c +464 -0
- data/ext/isomorfeus_ferret_ext/test_q_span.c +575 -0
- data/ext/isomorfeus_ferret_ext/test_ram_store.c +77 -0
- data/ext/isomorfeus_ferret_ext/test_search.c +1874 -0
- data/ext/isomorfeus_ferret_ext/test_segments.c +167 -0
- data/ext/isomorfeus_ferret_ext/test_similarity.c +25 -0
- data/ext/isomorfeus_ferret_ext/test_sort.c +333 -0
- data/ext/isomorfeus_ferret_ext/test_store.c +591 -0
- data/ext/isomorfeus_ferret_ext/test_store.h +3 -0
- data/ext/isomorfeus_ferret_ext/test_term.c +351 -0
- data/ext/isomorfeus_ferret_ext/test_term_vectors.c +373 -0
- data/ext/isomorfeus_ferret_ext/test_test.c +83 -0
- data/ext/isomorfeus_ferret_ext/test_threading.c +188 -0
- data/ext/isomorfeus_ferret_ext/testhelper.c +561 -0
- data/ext/isomorfeus_ferret_ext/testhelper.h +25 -0
- data/ext/isomorfeus_ferret_ext/tests_all.h +87 -0
- data/ext/isomorfeus_ferret_ext/uchar-ucs4.rl +1854 -0
- data/ext/isomorfeus_ferret_ext/uchar-utf8.rl +1999 -0
- data/ext/isomorfeus_ferret_ext/url.rl +27 -0
- data/ext/isomorfeus_ferret_ext/word_list.h +15156 -0
- data/lib/isomorfeus/ferret/document.rb +132 -0
- data/lib/isomorfeus/ferret/field_symbol.rb +85 -0
- data/lib/isomorfeus/ferret/index/field_infos.rb +48 -0
- data/lib/isomorfeus/ferret/index/index.rb +970 -0
- data/lib/isomorfeus/ferret/monitor.rb +323 -0
- data/lib/isomorfeus/ferret/stdlib_patches.rb +151 -0
- data/lib/isomorfeus/ferret/version.rb +5 -0
- data/lib/isomorfeus-ferret.rb +8 -0
- metadata +307 -0
@@ -0,0 +1,427 @@
|
|
1
|
+
#include "frt_global.h"
|
2
|
+
#include "frt_hash.h"
|
3
|
+
#include <stdarg.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
#include <stdlib.h>
|
6
|
+
#include <string.h>
|
7
|
+
#include <errno.h>
|
8
|
+
#include <assert.h>
|
9
|
+
#include <math.h>
|
10
|
+
#include <ctype.h>
|
11
|
+
#include <sys/types.h>
|
12
|
+
#include <unistd.h>
|
13
|
+
#include <signal.h>
|
14
|
+
|
15
|
+
const char *FRT_EMPTY_STRING = "";
|
16
|
+
|
17
|
+
int frt_scmp(const void *p1, const void *p2)
|
18
|
+
{
|
19
|
+
return strcmp(*(char **) p1, *(char **) p2);
|
20
|
+
}
|
21
|
+
|
22
|
+
void frt_strsort(char **str_array, int size)
|
23
|
+
{
|
24
|
+
qsort(str_array, size, sizeof(char *), &frt_scmp);
|
25
|
+
}
|
26
|
+
|
27
|
+
int frt_icmp(const void *p1, const void *p2)
|
28
|
+
{
|
29
|
+
int i1 = *(int *) p1;
|
30
|
+
int i2 = *(int *) p2;
|
31
|
+
|
32
|
+
if (i1 > i2) {
|
33
|
+
return 1;
|
34
|
+
}
|
35
|
+
else if (i1 < i2) {
|
36
|
+
return -1;
|
37
|
+
}
|
38
|
+
return 0;
|
39
|
+
}
|
40
|
+
|
41
|
+
int frt_icmp_risky(const void *p1, const void *p2)
|
42
|
+
{
|
43
|
+
return (*(int *)p1) - *((int *)p2);
|
44
|
+
}
|
45
|
+
|
46
|
+
unsigned int *frt_imalloc(unsigned int value)
|
47
|
+
{
|
48
|
+
unsigned int *p = FRT_ALLOC(unsigned int);
|
49
|
+
*p = value;
|
50
|
+
return p;
|
51
|
+
}
|
52
|
+
|
53
|
+
unsigned long *frt_lmalloc(unsigned long value)
|
54
|
+
{
|
55
|
+
unsigned long *p = FRT_ALLOC(unsigned long);
|
56
|
+
*p = value;
|
57
|
+
return p;
|
58
|
+
}
|
59
|
+
|
60
|
+
frt_u32 *frt_u32malloc(frt_u32 value)
|
61
|
+
{
|
62
|
+
frt_u32 *p = FRT_ALLOC(frt_u32);
|
63
|
+
*p = value;
|
64
|
+
return p;
|
65
|
+
}
|
66
|
+
|
67
|
+
frt_u64 *frt_u64malloc(frt_u64 value)
|
68
|
+
{
|
69
|
+
frt_u64 *p = FRT_ALLOC(frt_u64);
|
70
|
+
*p = value;
|
71
|
+
return p;
|
72
|
+
}
|
73
|
+
|
74
|
+
/* concatenate two strings freeing the second */
|
75
|
+
char *frt_estrcat(char *str1, char *str2)
|
76
|
+
{
|
77
|
+
size_t len1 = strlen(str1);
|
78
|
+
size_t len2 = strlen(str2);
|
79
|
+
FRT_REALLOC_N(str1, char, len1 + len2 + 3); /* leave room for <CR> */
|
80
|
+
memcpy(str1 + len1, str2, len2 + 1); /* make sure '\0' copied too */
|
81
|
+
free(str2);
|
82
|
+
return str1;
|
83
|
+
}
|
84
|
+
|
85
|
+
/* epstrdup: duplicate a string with a format, report if error */
|
86
|
+
char *frt_epstrdup(const char *fmt, int len, ...)
|
87
|
+
{
|
88
|
+
char *string;
|
89
|
+
va_list args;
|
90
|
+
len += (int) strlen(fmt);
|
91
|
+
|
92
|
+
string = FRT_ALLOC_N(char, len + 1);
|
93
|
+
va_start(args, len);
|
94
|
+
vsprintf(string, fmt, args);
|
95
|
+
va_end(args);
|
96
|
+
|
97
|
+
return string;
|
98
|
+
}
|
99
|
+
|
100
|
+
/* frt_estrdup: duplicate a string, report if error */
|
101
|
+
char *frt_estrdup(const char *s)
|
102
|
+
{
|
103
|
+
char *t = FRT_ALLOC_N(char, strlen(s) + 1);
|
104
|
+
strcpy(t, s);
|
105
|
+
return t;
|
106
|
+
}
|
107
|
+
|
108
|
+
/* Pretty print a float to the buffer. The buffer should have at least 32
|
109
|
+
* bytes available.
|
110
|
+
*/
|
111
|
+
char *frt_dbl_to_s(char *buf, double num)
|
112
|
+
{
|
113
|
+
char *p, *e;
|
114
|
+
|
115
|
+
#ifdef FRT_IS_C99
|
116
|
+
if (isinf(num)) {
|
117
|
+
return frt_estrdup(num < 0 ? "-Infinity" : "Infinity");
|
118
|
+
}
|
119
|
+
else if (isnan(num)) {
|
120
|
+
return frt_estrdup("NaN");
|
121
|
+
}
|
122
|
+
#endif
|
123
|
+
|
124
|
+
sprintf(buf, FRT_DBL2S, num);
|
125
|
+
if (!(e = strchr(buf, 'e'))) {
|
126
|
+
e = buf + strlen(buf);
|
127
|
+
}
|
128
|
+
if (!isdigit(e[-1])) {
|
129
|
+
/* reformat if ended with decimal point (ex 111111111111111.) */
|
130
|
+
sprintf(buf, "%#.6e", num);
|
131
|
+
if (!(e = strchr(buf, 'e'))) { e = buf + strlen(buf); }
|
132
|
+
}
|
133
|
+
p = e;
|
134
|
+
while (p[-1] == '0' && isdigit(p[-2])) {
|
135
|
+
p--;
|
136
|
+
}
|
137
|
+
|
138
|
+
memmove(p, e, strlen(e) + 1);
|
139
|
+
return buf;
|
140
|
+
}
|
141
|
+
|
142
|
+
/**
|
143
|
+
* frt_strapp: appends a string up to, but not including the \0 character to the
|
144
|
+
* end of a string returning a pointer to the next unassigned character in the
|
145
|
+
* string.
|
146
|
+
*/
|
147
|
+
char *frt_strapp(char *dst, const char *src) {
|
148
|
+
while (*src != '\0') {
|
149
|
+
*dst = *src;
|
150
|
+
++dst;
|
151
|
+
++src;
|
152
|
+
}
|
153
|
+
return dst;
|
154
|
+
}
|
155
|
+
|
156
|
+
/* strfmt: like sprintf except that it allocates memory for the string */
|
157
|
+
char *frt_vstrfmt(const char *fmt, va_list args)
|
158
|
+
{
|
159
|
+
char *string;
|
160
|
+
char *p = (char *) fmt, *q;
|
161
|
+
int len = (int) strlen(fmt) + 1;
|
162
|
+
int slen, curlen;
|
163
|
+
const char *s;
|
164
|
+
long l;
|
165
|
+
double d;
|
166
|
+
|
167
|
+
q = string = FRT_ALLOC_N(char, len);
|
168
|
+
|
169
|
+
while (*p) {
|
170
|
+
if (*p == '%') {
|
171
|
+
p++;
|
172
|
+
switch (*p) {
|
173
|
+
case 's':
|
174
|
+
p++;
|
175
|
+
s = va_arg(args, char *);
|
176
|
+
/* to be consistent with printf print (null) for NULL */
|
177
|
+
if (!s) {
|
178
|
+
s = "(null)";
|
179
|
+
}
|
180
|
+
slen = (int) strlen(s);
|
181
|
+
len += slen;
|
182
|
+
curlen = q - string;
|
183
|
+
FRT_REALLOC_N(string, char, len);
|
184
|
+
q = string + curlen;
|
185
|
+
memcpy(q, s, slen);
|
186
|
+
q += slen;
|
187
|
+
continue;
|
188
|
+
case 'f':
|
189
|
+
p++;
|
190
|
+
len += 32;
|
191
|
+
*q = 0;
|
192
|
+
FRT_REALLOC_N(string, char, len);
|
193
|
+
q = string + strlen(string);
|
194
|
+
d = va_arg(args, double);
|
195
|
+
frt_dbl_to_s(q, d);
|
196
|
+
q += strlen(q);
|
197
|
+
continue;
|
198
|
+
case 'd':
|
199
|
+
p++;
|
200
|
+
len += 20;
|
201
|
+
*q = 0;
|
202
|
+
FRT_REALLOC_N(string, char, len);
|
203
|
+
q = string + strlen(string);
|
204
|
+
l = va_arg(args, long);
|
205
|
+
q += sprintf(q, "%ld", l);
|
206
|
+
continue;
|
207
|
+
default:
|
208
|
+
break;
|
209
|
+
}
|
210
|
+
}
|
211
|
+
*q = *p;
|
212
|
+
p++;
|
213
|
+
q++;
|
214
|
+
}
|
215
|
+
*q = 0;
|
216
|
+
|
217
|
+
return string;
|
218
|
+
}
|
219
|
+
|
220
|
+
char *frt_strfmt(const char *fmt, ...)
|
221
|
+
{
|
222
|
+
va_list args;
|
223
|
+
char *str;
|
224
|
+
va_start(args, fmt);
|
225
|
+
str = frt_vstrfmt(fmt, args);
|
226
|
+
va_end(args);
|
227
|
+
return str;
|
228
|
+
}
|
229
|
+
|
230
|
+
void frt_dummy_free(void *p)
|
231
|
+
{
|
232
|
+
(void)p; /* suppress unused argument warning */
|
233
|
+
}
|
234
|
+
|
235
|
+
#ifdef HAVE_GDB
|
236
|
+
#define CMD_BUF_SIZE (128 + FILENAME_MAX)
|
237
|
+
/* need to declare this as it is masked by default in linux */
|
238
|
+
|
239
|
+
static char *build_shell_command()
|
240
|
+
{
|
241
|
+
int pid = getpid();
|
242
|
+
char *buf = FRT_ALLOC_N(char, CMD_BUF_SIZE);
|
243
|
+
char *command =
|
244
|
+
"gdb -quiet -ex='bt' -ex='quit' %s %d 2>/dev/null | grep '^[ #]'";
|
245
|
+
|
246
|
+
snprintf(buf, CMD_BUF_SIZE, command, frt_progname(), pid);
|
247
|
+
return buf;
|
248
|
+
}
|
249
|
+
|
250
|
+
#endif
|
251
|
+
|
252
|
+
/**
|
253
|
+
* Call out to gdb to get our stacktrace.
|
254
|
+
*/
|
255
|
+
char *frt_get_stacktrace()
|
256
|
+
{
|
257
|
+
#ifdef HAVE_GDB
|
258
|
+
FILE *stream;
|
259
|
+
char *gdb_filename = NULL, *buf = NULL, *stack = NULL;
|
260
|
+
int offset = -FRT_BUFFER_SIZE;
|
261
|
+
|
262
|
+
if ( !(buf = build_shell_command()) ) {
|
263
|
+
fprintf(EXCEPTION_STREAM,
|
264
|
+
"Unable to build stacktrace shell command\n");
|
265
|
+
goto cleanup;
|
266
|
+
}
|
267
|
+
|
268
|
+
if ( !(stream = popen(buf, "r")) ) {
|
269
|
+
fprintf(EXCEPTION_STREAM,
|
270
|
+
"Unable to exec stacktrace shell command: '%s'\n", buf);
|
271
|
+
goto cleanup;
|
272
|
+
}
|
273
|
+
|
274
|
+
do {
|
275
|
+
offset += FRT_BUFFER_SIZE;
|
276
|
+
FRT_REALLOC_N(stack, char, offset + FRT_BUFFER_SIZE);
|
277
|
+
FRT_ZEROSET_N(stack + offset, char, FRT_BUFFER_SIZE);
|
278
|
+
} while(fread(stack + offset, 1, FRT_BUFFER_SIZE, stream) == FRT_BUFFER_SIZE);
|
279
|
+
|
280
|
+
pclose(stream);
|
281
|
+
|
282
|
+
cleanup:
|
283
|
+
if (gdb_filename) free(gdb_filename);
|
284
|
+
if (buf) free(buf);
|
285
|
+
return stack;
|
286
|
+
#else
|
287
|
+
return NULL;
|
288
|
+
#endif
|
289
|
+
}
|
290
|
+
|
291
|
+
void frt_print_stacktrace()
|
292
|
+
{
|
293
|
+
char *stack = frt_get_stacktrace();
|
294
|
+
|
295
|
+
if (stack) {
|
296
|
+
fprintf(EXCEPTION_STREAM, "Stack trace:\n%s", stack);
|
297
|
+
free(stack);
|
298
|
+
} else {
|
299
|
+
fprintf(EXCEPTION_STREAM, "Stack trace not avaialble\n");
|
300
|
+
}
|
301
|
+
}
|
302
|
+
|
303
|
+
typedef struct FreeMe
|
304
|
+
{
|
305
|
+
void *p;
|
306
|
+
frt_free_ft free_func;
|
307
|
+
} FreeMe;
|
308
|
+
|
309
|
+
static FreeMe *free_mes = NULL;
|
310
|
+
static int free_mes_size = 0;
|
311
|
+
static int free_mes_capa = 0;
|
312
|
+
|
313
|
+
void frt_register_for_cleanup(void *p, frt_free_ft free_func)
|
314
|
+
{
|
315
|
+
FreeMe *free_me;
|
316
|
+
if (free_mes_capa == 0) {
|
317
|
+
free_mes_capa = 16;
|
318
|
+
free_mes = FRT_ALLOC_N(FreeMe, free_mes_capa);
|
319
|
+
}
|
320
|
+
else if (free_mes_capa <= free_mes_size) {
|
321
|
+
free_mes_capa *= 2;
|
322
|
+
FRT_REALLOC_N(free_mes, FreeMe, free_mes_capa);
|
323
|
+
}
|
324
|
+
free_me = free_mes + free_mes_size++;
|
325
|
+
free_me->p = p;
|
326
|
+
free_me->free_func = free_func;
|
327
|
+
}
|
328
|
+
|
329
|
+
#define MAX_PROG_NAME 200
|
330
|
+
static char name[MAX_PROG_NAME]; /* program name for error msgs */
|
331
|
+
|
332
|
+
/* frt_setprogname: set stored name of program */
|
333
|
+
void frt_setprogname(const char *str)
|
334
|
+
{
|
335
|
+
strncpy(name, str, sizeof(name) - 1);
|
336
|
+
}
|
337
|
+
|
338
|
+
const char *frt_progname()
|
339
|
+
{
|
340
|
+
return name;
|
341
|
+
}
|
342
|
+
|
343
|
+
static const char *signal_to_string(int signum)
|
344
|
+
{
|
345
|
+
switch (signum)
|
346
|
+
{
|
347
|
+
case SIGILL: return "SIGILL";
|
348
|
+
case SIGABRT: return "SIGABRT";
|
349
|
+
case SIGFPE: return "SIGFPE";
|
350
|
+
#if !defined POSH_OS_WIN32 && !defined POSH_OS_WIN64
|
351
|
+
case SIGBUS: return "SIGBUS";
|
352
|
+
#endif
|
353
|
+
case SIGSEGV: return "SIGSEGV";
|
354
|
+
}
|
355
|
+
|
356
|
+
return "Unknown Signal";
|
357
|
+
}
|
358
|
+
|
359
|
+
static void sighandler_crash(int signum)
|
360
|
+
{
|
361
|
+
frt_print_stacktrace();
|
362
|
+
FRT_XEXIT("Signal", "Exiting on signal %s (%d)", signal_to_string(signum), signum);
|
363
|
+
}
|
364
|
+
|
365
|
+
#define SETSIG_IF_UNSET(sig, handler) do { \
|
366
|
+
signal(sig, handler); \
|
367
|
+
} while(0)
|
368
|
+
|
369
|
+
void frt_init(int argc, const char *const argv[])
|
370
|
+
{
|
371
|
+
if (argc > 0) {
|
372
|
+
frt_setprogname(argv[0]);
|
373
|
+
}
|
374
|
+
|
375
|
+
SETSIG_IF_UNSET(SIGILL , sighandler_crash);
|
376
|
+
SETSIG_IF_UNSET(SIGABRT, sighandler_crash);
|
377
|
+
SETSIG_IF_UNSET(SIGFPE , sighandler_crash);
|
378
|
+
#if !defined POSH_OS_WIN32 && !defined POSH_OS_WIN64
|
379
|
+
SETSIG_IF_UNSET(SIGBUS , sighandler_crash);
|
380
|
+
#endif
|
381
|
+
SETSIG_IF_UNSET(SIGSEGV, sighandler_crash);
|
382
|
+
|
383
|
+
atexit(&frt_hash_finalize);
|
384
|
+
}
|
385
|
+
|
386
|
+
/**
|
387
|
+
* For general use when testing
|
388
|
+
*
|
389
|
+
* TODO wrap in #ifdef
|
390
|
+
*/
|
391
|
+
|
392
|
+
static bool p_switch = false;
|
393
|
+
static bool p_switch_tmp = false;
|
394
|
+
|
395
|
+
void p(const char *format, ...)
|
396
|
+
{
|
397
|
+
va_list args;
|
398
|
+
|
399
|
+
if (!p_switch) return;
|
400
|
+
|
401
|
+
va_start(args, format);
|
402
|
+
vfprintf(stderr, format, args);
|
403
|
+
va_end(args);
|
404
|
+
}
|
405
|
+
|
406
|
+
void p_on()
|
407
|
+
{
|
408
|
+
fprintf(stderr, "> > > > > STARTING PRINT\n");
|
409
|
+
p_switch = true;
|
410
|
+
}
|
411
|
+
|
412
|
+
void p_off()
|
413
|
+
{
|
414
|
+
fprintf(stderr, "< < < < < STOPPING PRINT\n");
|
415
|
+
p_switch = false;
|
416
|
+
}
|
417
|
+
|
418
|
+
void frt_p_pause()
|
419
|
+
{
|
420
|
+
p_switch_tmp = p_switch;
|
421
|
+
p_switch = false;
|
422
|
+
}
|
423
|
+
|
424
|
+
void frt_p_resume()
|
425
|
+
{
|
426
|
+
p_switch = p_switch_tmp;
|
427
|
+
}
|
@@ -0,0 +1,290 @@
|
|
1
|
+
#ifndef FRT_GLOBAL_H
|
2
|
+
#define FRT_GLOBAL_H
|
3
|
+
|
4
|
+
#include "frt_config.h"
|
5
|
+
#include "frt_except.h"
|
6
|
+
#include "frt_lang.h"
|
7
|
+
#include <stdlib.h>
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <stdarg.h>
|
10
|
+
#include <assert.h>
|
11
|
+
#include <string.h>
|
12
|
+
#include <ruby.h>
|
13
|
+
|
14
|
+
typedef ID FrtSymbol;
|
15
|
+
|
16
|
+
#define FRT_MAX_WORD_SIZE 255
|
17
|
+
#define FRT_MAX_FILE_PATH 1024
|
18
|
+
#define FRT_BUFFER_SIZE 1024
|
19
|
+
|
20
|
+
#define FRT_DBL2S "%#.7g"
|
21
|
+
|
22
|
+
#if __GNUC__ >= 3
|
23
|
+
# define FRT_ATTR_ALWAYS_INLINE inline __attribute__ ((always_inline))
|
24
|
+
# define FRT_ATTR_MALLOC __attribute__ ((malloc))
|
25
|
+
# define FRT_ATTR_CONST __attribute__ ((const))
|
26
|
+
# define likely(x) __builtin_expect(!!(x), 1)
|
27
|
+
# define unlikely(x) __builtin_expect(!!(x), 0)
|
28
|
+
#else
|
29
|
+
# define FRT_ATTR_ALWAYS_INLINE
|
30
|
+
# define FRT_ATTR_MALLOC
|
31
|
+
# define FRT_ATTR_CONST
|
32
|
+
# define likely(x) (x)
|
33
|
+
# define unlikely(x) (x)
|
34
|
+
#endif
|
35
|
+
|
36
|
+
typedef void (*frt_free_ft)(void *key);
|
37
|
+
|
38
|
+
#define FRT_NELEMS(array) ((int)(sizeof(array)/sizeof(array[0])))
|
39
|
+
|
40
|
+
|
41
|
+
#define FRT_ZEROSET(ptr, type) memset(ptr, 0, sizeof(type))
|
42
|
+
#define FRT_ZEROSET_N(ptr, type, n) memset(ptr, 0, sizeof(type)*(n))
|
43
|
+
|
44
|
+
#define FRT_ALLOC_AND_ZERO(type) (type*)frt_ecalloc(sizeof(type))
|
45
|
+
#define FRT_ALLOC_AND_ZERO_N(type,n) (type*)frt_ecalloc(sizeof(type)*(n))
|
46
|
+
|
47
|
+
#define FRT_REF(a) (a)->ref_cnt++
|
48
|
+
#define FRT_DEREF(a) (a)->ref_cnt--
|
49
|
+
|
50
|
+
#define FRT_NEXT_NUM(index, size) (((index) + 1) % (size))
|
51
|
+
#define FRT_PREV_NUM(index, size) (((index) + (size) - 1) % (size))
|
52
|
+
|
53
|
+
#define FRT_MIN(a, b) ((a) < (b) ? (a) : (b))
|
54
|
+
#define FRT_MAX(a, b) ((a) > (b) ? (a) : (b))
|
55
|
+
|
56
|
+
#define FRT_MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c)))
|
57
|
+
#define FRT_MAX3(a, b, c) ((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
|
58
|
+
|
59
|
+
#define FRT_ABS(n) ((n >= 0) ? n : -n)
|
60
|
+
#define FRT_TO_WORD(n) (((n - 1) >> 5) + 1)
|
61
|
+
|
62
|
+
#define FRT_RECAPA(self, len, capa, ptr, type) \
|
63
|
+
do {\
|
64
|
+
if (self->len >= self->capa) {\
|
65
|
+
if (self->capa > 0) {\
|
66
|
+
self->capa <<= 1;\
|
67
|
+
} else {\
|
68
|
+
self->capa = 4;\
|
69
|
+
}\
|
70
|
+
self->ptr = (type *)frt_erealloc(self->ptr, sizeof(type) * self->capa);\
|
71
|
+
}\
|
72
|
+
} while (0)
|
73
|
+
|
74
|
+
#if defined POSH_OS_WIN32 || defined POSH_OS_WIN64
|
75
|
+
# define Jx fprintf(stderr,"%s, %d\n", __FILE__, __LINE__);
|
76
|
+
# define Xj fprintf(stdout,"%s, %d\n", __FILE__, __LINE__);
|
77
|
+
#else
|
78
|
+
# define Jx fprintf(stderr,"%s, %d: %s\n", __FILE__, __LINE__, __func__);
|
79
|
+
# define Xj fprintf(stdout,"%s, %d: %s\n", __FILE__, __LINE__, __func__);
|
80
|
+
#endif
|
81
|
+
|
82
|
+
extern unsigned int *frt_imalloc(unsigned int value);
|
83
|
+
extern unsigned long *frt_lmalloc(unsigned long value);
|
84
|
+
extern frt_u32 *frt_u32malloc(frt_u32 value);
|
85
|
+
extern frt_u64 *frt_u64malloc(frt_u64 value);
|
86
|
+
|
87
|
+
extern char *frt_estrdup(const char *s);
|
88
|
+
extern char *frt_estrcat(char *str, char *str_cat);
|
89
|
+
extern char *frt_epstrdup(const char *fmt, int len, ...);
|
90
|
+
|
91
|
+
extern char *frt_strapp(char *dst, const char *src);
|
92
|
+
|
93
|
+
extern const char *FRT_EMPTY_STRING;
|
94
|
+
|
95
|
+
extern int frt_scmp(const void *p1, const void *p2);
|
96
|
+
extern int frt_icmp(const void *p1, const void *p2);
|
97
|
+
extern int frt_icmp_risky(const void *p1, const void *p2);
|
98
|
+
extern void frt_strsort(char **string_array, int size);
|
99
|
+
|
100
|
+
extern char *frt_dbl_to_s(char *buf, double num);
|
101
|
+
extern char *frt_strfmt(const char *fmt, ...);
|
102
|
+
extern char *frt_vstrfmt(const char *fmt, va_list args);
|
103
|
+
|
104
|
+
extern char *frt_get_stacktrace();
|
105
|
+
extern void frt_print_stacktrace();
|
106
|
+
|
107
|
+
extern void frt_register_for_cleanup(void *p, frt_free_ft free_func);
|
108
|
+
|
109
|
+
/**
|
110
|
+
* A dummy function which can be passed to functions which expect a free
|
111
|
+
* function such as frt_h_new() if you don't want the free functions to do anything.
|
112
|
+
* This function will do nothing.
|
113
|
+
*
|
114
|
+
* @param p the object which this function will be called on.
|
115
|
+
*/
|
116
|
+
extern void frt_dummy_free(void *p);
|
117
|
+
|
118
|
+
/**
|
119
|
+
* Returns the count of leading [MSB] 0 bits in +word+.
|
120
|
+
*/
|
121
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
122
|
+
int frt_count_leading_zeros(frt_u32 word)
|
123
|
+
{
|
124
|
+
#ifdef __GNUC__
|
125
|
+
if (word)
|
126
|
+
return __builtin_clz(word);
|
127
|
+
return 32;
|
128
|
+
#else
|
129
|
+
static const int count_leading_zeros[] = {
|
130
|
+
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
|
131
|
+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
132
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
133
|
+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
134
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
135
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
136
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
137
|
+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
138
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
139
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
140
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
141
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
142
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
143
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
144
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
145
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
146
|
+
};
|
147
|
+
if (word & 0xff) return count_leading_zeros[word & 0xff];
|
148
|
+
word >>= 8; if (word & 0xff) return count_leading_zeros[word & 0xff] + 8;
|
149
|
+
word >>= 8; if (word & 0xff) return count_leading_zeros[word & 0xff] + 16;
|
150
|
+
word >>= 8; return count_leading_zeros[word & 0xff] + 24;
|
151
|
+
#endif
|
152
|
+
}
|
153
|
+
|
154
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
155
|
+
int frt_count_leading_ones(frt_u32 word)
|
156
|
+
{
|
157
|
+
return frt_count_leading_zeros(~word);
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Return the count of trailing [LSB] 0 bits in +word+.
|
162
|
+
*/
|
163
|
+
|
164
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
165
|
+
int frt_count_trailing_zeros(frt_u32 word)
|
166
|
+
{
|
167
|
+
#ifdef __GNUC__
|
168
|
+
if (word)
|
169
|
+
return __builtin_ctz(word);
|
170
|
+
return 32;
|
171
|
+
#else
|
172
|
+
static const int count_trailing_zeros[] = {
|
173
|
+
8, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
174
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
175
|
+
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
176
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
177
|
+
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
178
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
179
|
+
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
180
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
181
|
+
7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
182
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
183
|
+
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
184
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
185
|
+
6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
186
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
187
|
+
5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
|
188
|
+
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
|
189
|
+
};
|
190
|
+
if (word & 0xff) return count_trailing_zeros[word & 0xff];
|
191
|
+
word >>= 8; if (word & 0xff) return count_trailing_zeros[word & 0xff] + 8;
|
192
|
+
word >>= 8; if (word & 0xff) return count_trailing_zeros[word & 0xff] + 16;
|
193
|
+
word >>= 8; return count_trailing_zeros[word & 0xff] + 24;
|
194
|
+
#endif
|
195
|
+
}
|
196
|
+
|
197
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
198
|
+
int frt_count_trailing_ones(frt_u32 word)
|
199
|
+
{
|
200
|
+
return frt_count_trailing_zeros(~word);
|
201
|
+
}
|
202
|
+
|
203
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
204
|
+
int frt_count_ones(frt_u32 word)
|
205
|
+
{
|
206
|
+
#ifdef __GNUC__
|
207
|
+
return __builtin_popcount(word);
|
208
|
+
#else
|
209
|
+
static const frt_uchar count_ones[] = {
|
210
|
+
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
|
211
|
+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
212
|
+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
213
|
+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
214
|
+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
215
|
+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
216
|
+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
217
|
+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
218
|
+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
|
219
|
+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
220
|
+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
221
|
+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
222
|
+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
|
223
|
+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
224
|
+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
|
225
|
+
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
|
226
|
+
};
|
227
|
+
return count_ones[(word ) & 0xff]
|
228
|
+
+ count_ones[(word >> 8 ) & 0xff]
|
229
|
+
+ count_ones[(word >> 16) & 0xff]
|
230
|
+
+ count_ones[(word >> 24) & 0xff];
|
231
|
+
#endif
|
232
|
+
}
|
233
|
+
|
234
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
235
|
+
int frt_count_zeros(frt_u32 word)
|
236
|
+
{
|
237
|
+
return frt_count_ones(~word);
|
238
|
+
}
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Round up to the next power of 2
|
242
|
+
*/
|
243
|
+
static FRT_ATTR_ALWAYS_INLINE FRT_ATTR_CONST
|
244
|
+
int frt_round2(frt_u32 word)
|
245
|
+
{
|
246
|
+
return 1 << (32 - frt_count_leading_zeros(word));
|
247
|
+
}
|
248
|
+
|
249
|
+
/**
|
250
|
+
* For coverage, we don't want FRT_XEXIT to actually exit on uncaught
|
251
|
+
* exceptions. +frt_x_abort_on_exception+ is +true+ by default, set it to
|
252
|
+
* +false+, and +frt_x_has_aborted+ will be set as appropriate. We also
|
253
|
+
* don't want spurious errors to be printed out to stderr, so we give
|
254
|
+
* the option to set where errors go to with +frt_x_exception_stream+.
|
255
|
+
*/
|
256
|
+
|
257
|
+
extern bool frt_x_abort_on_exception;
|
258
|
+
extern bool frt_x_has_aborted;
|
259
|
+
extern FILE *frt_x_exception_stream;
|
260
|
+
|
261
|
+
/**
|
262
|
+
* The convenience macro +EXCEPTION_STREAM+ returns stderr when
|
263
|
+
* +frt_x_exception_stream+ isn't explicitely set.
|
264
|
+
*/
|
265
|
+
#define EXCEPTION 2
|
266
|
+
#define EXCEPTION_STREAM (frt_x_exception_stream ? frt_x_exception_stream : stderr)
|
267
|
+
|
268
|
+
#ifdef DEBUG
|
269
|
+
extern bool frt_x_do_logging;
|
270
|
+
#define xlog if (frt_x_do_logging) printf
|
271
|
+
#else
|
272
|
+
#define xlog()
|
273
|
+
#endif
|
274
|
+
|
275
|
+
extern void frt_init(int arc, const char *const argv[]);
|
276
|
+
extern void frt_setprogname(const char *str);
|
277
|
+
extern const char *frt_progname();
|
278
|
+
extern void frt_micro_sleep(const int micro_seconds);
|
279
|
+
|
280
|
+
/**
|
281
|
+
* For general use during testing. Switch this on and off for print statements
|
282
|
+
* to only print when p_on is called and not after p_off is called
|
283
|
+
*/
|
284
|
+
extern void p(const char *format, ...);
|
285
|
+
extern void p_on();
|
286
|
+
extern void p_off();
|
287
|
+
extern void frt_p_pause();
|
288
|
+
extern void frt_p_resume();
|
289
|
+
|
290
|
+
#endif
|