Redis 中利用 sizeof 从 sds 转为 sdshdr

php中文网
发布: 2016-06-07 16:41:32
原创
2019人浏览过

今天在阅读 redis 源码时看到 sds.h 中的 sdslen 和 sdsavail 中看到一行语句不是很理解 struct sdshdr * sh = ( void * )( s - ( sizeof ( struct sdshdr ))); 这里 s 是 const sds 类型,相关类型定义如下 typedef char * sds ; struct sdshdr { unsigned

今天在阅读 redis 源码时看到 sds.h 中的 sdslen 和 sdsavail 中看到一行语句不是很理解

<span class="k">struct</span> <span class="n">sdshdr</span> <span class="o">*</span><span class="n">sh</span> <span class="o">=</span> <span class="p">(</span><span class="kt">void</span><span class="o">*</span><span class="p">)(</span><span class="n">s</span><span class="o">-</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">sdshdr</span><span class="p">)));</span>
登录后复制

这里 s 是 const sds 类型,相关类型定义如下

<span class="k">typedef</span> <span class="kt">char</span> <span class="o">*</span><span class="n">sds</span><span class="p">;</span>
<span class="k">struct</span> <span class="n">sdshdr</span> <span class="p">{</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">len</span><span class="p">;</span>
    <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">free</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">buf</span><span class="p">[];</span>
<span class="p">};</span>
登录后复制

当使用 sdsnewlen(const void *init, size_t initlen) 创建一个新的动态字符串时,Redis 会创建一个 struct sdshdr *sh 结构体,并将这个结构体的最后一个字符数组,也就是 sh->buf 返回,这样返回值是个普通的 C 字符串,因此便可以使用一些 string.h 和 stdlib.h 中的 C 字符串函数,而当想访问 sds 的 len 和 free 属性时,就需要先转成 struct sdshdr 结构。但是直接用 s-(sizeof(struct sdshdr)) 却是不知道怎么回事。

后来直接使用那一行代码进行 Google,然后找到了结果。

C99 中的标准规定:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

struct sdshdr 结构体中的最后一个 char buf[] 被称为 flexible array member,在计算结构体大小的时候是不记入在内的,因此 sizeof(struct sdshdr) 实际上就是 sizeof(unsigned int) + sizeof(unsigned int) 这样就能理解了。

-----------
|5|0|redis|
-----------
^   ^
sh  sh->buf
登录后复制

所谓的 sizeof(struct sdshdr) 实际上是就是 len 和 free 所占的大小,因此用 sh->buf 的位置减去 sizeof(struct sdshdr) 就是 sh 的位置了,再经过 struct sdshdr * 转换,就可以得到 sds 对应的 struct sdshdr 结构体了。

参考资料:

  • Hacking Strings
  • redis sds数据结构的指针技巧
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号