From 7cb08a5c6749541d6da4cfdf65483567d32d33a5 Mon Sep 17 00:00:00 2001 From: ffashion Date: Thu, 29 Feb 2024 19:20:07 +0800 Subject: [PATCH] fix pool: fix mem pool style and add pcallc --- src/adb/packet.c | 6 +++++- src/adb/xdbd_adb.c | 2 +- src/adb/xdbd_adb_request.c | 2 +- src/core/xdbd_pool.c | 36 ++++++++++++++++++------------------ src/core/xdbd_pool.h | 16 +++++----------- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/adb/packet.c b/src/adb/packet.c index 15b94bf..cf81b1e 100644 --- a/src/adb/packet.c +++ b/src/adb/packet.c @@ -388,7 +388,11 @@ void xdbd_dump_adb_packet(xdbd_pool_t *pool, const xdbd_adb_packet_t *p) { hex_payload.size = xdbd_buf_size(p->payload); payload = xdbd_dump_hex(pool, hex_payload); - + if (payload == NULL) { + // bfdev_log_debug("payload is null\n"); + // return; + } + bfdev_log_debug("%p", payload); bfdev_log_debug("%.*s\n", (int)xdbd_buf_size(payload), payload->pos); return; } diff --git a/src/adb/xdbd_adb.c b/src/adb/xdbd_adb.c index 8f62ca0..7e2e8d8 100644 --- a/src/adb/xdbd_adb.c +++ b/src/adb/xdbd_adb.c @@ -66,7 +66,7 @@ int xdbd_adb_parse_adb_header(xdbd_adb_request_t *r, xdbd_buf_t *b) { xdbd_memcpy(&r->h, b->start, sizeof(xdbd_adb_header_t)); - r->p = xdbd_palloc(r->temp_pool, sizeof(xdbd_adb_packet_t)); + r->p = xdbd_pcalloc(r->temp_pool, sizeof(xdbd_adb_packet_t)); if (r->p == NULL) { return XDBD_ERR; } diff --git a/src/adb/xdbd_adb_request.c b/src/adb/xdbd_adb_request.c index b69af4d..58d636a 100644 --- a/src/adb/xdbd_adb_request.c +++ b/src/adb/xdbd_adb_request.c @@ -5,7 +5,7 @@ xdbd_adb_request_t *xdbd_adb_create_request(xdbd_connection_t *c) { xdbd_adb_request_t *r; - r = xdbd_palloc(c->pool, sizeof(xdbd_adb_request_t)); + r = xdbd_pcalloc(c->pool, sizeof(xdbd_adb_request_t)); if (r == NULL) { return NULL; } diff --git a/src/core/xdbd_pool.c b/src/core/xdbd_pool.c index 4c19bf8..ca7635a 100644 --- a/src/core/xdbd_pool.c +++ b/src/core/xdbd_pool.c @@ -6,10 +6,9 @@ #include #include #include +#include -static inline void * -cache_alloc(xdbd_pool_t *pool, size_t size) -{ +static inline void *cache_alloc(xdbd_pool_t *pool, size_t size) { xdbd_cache_t *cache; void *retval; @@ -42,9 +41,7 @@ cache_alloc(xdbd_pool_t *pool, size_t size) return retval; } -static inline void * -block_alloc(xdbd_pool_t *pool, size_t size) -{ +static inline void *block_alloc(xdbd_pool_t *pool, size_t size) { xdbd_block_t *block; void *retval; @@ -66,18 +63,25 @@ block_alloc(xdbd_pool_t *pool, size_t size) return retval; } -void * -xdbd_palloc(xdbd_pool_t *pool, size_t size) -{ +void *xdbd_palloc(xdbd_pool_t *pool, size_t size) { if (size < XDBD_PAGE_SIZE) return cache_alloc(pool, size); return block_alloc(pool, size); } -xdbd_pool_t * -xdbd_create_pool() -{ +void *xdbd_pcalloc(xdbd_pool_t *pool, size_t size) { + void *p; + p = xdbd_palloc(pool, size); + if (p == NULL) { + return NULL; + } + + xdbd_memzero(p, size); + return p; +} + +xdbd_pool_t *xdbd_create_pool() { xdbd_pool_t *pool; pool = bfdev_malloc(NULL, sizeof(*pool)); @@ -90,9 +94,7 @@ xdbd_create_pool() return pool; } -void -xdbd_release_pool(xdbd_pool_t *pool) -{ +void xdbd_release_pool(xdbd_pool_t *pool) { xdbd_cache_t *cache, *tcache; xdbd_block_t *block, *tblock; @@ -110,9 +112,7 @@ xdbd_release_pool(xdbd_pool_t *pool) bfdev_slist_head_init(&pool->block); } -void -xdbd_destroy_pool(xdbd_pool_t *pool) -{ +void xdbd_destroy_pool(xdbd_pool_t *pool) { xdbd_release_pool(pool); bfdev_free(NULL, pool); } diff --git a/src/core/xdbd_pool.h b/src/core/xdbd_pool.h index 6152bcc..ef14973 100644 --- a/src/core/xdbd_pool.h +++ b/src/core/xdbd_pool.h @@ -39,16 +39,10 @@ struct xdbd_block { void *data; }; -extern __bfdev_malloc void * -xdbd_palloc(xdbd_pool_t *pool, size_t size); - -extern xdbd_pool_t * -xdbd_create_pool(); - -extern void -xdbd_release_pool(xdbd_pool_t *pool); - -extern void -xdbd_destroy_pool(xdbd_pool_t *pool); +__bfdev_malloc void *xdbd_palloc(xdbd_pool_t *pool, size_t size); +void *xdbd_pcalloc(xdbd_pool_t *pool, size_t size); +xdbd_pool_t *xdbd_create_pool(); +void xdbd_release_pool(xdbd_pool_t *pool); +void xdbd_destroy_pool(xdbd_pool_t *pool); #endif /*__XDBD_POOL__H__*/