Compiler

clang优化器的一个问题

| Kernel | #clang | #compiler | #optimization | #FreeBSD

📜 历史文件已不具备现实意义

今天的一个偶然的发现。FreeBSD clang version 3.6.1 (tags/RELEASE_361/final 237755) 20150525clang 3.8 2015/07/20 的版本同样有此问题。

之前, FreeBSD 上 strndup(3) 的实现是这样的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
char *
strndup(const char *str, size_t n)
{
	size_t len;
	char *copy;

	len = strnlen(str, n);
	if ((copy = malloc(len + 1)) == NULL)
		return (NULL);
	memcpy(copy, str, len);
	copy[len] = '\0';
	return (copy);
}

而 OpenBSD 上的实现,则是这样的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
char *
strndup(const char *str, size_t maxlen)
{
	char *copy;
	size_t len;

	len = strnlen(str, maxlen);
	copy = malloc(len + 1);
	if (copy != NULL) {
		(void)memcpy(copy, str, len);
		copy[len] = '\0';
	}

	return copy;
}
阅读全文…( 本文约 304 字,阅读大致需要 1 分钟 )

GNU libtool 和 FreeBSD 10

FreeBSD 开始用 10 以上的版本号有一段时间了。这个变动立刻导致了大量 ports 无法编译,多数情况下是因为开发者采用了较早版本的 libtool 导致的,具体来说,是类似下面的代码:

阅读全文…( 本文约 198 字,阅读大致需要 1 分钟 )

sizeof(void *)和sizeof(int(*)(void))

以前一直没注意过这个问题,今天在邮件列表看到 Matthew Flemming 发的邮件才知道实际上 C 标准并不保证 sizeof(void *) == sizeof(int(*)(void))。不过,几乎所有的现代系统上这个等式都是成立的。

阅读全文…( 本文约 180 字,阅读大致需要 1 分钟 )