summaryrefslogtreecommitdiff
path: root/dln.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-11-09 12:57:53 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-11-09 12:57:53 +0000
commit5ddcc93a3f9ffaeed5a78aa0fa7a847880102600 (patch)
treead5ccb4eb3194d29445c2101b32e182326ddb84d /dln.c
parent6f4bfdc11554951e00f7ab7afab10c3ecff9c688 (diff)
* dln.c (init_funcname): allocate and build initialization
funciton name at once. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29737 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dln.c')
-rw-r--r--dln.c47
1 files changed, 25 insertions, 22 deletions
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)