s[rarest], (UV)rarest));
}
-/* If SvTAIL(littlestr), it has a fake '\n' at end. */
-/* If SvTAIL is actually due to \Z or \z, this gives false positives
- if multiline */
/*
=for apidoc fbm_instr
Returns the location of the SV in the string delimited by C<big> and
-C<bigend>. It returns C<NULL> if the string can't be found. The C<sv>
+C<bigend> (C<bigend>) is the char following the last char).
+It returns C<NULL> if the string can't be found. The C<sv>
does not have to be C<fbm_compiled>, but the search will not be as fast
then.
=cut
+
+If SvTAIL(littlestr) is true, a fake "\n" was appended to to the string
+during FBM compilation due to FBMcf_TAIL in flags. It indicates that
+the littlestr must be anchored to the end of bigstr (or to any \n if
+FBMrf_MULTILINE).
+
+E.g. The regex compiler would compile /abc/ to a littlestr of "abc",
+while /abc$/ compiles to "abc\n" with SvTAIL() true.
+
+A littlestr of "abc", !SvTAIL matches as /abc/;
+a littlestr of "ab\n", SvTAIL matches as:
+ without FBMrf_MULTILINE: /ab\n?\z/
+ with FBMrf_MULTILINE: /ab\n/ || /ab\z/;
+
+(According to Ilya from 1999; I don't know if this is still true, DAPM 2015):
+ "If SvTAIL is actually due to \Z or \z, this gives false positives
+ if multiline".
*/
+
char *
Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags)
{
switch (littlelen) { /* Special cases for 0, 1 and 2 */
case 0:
return (char*)big; /* Cannot be SvTAIL! */
+
case 1:
- if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */
- /* Know that bigend != big. */
- if (bigend[-1] == '\n')
- return (char *)(bigend - 1);
- return (char *) bigend;
- }
- s = big;
- while (s < bigend) {
- if (*s == *little)
- return (char *)s;
- s++;
- }
+ if (SvTAIL(littlestr) && !multiline) /* Anchor only! */
+ /* [-1] is safe because we know that bigend != big. */
+ return (char *) (bigend - (bigend[-1] == '\n'));
+
+ s = (unsigned char *)memchr((void*)big, *little, bigend-big);
+ if (s)
+ return (char *)s;
if (SvTAIL(littlestr))
return (char *) bigend;
return NULL;
+
case 2:
if (SvTAIL(littlestr) && !multiline) {
- if (bigend[-1] == '\n' && bigend[-2] == *little)
+ /* a littlestr with SvTAIL must be of the form "X\n" (where X
+ * is a single char). It is anchored, and can only match
+ * "....X\n" or "....X" */
+ if (bigend[-2] == *little && bigend[-1] == '\n')
return (char*)bigend - 2;
if (bigend[-1] == *little)
return (char*)bigend - 1;
return NULL;
}
+
{
- /* This should be better than FBM if c1 == c2, and almost
- as good otherwise: maybe better since we do less indirection.
- And we save a lot of memory by caching no table. */
- const unsigned char c1 = little[0];
- const unsigned char c2 = little[1];
-
- s = big + 1;
- bigend--;
- if (c1 != c2) {
- while (s <= bigend) {
- if (s[0] == c2) {
- if (s[-1] == c1)
- return (char*)s - 1;
- s += 2;
- continue;
- }
- next_chars:
- if (s[0] == c1) {
- if (s == bigend)
- goto check_1char_anchor;
- if (s[1] == c2)
- return (char*)s;
- else {
- s++;
- goto next_chars;
- }
- }
- else
- s += 2;
- }
- goto check_1char_anchor;
- }
- /* Now c1 == c2 */
- while (s <= bigend) {
- if (s[0] == c1) {
- if (s[-1] == c1)
- return (char*)s - 1;
- if (s == bigend)
- goto check_1char_anchor;
- if (s[1] == c1)
- return (char*)s;
- s += 3;
- }
- else
- s += 2;
- }
- }
- check_1char_anchor: /* One char and anchor! */
- if (SvTAIL(littlestr) && (*bigend == *little))
- return (char *)bigend; /* bigend is already decremented. */
- return NULL;
+ /* memchr() is likely to be very fast, possibly using whatever
+ * hardware support is available, such as checking a whole
+ * cache line in one instruction.
+ * So for a 2 char pattern, calling memchr() is likely to be
+ * faster than running FBM, or rolling our own. The previous
+ * version of this code was roll-your-own which typically
+ * only needed to read every 2nd char, which was good back in
+ * the day, but no longer.
+ */
+ unsigned char c1 = little[0];
+ unsigned char c2 = little[1];
+
+ /* *** for all this case, bigend points to the last char,
+ * not the trailing \0: this makes the conditions slightly
+ * simpler */
+ bigend--;
+ s = big;
+ if (c1 != c2) {
+ while (s < bigend) {
+ /* do a quick test for c1 before calling memchr();
+ * this avoids the expensive fn call overhead when
+ * there are lots of c1's */
+ if (LIKELY(*s != c1)) {
+ s++;
+ s = (unsigned char *)memchr((void*)s, c1, bigend - s);
+ if (!s)
+ break;
+ }
+ if (s[1] == c2)
+ return (char*)s;
+
+ /* failed; try searching for c2 this time; that way
+ * we don't go pathologically slow when the string
+ * consists mostly of c1's or vice versa.
+ */
+ s += 2;
+ if (s > bigend)
+ break;
+ s = (unsigned char *)memchr((void*)s, c2, bigend - s + 1);
+ if (!s)
+ break;
+ if (s[-1] == c1)
+ return (char*)s - 1;
+ }
+ }
+ else {
+ /* c1, c2 the same */
+ while (s < bigend) {
+ if (s[0] == c1) {
+ got_1char:
+ if (s[1] == c1)
+ return (char*)s;
+ s += 2;
+ }
+ else {
+ s++;
+ s = (unsigned char *)memchr((void*)s, c1, bigend - s);
+ if (!s || s >= bigend)
+ break;
+ goto got_1char;
+ }
+ }
+ }
+
+ /* failed to find 2 chars; try anchored match at end without
+ * the \n */
+ if (SvTAIL(littlestr) && bigend[0] == little[0])
+ return (char *)bigend;
+ return NULL;
+ }
+
default:
break; /* Only lengths 0 1 and 2 have special-case code. */
}
}
return NULL;
}
+
if (!SvVALID(littlestr)) {
+ /* not compiled; use Perl_ninstr() instead */
char * const b = ninstr((char*)big,(char*)bigend,
(char*)little, (char*)little + littlelen);
oldlittle = little;
if (s < bigend) {
const unsigned char * const table = (const unsigned char *) mg->mg_ptr;
+ const unsigned char lastc = *little;
I32 tmp;
top2:
if ((tmp = table[*s])) {
- if ((s += tmp) < bigend)
- goto top2;
- goto check_end;
+ /* *s != lastc; earliest position it could match now is
+ * tmp slots further on */
+ if ((s += tmp) >= bigend)
+ goto check_end;
+ if (LIKELY(*s != lastc)) {
+ s++;
+ s = (unsigned char *)memchr((void*)s, lastc, bigend - s);
+ if (!s) {
+ s = bigend;
+ goto check_end;
+ }
+ goto top2;
+ }
}
- else { /* less expensive than calling strncmp() */
+
+
+ /* hand-rolled strncmp(): less expensive than calling the
+ * real function (maybe???) */
+ {
unsigned char * const olds = s;
tmp = littlelen;
}
}
+
/*
=for apidoc foldEQ
Configure doesn't test for that yet. For Solaris, setenv() and unsetenv()
were introduced in Solaris 9, so testing for HAS UNSETENV is sufficient.
*/
-# if defined(__CYGWIN__)|| defined(__SYMBIAN32__) || defined(__riscos__) || (defined(__sun) && defined(HAS_UNSETENV))
+# if defined(__CYGWIN__)|| defined(__SYMBIAN32__) || defined(__riscos__) || (defined(__sun) && defined(HAS_UNSETENV)) || defined(PERL_DARWIN)
# if defined(HAS_UNSETENV)
if (val == NULL) {
(void)unsetenv(nam);
/* this is called in parent before the fork() */
void
Perl_atfork_lock(void)
+#if defined(USE_ITHREADS)
+# ifdef USE_PERLIO
+ PERL_TSA_ACQUIRE(PL_perlio_mutex)
+# endif
+# ifdef MYMALLOC
+ PERL_TSA_ACQUIRE(PL_malloc_mutex)
+# endif
+ PERL_TSA_ACQUIRE(PL_op_mutex)
+#endif
{
#if defined(USE_ITHREADS)
dVAR;
/* this is called in both parent and child after the fork() */
void
Perl_atfork_unlock(void)
+#if defined(USE_ITHREADS)
+# ifdef USE_PERLIO
+ PERL_TSA_RELEASE(PL_perlio_mutex)
+# endif
+# ifdef MYMALLOC
+ PERL_TSA_RELEASE(PL_malloc_mutex)
+# endif
+ PERL_TSA_RELEASE(PL_op_mutex)
+#endif
{
#if defined(USE_ITHREADS)
dVAR;
while (deftypes ||
(!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
{
+ Stat_t statbuf;
if (deftypes) {
deftypes = 0;
*tmpbuf = '\0';
Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p);
}
}
+ else {
+ Perl_croak(aTHX_ "Invalid number '%s' for -C option.\n", p);
+ }
}
else {
for (; *p; p++) {
va_list apc;
PERL_ARGS_ASSERT_MY_VSNPRINTF;
-#ifndef HAS_VSNPRINTF
- PERL_UNUSED_VAR(len);
-#endif
Perl_va_copy(ap, apc);
# ifdef HAS_VSNPRINTF
retval = vsnprintf(buffer, len, format, apc);
# else
+ PERL_UNUSED_ARG(len);
retval = vsprintf(buffer, format, apc);
# endif
va_end(apc);
# ifdef HAS_VSNPRINTF
retval = vsnprintf(buffer, len, format, ap);
# else
+ PERL_UNUSED_ARG(len);
retval = vsprintf(buffer, format, ap);
# endif
#endif /* #ifdef NEED_VA_COPY */
#endif /* #ifdef USE_C_BACKTRACE */
+#ifdef PERL_TSA_ACTIVE
+
+/* pthread_mutex_t and perl_mutex are typedef equivalent
+ * so casting the pointers is fine. */
+
+int perl_tsa_mutex_lock(perl_mutex* mutex)
+{
+ return pthread_mutex_lock((pthread_mutex_t *) mutex);
+}
+
+int perl_tsa_mutex_unlock(perl_mutex* mutex)
+{
+ return pthread_mutex_unlock((pthread_mutex_t *) mutex);
+}
+
+int perl_tsa_mutex_destroy(perl_mutex* mutex)
+{
+ return pthread_mutex_destroy((pthread_mutex_t *) mutex);
+}
+
+#endif
+
/*
* ex: set ts=8 sts=4 sw=4 et:
*/