// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: aofRewriteBufferAppend
void aofRewriteBufferAppend(unsigned char *s, unsigned long len) {

    // 指向最后一个缓存块
    listNode *ln = listLast(server.aof_rewrite_buf_blocks);
    aofrwblock *block = ln ? ln->value : NULL;

    while(len) {
        /* If we already got at least an allocated block, try appending
         * at least some piece into it. 
         *
         * 如果已经有至少一个缓存块，那么尝试将内容追加到这个缓存块里面
         */
        if (block) {
            unsigned long thislen = (block->free < len) ? block->free : len;
            if (thislen) {  /* The current block is not already full. */
                memcpy(block->buf+block->used, s, thislen);
                block->used += thislen;
                block->free -= thislen;
                s += thislen;
                len -= thislen;
            }
        }

        // 如果 block != NULL ，那么这里是创建另一个缓存块买容纳 block 装不下的内容
        // 如果 block == NULL ，那么这里是创建缓存链表的第一个缓存块
        if (len) { /* First block to allocate, or need another block. */
            int numblocks;

            // 分配缓存块
            block = zmalloc(sizeof(*block));
            block->free = AOF_RW_BUF_BLOCK_SIZE;
            block->used = 0;

            // 链接到链表末尾
            listAddNodeTail(server.aof_rewrite_buf_blocks,block);

            /* Log every time we cross more 10 or 100 blocks, respectively
             * as a notice or warning. 
             *
             * 每次创建 10 个缓存块就打印一个日志，用作标记或者提醒
             */
            numblocks = listLength(server.aof_rewrite_buf_blocks);
            if (((numblocks+1) % 10) == 0) {
                int level = ((numblocks+1) % 100) == 0 ? REDIS_WARNING :
                                                         REDIS_NOTICE;
                redisLog(level,"Background AOF buffer size: %lu MB",
                    aofRewriteBufferSize()/(1024*1024));
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x81572(%rip), %rax     # 0xc6fa8
movq 0x508(%rax), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x45a4c
movq 0x10(%rax), %r15
jmp 0x45a4f
xorl %r15d, %r15d
leaq 0x10(%r15), %r14
movq %r12, %rbp
testq %rbp, %rbp
je 0x45b20
testq %r15, %r15
je 0x45a9e
movq 0x8(%r15), %r13
testq %r13, %r13
je 0x45a9e
cmpq %rbp, %r13
cmovaeq %rbp, %r13
movq (%r15), %rdi
addq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x1fbd0
addq %r13, (%r15)
subq %r13, 0x8(%r15)
addq %r13, %rbx
movq %rbp, %r12
movl $0x0, %ebp
subq %r13, %r12
je 0x45a56
jmp 0x45aa1
movq %rbp, %r12
movl $0xa00010, %edi         # imm = 0xA00010
callq 0x2828d
movq %rax, %r15
movq $0xa00000, 0x8(%rax)    # imm = 0xA00000
andq $0x0, (%rax)
leaq 0x814e7(%rip), %r14     # 0xc6fa8
movq 0x508(%r14), %rdi
movq %rax, %rsi
callq 0x1fe4c
movq 0x508(%r14), %rax
movl 0x28(%rax), %ecx
incl %ecx
movl %ecx, %eax
cltd
pushq $0xa
popq %rsi
idivl %esi
testl %edx, %edx
jne 0x45a4f
movl %ecx, %eax
cltd
pushq $0x64
popq %rcx
idivl %ecx
cmpl $0x1, %edx
movl $0x0, %ebp
adcl $0x2, %ebp
callq 0x459e6
shrq $0x14, %rax
movl %ebp, %edi
leaq 0x4aabf(%rip), %rsi     # 0x905d0
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
jmp 0x45a4f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq