glibc源码解读——memset
来源:互联网 发布:win10 蓝牙共享网络 编辑:程序博客网 时间:2024/06/11 06:52
#include <string.h>#include <memcopy.h>#undef memsetvoid *inhibit_loop_to_libcallmemset(void *dstpp, int c, size_t len){ long int dstp = (long int)dstpp; if (len >= 8) { size_t xlen; op_t cccc; cccc = (unsigned char)c; cccc |= cccc << 8; cccc |= cccc << 16; if (OPSIZ > 4) /* Do the shift in two steps to avoid warning if long has 32 bits. */ cccc |= (cccc << 16) << 16; /* There are at least some bytes to set. No need to test for LEN == 0 in this alignment loop. */ while (dstp % OPSIZ != 0) { ((byte *)dstp)[0] = c; dstp += 1; len -= 1; } /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */ xlen = len / (OPSIZ * 8); while (xlen > 0) { ((op_t *)dstp)[0] = cccc; ((op_t *)dstp)[1] = cccc; ((op_t *)dstp)[2] = cccc; ((op_t *)dstp)[3] = cccc; ((op_t *)dstp)[4] = cccc; ((op_t *)dstp)[5] = cccc; ((op_t *)dstp)[6] = cccc; ((op_t *)dstp)[7] = cccc; dstp += 8 * OPSIZ; xlen -= 1; } len %= OPSIZ * 8; /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */ xlen = len / OPSIZ; while (xlen > 0) { ((op_t *)dstp)[0] = cccc; dstp += OPSIZ; xlen -= 1; } len %= OPSIZ; } /* Write the last few bytes. */ while (len > 0) { ((byte *)dstp)[0] = c; dstp += 1; len -= 1; } return dstpp;}libc_hidden_builtin_def(memset)
其中部分定义
#define op_t unsigned long int#define OPSIZ(sizeof(op_t))typedef unsigned char byte;
考虑到效率的因素:
1、len < 8 的时候,按每次一个字节来读写。
2、len >= 8 的时候,32位机器按每次 4 个字节来读写,64位机器按每次 8 个字节来读写,连续做 8 次。剩下的接着按 每次 4 或 8 字节读写, 每次 1 字节读写。
疑问:
假如在32位机器下,每次取得 dstp 的地址都是 4 的倍数吗?怎么才能取得 不是 4 倍数的地址?
阅读全文
0 0
- glibc源码解读——memset
- glibc源码解读——malloc
- glibc源码
- kubernetes源码解读——源码结构
- memset源码
- memset源码
- memset函数详细解读
- nutch-2.0源码解读——简介
- Hibernate源码解读——查询
- hadoop源码解读——configuration
- hadoop源码解读——Configured
- Java源码解读——ArrayList
- OpenCV 源码解读——createsample
- opencv源码解读——haartraining
- node.js——http源码解读
- Spark源码解读(4)——RDD
- Spark源码解读(8)——累加器
- Caffe源码解读1 —— Blob
- struts2学习之---实现Action
- JFileChooser文件保存位置选择器
- MATLAB: cat函数使用
- eclipse如何debug调试jdk源码
- javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String 错误解决办法
- glibc源码解读——memset
- 注解的使用
- DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
- mac 配置maven环境变量(亲测可用)
- CSDN如何转载别人的文章
- MATLAB--对于im2bw函数的优化
- Eclipse中的Use as Source Folder的操作在Android Studio中的实现。
- 看我们如何“把大象放进冰箱里”
- java代码实现企业发放的奖金根据利润提成问题