happyfish100 / libshmcache

libshmcache is a local cache in the share memory for multi processes. high performance due to read is lockless. libshmcache is 100+ times faster than a remote interface such as redis.
449 stars 136 forks source link

代码缺陷! #37

Open YangXin-Sheep opened 2 years ago

YangXin-Sheep commented 2 years ago

static inline struct shm_hash_entry shm_get_hentry_ptr(struct shmcache_context context, const int64_t offset) { ··· base = shmopt_get_value_segment(context, conv.segment.index & 0xFF); ··· }

static inline int64_t shm_get_hentry_offset(struct shm_hash_entry *entry) { ··· conv.segment.index = entry->memory.index.segment | 0x4000; ··· }

为什么段segment index只用了8位,代表段数最多只能是255,设置段地址|0x4000也是看不懂什么操作!

happyfish100 commented 2 years ago

union shm_hentry_offset { int64_t offset; struct { int index :16; int64_t offset :48; } segment; };

segment index是16位啊!

happyfish100 commented 2 years ago

0x4000为占位符。

YangXin-Sheep commented 2 years ago

按entry->memory.index.segment | 0x4000作为占位符,我理解低14位都是可用的,但是conv.segment.index & 0xFF这里只使用了低8位啊,说明目前只有255字节是可以用的。所以我理解0xFF是不是写错了,应该是0xFFFF,当时在阅读代码的时候一直纳闷0xFF是16位?

YangXin-Sheep commented 2 years ago

union shm_hentry_offset { int64_t offset; struct { int index :16; int64_t offset :48; } segment; };

segment index是16位啊!

段地址index 16位,offset 48位段内偏移这个是没有问题的。conv.segment.index & 0xFF这里只拿了低8位,也就是目前只支持了最大127个段,因为int有符号。

YangXin-Sheep commented 2 years ago

这里实际上分配的时候才有striping的概念,但是寻址实际还是只有段的概念。