glibc-2.19 之 strlen 实现
前几天遇到一个有意思的问题,实现strlen 不考虑线程安全:
下面是我的实现:
size_t strlen(const char* s)
{
const char* p = s;
while (*p++);
return p--s;
}
Glibc 2.19 的实现:
针对此实现,函数头部分没太明白, size_t strlen (str) const char *str; 详细情况参见Glibc 2.19, 下面的实现还是比较经典的兼顾性能和体系机构。
/* Return the length of the null-terminated string STR. Scan for
the null terminator quickly by testing four bytes at a time. */
size_t
strlen (str)
const char *str;
{
const char *char_ptr;
const unsigned long int *longword_ptr;
unsigned long int longword, himagic, lomagic; /* Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary. */
for (char_ptr = str; ((unsigned long int) char_ptr
& (sizeof (longword) - )) != ;
++char_ptr)
if (*char_ptr == '\0')
return char_ptr - str; /* All these elucidatory comments refer to 4-byte longwords,
but the theory applies equally well to 8-byte longwords. */ longword_ptr = (unsigned long int *) char_ptr; /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
the "holes." Note that there is a hole just to the left of
each byte, with an extra at the end: bits: 01111110 11111110 11111110 11111111
bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD The 1-bits make sure that carries propagate to the next 0-bit.
The 0-bits provide holes for carries to fall into. */
himagic = 0x80808080L;
lomagic = 0x01010101L;
if (sizeof (longword) > )
{
/* 64-bit version of the magic. */
/* Do the shift in two steps to avoid a warning if long has 32 bits. */
himagic = ((himagic << ) << ) | himagic;
lomagic = ((lomagic << ) << ) | lomagic;
}
if (sizeof (longword) > )
abort (); /* Instead of the traditional loop which tests each character,
we will test a longword at a time. The tricky part is testing
if *any of the four* bytes in the longword in question are zero. */
for (;;)
{
longword = *longword_ptr++; if (((longword - lomagic) & ~longword & himagic) != )
{
/* Which of the bytes was the zero? If none of them were, it was
a misfire; continue the search. */ const char *cp = (const char *) (longword_ptr - ); if (cp[] == )
return cp - str;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (sizeof (longword) > )
{
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
if (cp[] == )
return cp - str + ;
}
}
}
}
glibc-2.19 之 strlen 实现的更多相关文章
- strlen源码剖析(可查看glibc和VC的CRT源代码)
学习高效编程的有效途径之一就是阅读高手写的源代码,CRT(C/C++ Runtime Library)作为底层的函数库,实现必然高效.恰好手中就有glibc和VC的CRT源代码,于是挑了一个相对简单的 ...
- 【转】strlen源码
strlen源码剖析 学习高效编程的有效途径之一就是阅读高手写的源代码,CRT(C/C++ Runtime Library)作为底层的函数库,实现必然高效.恰好手中就有glibc和VC的CRT源代码, ...
- glibc 各版本发布时间以及内核默认glibc版本
最近有些软件要求glibc 2.14+,centos 6.x自带的版本是2.12的,特查了下glibc 各版本发布时间以及与对应的内核,如下: Complete glibc release histo ...
- glibc resolv/res_send.c getaddrinfo() buffer stack smash when dealing malformation big DNS Response Package
catalogue . 漏洞简述 . 调试环境搭建 . 漏洞利用 . 漏洞分析 . 缓解修复方案 1. 漏洞简述 0x1: 函数调用顺序 getaddrinfo (getaddrinfo.c) -&g ...
- Linux Glibc库严重安全漏洞修复方案通知(腾讯开发者社区)
如何查看当前glibc的版本号? rpm -aq | grep glibc 尊敬的用户: 您好!2015年1月28日, 腾讯云安全情报监测到LinuxGlibc库存在一处严重安全漏洞,可以 ...
- 串行FLASH文件系统FatFs---转自野火论坛
为了支持长文件名,需要用到FATFS源码中的cc936.c的两个函数ff_convert,ff_wtoupper:这里面直接用了两个大数组(127KB)来做unicode转gbk(OEM)的对照表,这 ...
- 第25章 串行FLASH文件系统FatFs
25.1 文件系统 即使读者可能不了解文件系统,读者也一定对“文件”这个概念十分熟悉.数据在PC上是以文件的形式储存在磁盘中的,这些数据的形式一般为ASCII码或二进制形式.在上一章我们已经写好了Q ...
- ACM自己之前寒假的基础总结
1.const double pi = acos(-1.0); acos:反余弦函数,需要#include<math.h>函数库,acos(-1.0)的意思就是求π的值 2.算法竞赛中,不 ...
- Csharp and Vbscript: Encryption/Decryption Functional
1 /// <summary> 2 /// 塗聚文 3 /// 20130621 4 /// 自定义字符串加密解密 5 /// < ...
随机推荐
- sql小总结2
SQL NULL 值 如果表中的某个列是可选的,那么我们可以在不向该列添加值的情况下插入新记录或更新已有的记录.这意味着该字段将以 NULL 值保存. NULL 值的处理方式与其他值不同. NULL ...
- 4.事务提交过程,交易的基本概念,Oracle交易周期,保存点savepoint,数据库的隔离级别
事务提交过程 事务 基本概念 概念:一个或者多个DML语言组成 特点:要么都成功.要么都失败 事务的隔离性:多个client同一时候操作数据库的时候.要隔离它们的操作, 否则出现:脏读 不可反 ...
- Linux查看非root流程执行
Linux查看非root流程执行 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ps -U root -u root -N PID TTY TIME CMD ...
- 左右linuxserver自己主动重启过程监控和简单的解决方案
转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog) 本周開始,新手游进行删档封測阶段,前两天表现还好,今天更新后出现几次宕机行为.比較影响玩家的測试和体验,我 ...
- I/O概述和审查操作
I/O流量可表示非常多不同种类的输入源和输出目的地.它包含一个简单的字节流,基本数据(int.boolean.double等待),本地化字符,和对象.仅是简单地传递数据,另一些流能够操作和转换数据 不 ...
- 使用Json让Java和C#沟通的方法
原文:使用Json让Java和C#沟通的方法 最近很忙啊,新项目下来了,都没时间写博客了.频率降低点,但不能不总结跟大家分享啊. 我们在项目里经常要涉及到各模块间的通信,这其中又不可避免要碰到各类语言 ...
- html转换为纯文本,支持撇号
/// <summary> /// html转换为纯文本 /// </summary> /// <param name="source">< ...
- 【百度地图API】手机浏览器抓包工具及其使用方法
原文:[百度地图API]手机浏览器抓包工具及其使用方法 摘要:为了测试地图API在手机浏览器上的性能,需要给手机浏览器设置代理.通过代理,我们可以在PC上获取到抓包数据.进而对性能做进一步分析. -- ...
- HDU 5055 Bob and math problem(结构体)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055 Problem Description Recently, Bob has been think ...
- 使用Jenkins来构建Docker容器
使用Jenkins来构建Docker容器(Ubuntu 14.04) 当开发更新了代码,提交到Gitlab上,然后由测试人员触发Jenkins,于是一个应用的新版本就被构建了.听起来貌似很简单,dua ...