aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKarl Heuer1997-12-09 23:01:27 +0000
committerKarl Heuer1997-12-09 23:01:27 +0000
commit320a2a7349b5a682b22ab68f1d8dfcedbae17b88 (patch)
tree07487e7d1cf12ccb9625f0ced009dacaac530b37 /src
parentdb54baaa69641fe652ab660748428fca4c7e8346 (diff)
downloademacs-320a2a7349b5a682b22ab68f1d8dfcedbae17b88.tar.gz
emacs-320a2a7349b5a682b22ab68f1d8dfcedbae17b88.zip
(TYPICAL_FAILURE_SIZE): Renamed from MAX_FAILURE_ITEMS.
Define it simply as a number. (DOUBLE_FAIL_STACK, regex_compile): Set the limit at the size TYPICAL_FAILURE_SIZE specifies, rather than at twice that much. (re_max_failures): Double the initial values. (INIT_FAIL_STACK): Use TYPICAL_FAILURE_SIZE so that INIT_FAILURE_ALLOC counts in the proper units. (INIT_FAILURE_ALLOC): Increase to 20. (FAIL_STACK_GROWTH_FACTOR): New macro. (GROW_FAIL_STACK): Renamed from DOUBLE_FAIL_STACK. FAIL_STACK_GROWTH_FACTOR controls what ratio to increase size by.
Diffstat (limited to 'src')
-rw-r--r--src/regex.c70
1 files changed, 42 insertions, 28 deletions
diff --git a/src/regex.c b/src/regex.c
index 5a98af0678e..49ec073b892 100644
--- a/src/regex.c
+++ b/src/regex.c
@@ -1120,23 +1120,25 @@ static const char *re_error_msgid[] =
1120 REGEX_ALLOCATE_STACK. */ 1120 REGEX_ALLOCATE_STACK. */
1121 1121
1122 1122
1123/* Number of failure points for which to initially allocate space 1123/* Approximate number of failure points for which to initially allocate space
1124 when matching. If this number is exceeded, we allocate more 1124 when matching. If this number is exceeded, we allocate more
1125 space, so it is not a hard limit. */ 1125 space, so it is not a hard limit. */
1126#ifndef INIT_FAILURE_ALLOC 1126#ifndef INIT_FAILURE_ALLOC
1127#define INIT_FAILURE_ALLOC 5 1127#define INIT_FAILURE_ALLOC 20
1128#endif 1128#endif
1129 1129
1130/* Roughly the maximum number of failure points on the stack. Would be 1130/* Roughly the maximum number of failure points on the stack. Would be
1131 exactly that if always used MAX_FAILURE_ITEMS items each time we failed. 1131 exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
1132 This is a variable only so users of regex can assign to it; we never 1132 This is a variable only so users of regex can assign to it; we never
1133 change it ourselves. */ 1133 change it ourselves. */
1134#if defined (MATCH_MAY_ALLOCATE) 1134#if defined (MATCH_MAY_ALLOCATE)
1135/* 4400 was enough to cause a crash on Alpha OSF/1, 1135/* Note that 4400 is enough to cause a crash on Alpha OSF/1,
1136 whose default stack limit is 2mb. */ 1136 whose default stack limit is 2mb. In order for a larger
1137int re_max_failures = 20000; 1137 value to work reliably, you have to try to make it accord
1138 with the process stack limit. */
1139int re_max_failures = 40000;
1138#else 1140#else
1139int re_max_failures = 2000; 1141int re_max_failures = 4000;
1140#endif 1142#endif
1141 1143
1142union fail_stack_elt 1144union fail_stack_elt
@@ -1166,7 +1168,8 @@ typedef struct
1166#define INIT_FAIL_STACK() \ 1168#define INIT_FAIL_STACK() \
1167 do { \ 1169 do { \
1168 fail_stack.stack = (fail_stack_elt_t *) \ 1170 fail_stack.stack = (fail_stack_elt_t *) \
1169 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \ 1171 REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
1172 * sizeof (fail_stack_elt_t)); \
1170 \ 1173 \
1171 if (fail_stack.stack == NULL) \ 1174 if (fail_stack.stack == NULL) \
1172 return -2; \ 1175 return -2; \
@@ -1186,24 +1189,37 @@ typedef struct
1186#endif 1189#endif
1187 1190
1188 1191
1189/* Double the size of FAIL_STACK, up to approximately `re_max_failures' items. 1192/* Double the size of FAIL_STACK, up to a limit
1193 which allows approximately `re_max_failures' items.
1190 1194
1191 Return 1 if succeeds, and 0 if either ran out of memory 1195 Return 1 if succeeds, and 0 if either ran out of memory
1192 allocating space for it or it was already too large. 1196 allocating space for it or it was already too large.
1193 1197
1194 REGEX_REALLOCATE_STACK requires `destination' be declared. */ 1198 REGEX_REALLOCATE_STACK requires `destination' be declared. */
1195 1199
1196#define DOUBLE_FAIL_STACK(fail_stack) \ 1200/* Factor to increase the failure stack size by
1197 ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS \ 1201 when we increase it.
1202 This used to be 2, but 2 was too wasteful
1203 because the old discarded stacks added up to as much space
1204 were as ultimate, maximum-size stack. */
1205#define FAIL_STACK_GROWTH_FACTOR 4
1206
1207#define GROW_FAIL_STACK(fail_stack) \
1208 ((fail_stack).size >= re_max_failures * TYPICAL_FAILURE_SIZE \
1198 ? 0 \ 1209 ? 0 \
1199 : ((fail_stack).stack = (fail_stack_elt_t *) \ 1210 : ((fail_stack).stack \
1211 = (fail_stack_elt_t *) \
1200 REGEX_REALLOCATE_STACK ((fail_stack).stack, \ 1212 REGEX_REALLOCATE_STACK ((fail_stack).stack, \
1201 (fail_stack).size * sizeof (fail_stack_elt_t), \ 1213 (fail_stack).size * sizeof (fail_stack_elt_t), \
1202 ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \ 1214 MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1215 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1216 * FAIL_STACK_GROWTH_FACTOR))), \
1203 \ 1217 \
1204 (fail_stack).stack == NULL \ 1218 (fail_stack).stack == NULL \
1205 ? 0 \ 1219 ? 0 \
1206 : ((fail_stack).size <<= 1, \ 1220 : (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
1221 ((fail_stack).size * sizeof (fail_stack_elt_t) \
1222 * FAIL_STACK_GROWTH_FACTOR)), \
1207 1))) 1223 1)))
1208 1224
1209 1225
@@ -1212,7 +1228,7 @@ typedef struct
1212 space to do so. */ 1228 space to do so. */
1213#define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \ 1229#define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
1214 ((FAIL_STACK_FULL () \ 1230 ((FAIL_STACK_FULL () \
1215 && !DOUBLE_FAIL_STACK (FAIL_STACK)) \ 1231 && !GROW_FAIL_STACK (FAIL_STACK)) \
1216 ? 0 \ 1232 ? 0 \
1217 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \ 1233 : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
1218 1)) 1234 1))
@@ -1255,7 +1271,7 @@ typedef struct
1255 if we ever fail back to it. 1271 if we ever fail back to it.
1256 1272
1257 Requires variables fail_stack, regstart, regend, reg_info, and 1273 Requires variables fail_stack, regstart, regend, reg_info, and
1258 num_regs be declared. DOUBLE_FAIL_STACK requires `destination' be 1274 num_regs be declared. GROW_FAIL_STACK requires `destination' be
1259 declared. 1275 declared.
1260 1276
1261 Does `return FAILURE_CODE' if runs out of memory. */ 1277 Does `return FAILURE_CODE' if runs out of memory. */
@@ -1279,7 +1295,7 @@ typedef struct
1279 /* Ensure we have enough space allocated for what we will push. */ \ 1295 /* Ensure we have enough space allocated for what we will push. */ \
1280 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \ 1296 while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
1281 { \ 1297 { \
1282 if (!DOUBLE_FAIL_STACK (fail_stack)) \ 1298 if (!GROW_FAIL_STACK (fail_stack)) \
1283 return failure_code; \ 1299 return failure_code; \
1284 \ 1300 \
1285 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \ 1301 DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
@@ -1346,13 +1362,14 @@ typedef struct
1346#define NUM_NONREG_ITEMS 4 1362#define NUM_NONREG_ITEMS 4
1347#endif 1363#endif
1348 1364
1349/* We push at most this many items on the stack. */ 1365/* Estimate the size of data pushed by a typical failure stack entry.
1350/* We used to use (num_regs - 1), which is the number of registers 1366 An estimate is all we need, because all we use this for
1351 this regexp will save; but that was changed to 5 1367 is to choose a limit for how big to make the failure stack. */
1352 to avoid stack overflow for a regexp with lots of parens. */ 1368
1353#define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS) 1369#define TYPICAL_FAILURE_SIZE 20
1354 1370
1355/* We actually push this many items. */ 1371/* This is how many items we actually use for a failure point.
1372 It depends on the regexp. */
1356#define NUM_FAILURE_ITEMS \ 1373#define NUM_FAILURE_ITEMS \
1357 (((0 \ 1374 (((0 \
1358 ? 0 : highest_active_reg - lowest_active_reg + 1) \ 1375 ? 0 : highest_active_reg - lowest_active_reg + 1) \
@@ -2939,12 +2956,9 @@ regex_compile (pattern, size, syntax, bufp)
2939 { 2956 {
2940 int num_regs = bufp->re_nsub + 1; 2957 int num_regs = bufp->re_nsub + 1;
2941 2958
2942 /* Since DOUBLE_FAIL_STACK refuses to double only if the current size 2959 if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
2943 is strictly greater than re_max_failures, the largest possible stack
2944 is 2 * re_max_failures failure points. */
2945 if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
2946 { 2960 {
2947 fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS); 2961 fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE);
2948 2962
2949#ifdef emacs 2963#ifdef emacs
2950 if (! fail_stack.stack) 2964 if (! fail_stack.stack)