#ifndef __MEM_POOL_H__
#define __MEM_POOL_H__

#define MEMORY_USE_DEFAULT         0
#define MEMORY_USE_MYMALLOC        1
#define MEMORY_USE_JEMALLOC        2

#define MEMORY_SELECT_MALLOC       MEMORY_USE_DEFAULT
#include <stddef.h>
#include <stdint.h>

void *kvs_malloc(size_t size);
void kvs_free(void *ptr);

    

#define MP_PAGE_SIZE (16*1024)
#define MP_MIN_SHIFT 3
#define MP_MAX_SHIFT 12

#define MP_NBUCKETS (MP_MAX_SHIFT - MP_MIN_SHIFT + 1)
#define MP_MAGIC        0xB10C0A11u
#define MP_LARGE_CLASS  0xFFFFu

typedef struct mp_free_node {
    struct mp_free_node* next;
} mp_free_node_t;

// (blk_hdr + user_data)
typedef struct mp_blk_hdr {
    uint32_t magic;
    uint16_t class_id;
    uint16_t reserved;
} mp_blk_hdr_t;

typedef struct mp_page {
    void* mem;
    struct mp_page* next;
} mp_page_t;

typedef struct mp_bucket {
    mp_free_node_t* free_list; /* 指向用户区 */
} mp_bucket_t;

typedef struct mp_pool {
    mp_bucket_t buckets[MP_NBUCKETS];
    mp_page_t* pages;          /* 所有申请过的页 */
} mp_pool_t;

/* 初始化/销毁 */
void  mp_init(mp_pool_t* p);
void  mp_destroy(mp_pool_t* p);

/* 分配/释放 */
void* mp_alloc(size_t size);
void  mp_free(void* ptr);

#endif