From 17578d2427b98a2800a34827b7a0174a02629acb Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 9 Nov 2021 10:59:34 +0800 Subject: [PATCH] more --- include/util/mallocator.h | 8 +- source/util/src/arenaAllocator.c | 14 --- source/util/src/heapAllocator.c | 85 --------------- source/util/src/mallocator.c | 180 +++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+), 100 deletions(-) delete mode 100644 source/util/src/arenaAllocator.c delete mode 100644 source/util/src/heapAllocator.c create mode 100644 source/util/src/mallocator.c diff --git a/include/util/mallocator.h b/include/util/mallocator.h index 87cccdbedf..a4705bdd2c 100644 --- a/include/util/mallocator.h +++ b/include/util/mallocator.h @@ -27,7 +27,7 @@ typedef struct SMemAllocator SMemAllocator; #define MALLOCATOR_APIS \ void *(*malloc)(SMemAllocator *, size_t size); \ void *(*calloc)(SMemAllocator *, size_t nmemb, size_t size); \ - void *(*realloc)(SMemAllocator *, size_t size); \ + void *(*realloc)(SMemAllocator *, void *ptr, size_t size); \ void (*free)(SMemAllocator *, void *ptr); \ size_t (*usage)(SMemAllocator *); @@ -50,6 +50,12 @@ void tdDestroyHeapAllocator(SMemAllocator *pMemAllocator); SMemAllocator *tdCreateArenaAllocator(size_t size); void tdDestroyArenaAllocator(SMemAllocator *); +#define mMalloc(pMemAllocator, size) (*(pMemAllocator->malloc))(pMemAllocator, size) +#define mCalloc(pMemAllocator, nmemb, size) (*(pMemAllocator->calloc))(pMemAllocator, nmemb, size) +#define mRealloc(pMemAllocator, ptr, size) (*(pMemAllocator->realloc))(pMemAllocator, ptr, size) +#define mFree(pMemAllocator, ptr) (*(pMemAllocator->free))(pMemAllocator, ptr) +#define mUsage(pMemAllocator) (*(pMemAllocator->usage))(pMemAllocator) + #ifdef __cplusplus } #endif diff --git a/source/util/src/arenaAllocator.c b/source/util/src/arenaAllocator.c deleted file mode 100644 index 6dea4a4e57..0000000000 --- a/source/util/src/arenaAllocator.c +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ \ No newline at end of file diff --git a/source/util/src/heapAllocator.c b/source/util/src/heapAllocator.c deleted file mode 100644 index 645277b386..0000000000 --- a/source/util/src/heapAllocator.c +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#include "mallocator.h" - -typedef struct { - char name[64]; -} SHeapAllocator; - -static SHeapAllocator *haNew(); -static void haDestroy(SHeapAllocator *pha); -static void * haMalloc(SMemAllocator *pMemAllocator, size_t size); -void * haCalloc(SMemAllocator *pMemAllocator, size_t nmemb, size_t size); -static void haFree(SMemAllocator *pMemAllocator, void *ptr); - -SMemAllocator *tdCreateHeapAllocator() { - SMemAllocator *pMemAllocator = NULL; - - pMemAllocator = (SMemAllocator *)calloc(1, sizeof(*pMemAllocator)); - if (pMemAllocator == NULL) { - // TODO: handle error - return NULL; - } - - pMemAllocator->impl = haNew(); - if (pMemAllocator->impl == NULL) { - tdDestroyHeapAllocator(pMemAllocator); - return NULL; - } - - pMemAllocator->malloc = haMalloc; - pMemAllocator->calloc = haCalloc; - pMemAllocator->realloc = NULL; - pMemAllocator->free = haFree; - pMemAllocator->usage = NULL; - - return pMemAllocator; -} - -void tdDestroyHeapAllocator(SMemAllocator *pMemAllocator) { - if (pMemAllocator) { - // TODO - } -} - -/* ------------------------ STATIC METHODS ------------------------ */ -static SHeapAllocator *haNew() { - SHeapAllocator *pha = NULL; - /* TODO */ - return pha; -} - -static void haDestroy(SHeapAllocator *pha) { - // TODO -} - -static void *haMalloc(SMemAllocator *pMemAllocator, size_t size) { - void *ptr = NULL; - - ptr = malloc(size); - if (ptr) { - } - - return ptr; -} - -void *haCalloc(SMemAllocator *pMemAllocator, size_t nmemb, size_t size) { - /* TODO */ - return NULL; -} - -static void haFree(SMemAllocator *pMemAllocator, void *ptr) { /* TODO */ -} \ No newline at end of file diff --git a/source/util/src/mallocator.c b/source/util/src/mallocator.c new file mode 100644 index 0000000000..0bd8f0742a --- /dev/null +++ b/source/util/src/mallocator.c @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "mallocator.h" + +/* ------------------------ HEAP ALLOCATOR ------------------------ */ +typedef struct { + size_t tusage; +} SHeapAllocator; + +static void * haMalloc(SMemAllocator *pma, size_t size); +static void * haCalloc(SMemAllocator *pma, size_t nmemb, size_t size); +static void * haRealloc(SMemAllocator *pma, void *ptr, size_t size); +static void haFree(SMemAllocator *pma, void *ptr); +static size_t haUsage(SMemAllocator *pma); + +SMemAllocator *tdCreateHeapAllocator() { + SMemAllocator *pma = NULL; + + pma = calloc(1, sizeof(SMemAllocator) + sizeof(SHeapAllocator)); + if (pma) { + pma->impl = POINTER_SHIFT(pma, sizeof(SMemAllocator)); + pma->malloc = haMalloc; + pma->calloc = haCalloc; + pma->realloc = haRealloc; + pma->free = haFree; + pma->usage = haUsage; + } + + return pma; +} + +void tdDestroyHeapAllocator(SMemAllocator *pMemAllocator) { + // TODO +} + +static void *haMalloc(SMemAllocator *pma, size_t size) { + void * ptr; + size_t tsize = size + sizeof(size_t); + SHeapAllocator *pha = (SHeapAllocator *)(pma->impl); + + ptr = malloc(tsize); + if (ptr) { + *(size_t *)ptr = size; + ptr = POINTER_SHIFT(ptr, sizeof(size_t)); + atomic_fetch_add_64(&(pha->tusage), tsize); + } + + return ptr; +} + +static void *haCalloc(SMemAllocator *pma, size_t nmemb, size_t size) { + void * ptr; + size_t tsize = nmemb * size; + + ptr = haMalloc(pma, tsize); + if (ptr) { + memset(ptr, 0, tsize); + } + + return ptr; +} + +static void *haRealloc(SMemAllocator *pma, void *ptr, size_t size) { + size_t psize; + size_t tsize = size + sizeof(size_t); + + if (ptr == NULL) { + psize = 0; + } else { + psize = *(size_t *)POINTER_SHIFT(ptr, -sizeof(size_t)); + } + + if (psize < size) { + // TODO + } else { + return ptr; + } +} + +static void haFree(SMemAllocator *pma, void *ptr) { /* TODO */ + SHeapAllocator *pha = (SHeapAllocator *)(pma->impl); + if (ptr) { + size_t tsize = *(size_t *)POINTER_SHIFT(ptr, -sizeof(size_t)) + sizeof(size_t); + atomic_fetch_sub_64(&(pha->tusage), tsize); + free(POINTER_SHIFT(ptr, -sizeof(size_t))); + } +} + +static size_t haUsage(SMemAllocator *pma) { return ((SHeapAllocator *)(pma->impl))->tusage; } + +/* ------------------------ ARENA ALLOCATOR ------------------------ */ +typedef struct { + size_t usage; +} SArenaAllocator; + +#if 0 +SMemAllocator *pDefaultMA; + +typedef struct { + char name[64]; +} SHeapAllocator; + +static SHeapAllocator *haNew(); +static void haDestroy(SHeapAllocator *pha); +static void * haMalloc(SMemAllocator *pMemAllocator, size_t size); +void * haCalloc(SMemAllocator *pMemAllocator, size_t nmemb, size_t size); +static void haFree(SMemAllocator *pMemAllocator, void *ptr); + +SMemAllocator *tdCreateHeapAllocator() { + SMemAllocator *pMemAllocator = NULL; + + pMemAllocator = (SMemAllocator *)calloc(1, sizeof(*pMemAllocator)); + if (pMemAllocator == NULL) { + // TODO: handle error + return NULL; + } + + pMemAllocator->impl = haNew(); + if (pMemAllocator->impl == NULL) { + tdDestroyHeapAllocator(pMemAllocator); + return NULL; + } + + pMemAllocator->malloc = haMalloc; + pMemAllocator->calloc = haCalloc; + pMemAllocator->realloc = NULL; + pMemAllocator->free = haFree; + pMemAllocator->usage = NULL; + + return pMemAllocator; +} + +void tdDestroyHeapAllocator(SMemAllocator *pMemAllocator) { + if (pMemAllocator) { + // TODO + } +} + +/* ------------------------ STATIC METHODS ------------------------ */ +static SHeapAllocator *haNew() { + SHeapAllocator *pha = NULL; + /* TODO */ + return pha; +} + +static void haDestroy(SHeapAllocator *pha) { + // TODO +} + +static void *haMalloc(SMemAllocator *pMemAllocator, size_t size) { + void *ptr = NULL; + + ptr = malloc(size); + if (ptr) { + } + + return ptr; +} + +void *haCalloc(SMemAllocator *pMemAllocator, size_t nmemb, size_t size) { + /* TODO */ + return NULL; +} + +static void haFree(SMemAllocator *pMemAllocator, void *ptr) { /* TODO */ +} +#endif \ No newline at end of file -- GitLab