summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--dln.c47
2 files changed, 30 insertions, 22 deletions
diff --git a/ChangeLog b/ChangeLog
index ec53ed9..87dfcc4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Nov 9 21:57:45 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * dln.c (init_funcname): allocate and build initialization
+ funciton name at once.
+
Tue Nov 9 21:14:54 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* configure.in (AC_FUNC_GETPGRP, AC_FUNC_SETPGRP): no need when
diff --git a/dln.c b/dln.c
index 4e7addd..aa9f029 100644
--- a/dln.c
+++ b/dln.c
@@ -107,43 +107,46 @@ dln_loaderror(const char *format, ...)
#ifndef FUNCNAME_PATTERN
# if defined(__hp9000s300) || ((defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)) && !defined(__ELF__)) || defined(__BORLANDC__) || defined(NeXT) || defined(__WATCOMC__) || defined(MACOSX_DYLD)
-# define FUNCNAME_PATTERN "_Init_%s"
+# define FUNCNAME_PREFIX "_Init_"
# else
-# define FUNCNAME_PATTERN "Init_%s"
+# define FUNCNAME_PREFIX "Init_"
# endif
#endif
+#if defined __CYGWIN__ || defined DOSISH
+#define isdirsep(x) ((x) == '/' || (x) == '\\')
+#else
+#define isdirsep(x) ((x) == '/')
+#endif
+
static size_t
-init_funcname_len(char **buf, const char *file)
+init_funcname_len(const char **file)
{
- char *p;
- const char *slash;
- size_t len;
+ const char *p = *file, *base, *dot = NULL;
/* Load the file as an object one */
- for (slash = file-1; *file; file++) /* Find position of last '/' */
- if (*file == '/') slash = file;
-
- len = strlen(FUNCNAME_PATTERN) + strlen(slash + 1);
- *buf = xmalloc(len);
- snprintf(*buf, len, FUNCNAME_PATTERN, slash + 1);
- for (p = *buf; *p; p++) { /* Delete suffix if it exists */
- if (*p == '.') {
- *p = '\0'; break;
- }
+ for (base = p; *p; p++) { /* Find position of last '/' */
+ if (*p == '.') dot = p;
+ if (isdirsep(*p)) base = p+1;
}
- return p - *buf;
+ *file = base;
+ /* Delete suffix if it exists */
+ return (dot && dot > base ? dot : p) - base;
}
+static const char funcname_prefix[sizeof(FUNCNAME_PREFIX) - 1] = FUNCNAME_PREFIX;
+
#define init_funcname(buf, file) do {\
- size_t len = init_funcname_len(buf, file);\
- char *tmp = ALLOCA_N(char, len+1);\
+ const char *base = file;\
+ const size_t flen = init_funcname_len(&base);\
+ const size_t plen = sizeof(funcname_prefix);\
+ char *const tmp = ALLOCA_N(char, plen+flen+1);\
if (!tmp) {\
- free(*buf);\
dln_memerror();\
}\
- strlcpy(tmp, *buf, len + 1);\
- free(*buf);\
+ memcpy(tmp, funcname_prefix, plen);\
+ memcpy(tmp+plen, base, flen);\
+ tmp[plen+flen] = '\0';\
*buf = tmp;\
} while (0)