// File: /workspace/github_c_makefile_star2_O2/zuocheng-liu[P]ae-memcached/src/mem_cache.c
// Function: mem_cache_create
mem_cache_ptr mem_cache_create(size_t base_chunk_size, 
        double factor, 
        size_t power_largest, 
        size_t power_block, 
        size_t mem_limit, 
        int pre_alloc) {

    mem_cache_ptr mem_cache;
    slab_ptr *slabclass;
    size_t i;
    size_t chunk_size;
    if (base_chunk_size <= CHUNK_ALIGN_BYTES || 
            power_largest <= 0 || 
            power_block <= CHUNK_ALIGN_BYTES ||
            power_block < base_chunk_size ||
            mem_limit <= CHUNK_ALIGN_BYTES) {
        return NULL;
    }

    mem_cache = (mem_cache_ptr)calloc(1, sizeof(*mem_cache));
    if (NULL == mem_cache) {
        return NULL;
    }
    
    slabclass = (slab_ptr *)calloc(power_largest, sizeof(slab_ptr));
    if (NULL == slabclass) {
        free (mem_cache);
        return NULL;
    }

    mem_cache->mem_limit = mem_limit;
    mem_cache->mem_malloced = 0;
    mem_cache->factor = factor;
    mem_cache->power_block = power_block;
    mem_cache->base_chunk_size = base_chunk_size;
    mem_cache->power_smallest = 1;
    mem_cache->slabclass = slabclass;
    
    for (i = 1, chunk_size = base_chunk_size; i < power_largest; ++i) {
        if (chunk_size > power_block) {
            break;
        }
        slabclass[i] = slab_create(chunk_size, power_block, pre_alloc);
        LOG_DEBUG_F3("slab class %3lu: chunk size %6lu perslab %5u\n",
                    i, 
                    slabclass[i]->chunk_size, 
                    slabclass[i]->chunk_number_per_page);
        chunk_size *= factor;
    }
    
    mem_cache->power_largest = i - 1;
    mem_cache->mem_malloced = mem_cache->power_largest;
    
    return mem_cache;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rcx, %r12
movq %rdx, %r14
movsd %xmm0, 0x8(%rsp)
cmpq $0x9, %rdi
setb %al
testq %rsi, %rsi
sete %cl
cmpq $0x9, %rdx
setb %dl
orb %al, %dl
orb %cl, %dl
jne 0x4435
movq %rdi, %r13
cmpq %rdi, %r14
setb %al
cmpq $0x9, %r12
setb %cl
orb %al, %cl
jne 0x4435
movl %r8d, %ebx
movq %rsi, %r15
pushq $0x1
popq %rbp
pushq $0x40
popq %rsi
movq %rbp, %rdi
callq 0x2220
testq %rax, %rax
je 0x4435
movq %r12, 0x10(%rsp)
movq %rax, (%rsp)
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2220
testq %rax, %rax
je 0x442c
movq %rax, %r12
movq (%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, 0x28(%rax)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r14, 0x10(%rax)
movq %r13, 0x8(%rax)
movq $0x1, 0x18(%rax)
movq %r12, 0x38(%rax)
cmpq %r15, %rbp
jae 0x441b
cmpq %r14, %r13
ja 0x441b
movq %r13, %rdi
movq %r14, %rsi
movl %ebx, %edx
callq 0x7094
movq %rax, (%r12,%rbp,8)
leaq 0x8ee2(%rip), %rcx      # 0xd288
cmpl $0x2, 0x18(%rcx)
jge 0x43f7
movq %r13, %xmm0
punpckldq 0x3ef7(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x3eff(%rip), %xmm0     # 0x82c0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x8(%rsp), %xmm1
cvttsd2si %xmm1, %rax
movq %rax, %rcx
subsd 0x3eed(%rip), %xmm1     # 0x82d0
cvttsd2si %xmm1, %r13
sarq $0x3f, %rcx
andq %rcx, %r13
orq %rax, %r13
incq %rbp
jmp 0x437c
movq 0x8bfa(%rip), %rcx      # 0xcff8
movq (%rcx), %rdi
movq (%rax), %rcx
movl 0x10(%rax), %r8d
leaq 0x3ec9(%rip), %rsi      # 0x82d8
movq %rbp, %rdx
xorl %eax, %eax
callq 0x2260
jmp 0x43ac
decq %rbp
movq (%rsp), %rax
movq %rbp, 0x20(%rax)
movq %rbp, 0x30(%rax)
jmp 0x4437
movq (%rsp), %rdi
callq 0x2030
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq