From 13317a1500bca309ba5323f0acc0cdce60e2bf5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=BB=A4=E7=AB=A5=E9=9E=8B?= Date: Fri, 9 Dec 2022 23:29:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ziplist=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E9=83=A8=E5=88=86=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ziplist/readme.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ziplist/readme.md b/ziplist/readme.md index 6929d9f..2e81a02 100644 --- a/ziplist/readme.md +++ b/ziplist/readme.md @@ -305,6 +305,7 @@ int zipPrevLenByteDiff(unsigned char *p, unsigned int len) { } ``` +### 数据复制 内存空间分配使用`memmove`函数实现: ```c memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff); @@ -318,10 +319,15 @@ if (forcelarge) else zipStorePrevEntryLength(p+reqlen,reqlen); ``` - -### 数据复制 - - +维护 zltail, 将新插入的节点长度算到zltail里面,如果 nextdiff == 4 > 0,说明后个节点的 prevSize 其实变大了的, +也需要把这部分增加落实到 zltail 算在偏移量里,这样才能真的对齐表尾节点 +```c +ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen); +assert(zipEntrySafe(zl, newlen, p+reqlen, &tail, 1)); +if (p[reqlen+tail.headersize+tail.len] != ZIP_END) { + ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff); +} +``` ## 删除元素