图片修改

This commit is contained in:
1iaan
2026-01-16 07:46:25 +00:00
parent 7525492ee1
commit e404554363
17 changed files with 278 additions and 72 deletions

View File

@@ -1,9 +1,10 @@
#include "mem_pool.h"
#include <stddef.h>
#include <stdlib.h>
#define JEMALLOC_NO_DEMANGLE
#include <stdio.h>
#include <jemalloc/jemalloc.h>
void *kvs_malloc(size_t size) {
#if MEMORY_SELECT_MALLOC == MEMORY_USE_DEFAULT
return malloc(size);
@@ -59,7 +60,7 @@ size_t mp_user_size_from_class(uint16_t cid) {
}
int mp_add_page(mp_pool_t* p, void* mem) {
mp_page_t* pg = (mp_page_t*)malloc(sizeof(mp_page_t));
mp_page_t* pg = (mp_page_t*)je_malloc(sizeof(mp_page_t));
if (!pg) return -1;
pg->mem = mem;
pg->next = p->pages;
@@ -68,28 +69,43 @@ int mp_add_page(mp_pool_t* p, void* mem) {
}
int mp_grow(mp_pool_t* p, uint16_t cid) {
void* page = malloc(MP_PAGE_SIZE);
void* page = je_malloc(MP_PAGE_SIZE);
if (!page) return -1;
if (mp_add_page(p, page) != 0) { free(page); return -1; }
if (mp_add_page(p, page) != 0) { je_free(page); return -1; }
// cid对应的空间大小
const size_t user_sz = mp_user_size_from_class(cid);
const size_t block_sz = sizeof(mp_blk_hdr_t) + user_sz;
const size_t n = MP_PAGE_SIZE / block_sz;
if (n == 0) return -1;
size_t n = MP_PAGE_SIZE / block_sz;
if (n == 0) { je_free(page); return -1; }
uint8_t* cur = (uint8_t*)page;
mp_blk_hdr_t* first_hdr = (mp_blk_hdr_t*)cur;
first_hdr->magic = MP_MAGIC;
first_hdr->class_id = cid;
mp_free_node_t* first_node = (mp_free_node_t*)(first_hdr + 1);
mp_free_node_t* head = first_node;
mp_free_node_t* tail = first_node;
cur += block_sz;
n--;
for (size_t i = 0; i < n; ++i) {
mp_blk_hdr_t* h = (mp_blk_hdr_t*)cur;
h->magic = MP_MAGIC;
h->class_id = cid;
mp_free_node_t* user = (mp_free_node_t*)(h + 1);
user->next = p->buckets[cid].free_list;
p->buckets[cid].free_list = user;
mp_free_node_t* node = (mp_free_node_t*)(h + 1);
tail->next = node;
tail = node;
cur += block_sz;
}
tail->next = p->buckets[cid].free_list;
p->buckets[cid].free_list = head;
return 0;
}
@@ -104,8 +120,8 @@ void mp_destroy(mp_pool_t* p){
mp_page_t* pg = p->pages;
while (pg) {
mp_page_t* nxt = pg->next;
free(pg->mem);
free(pg);
je_free(pg->mem);
je_free(pg);
pg = nxt;
}
p->pages = NULL;
@@ -119,7 +135,7 @@ void* mp_alloc(size_t size){
uint16_t cid = mp_class_id_for(size);
if (cid == MP_LARGE_CLASS) {
mp_blk_hdr_t* h = (mp_blk_hdr_t*)malloc(sizeof(mp_blk_hdr_t) + size);
mp_blk_hdr_t* h = (mp_blk_hdr_t*)je_malloc(sizeof(mp_blk_hdr_t) + size);
if (!h) return NULL;
h->magic = MP_MAGIC;
h->class_id = MP_LARGE_CLASS;
@@ -149,7 +165,7 @@ void mp_free(void* ptr){
if (h->class_id == MP_LARGE_CLASS) {
/* 大对象:直接 free 整块(从 header 起) */
free(h);
je_free(h);
return;
}