From: Matthew Fernandez Date: Sat, 11 Jul 2020 22:05:53 +0000 (-0700) Subject: remove a level of indirection in vmalloc X-Git-Tag: 2.46.0~20^2^2~182^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f1103d72cc1acdfd132851df1a88447b4a156b01;p=graphviz remove a level of indirection in vmalloc The metadata for the allocator is now allocated directly in Vmalloc_t instead of within a Vmdata_t. This lets us allocate in a single block more efficiently. --- diff --git a/lib/vmalloc/vmalloc.h b/lib/vmalloc/vmalloc.h index ce62e36cb..d2e9a7f3a 100644 --- a/lib/vmalloc/vmalloc.h +++ b/lib/vmalloc/vmalloc.h @@ -72,6 +72,10 @@ extern "C" { #ifdef _VM_PRIVATE_ _VM_PRIVATE_ #endif + + void **allocated; /* pointers we have given out */ + size_t size; /* used entries in `allocated` */ + size_t capacity; /* available entries in `allocated` */ }; #define VM_TRUST 0000001 /* forgo some security checks */ diff --git a/lib/vmalloc/vmbest.c b/lib/vmalloc/vmbest.c index 9ddb55567..0e192d095 100644 --- a/lib/vmalloc/vmbest.c +++ b/lib/vmalloc/vmbest.c @@ -16,24 +16,24 @@ /** make room to store a new pointer we are about to allocate * - * @param vd Vmdata to operate on + * @param vm Vmalloc to operate on * @returns true on success */ -static bool make_space(Vmdata_t *vd) { +static bool make_space(Vmalloc_t *vm) { - if (vd->size == vd->capacity) { + if (vm->size == vm->capacity) { void **p; /* expand our allocation storage */ - size_t c = vd->capacity == 0 ? 1 : vd->capacity * 2; - p = realloc(vd->allocated, sizeof(vd->allocated[0]) * c); + size_t c = vm->capacity == 0 ? 1 : vm->capacity * 2; + p = realloc(vm->allocated, sizeof(vm->allocated[0]) * c); if (p == NULL) { return false; } /* save the new array */ - vd->allocated = p; - vd->capacity = c; + vm->allocated = p; + vm->capacity = c; } return true; @@ -44,10 +44,9 @@ static bool make_space(Vmdata_t *vd) { * @param size desired block size */ void *bestalloc(Vmalloc_t *vm, size_t size) { - Vmdata_t *vd = vm->data; void *p; - if (!make_space(vd)) { + if (!make_space(vm)) { return NULL; } @@ -56,14 +55,13 @@ void *bestalloc(Vmalloc_t *vm, size_t size) { return NULL; } - vd->allocated[vd->size] = p; - ++vd->size; + vm->allocated[vm->size] = p; + ++vm->size; return p; } int bestfree(Vmalloc_t *vm, void *data) { - Vmdata_t *vd = vm->data; size_t i; if (!data) { /* ANSI-ism */ @@ -71,13 +69,13 @@ int bestfree(Vmalloc_t *vm, void *data) { } /* find the pointer we previously allocated */ - for (i = 0; i < vd->size; ++i) { - if (vd->allocated[i] == data) { + for (i = 0; i < vm->size; ++i) { + if (vm->allocated[i] == data) { /* clear this slot */ - size_t extent = sizeof(vd->allocated[0]) * (vd->size - i - 1); - memmove(vd->allocated + i, vd->allocated + i + 1, extent); - --vd->size; + size_t extent = sizeof(vm->allocated[0]) * (vm->size - i - 1); + memmove(vm->allocated + i, vm->allocated + i + 1, extent); + --vm->size; /* give this back to the underlying allocator */ free(data); @@ -97,7 +95,6 @@ int bestfree(Vmalloc_t *vm, void *data) { * @param type ignored */ void *bestresize(Vmalloc_t *vm, void *data, size_t size, int type) { - Vmdata_t *vd = vm->data; size_t i; /* ignore type */ @@ -108,8 +105,8 @@ void *bestresize(Vmalloc_t *vm, void *data, size_t size, int type) { } /* find the pointer we previously allocated */ - for (i = 0; i < vd->size; ++i) { - if (vd->allocated[i] == data) { + for (i = 0; i < vm->size; ++i) { + if (vm->allocated[i] == data) { /* resize the allocation */ void *p = realloc(data, size); @@ -118,7 +115,7 @@ void *bestresize(Vmalloc_t *vm, void *data, size_t size, int type) { } /* save the updated pointer */ - vd->allocated[i] = p; + vm->allocated[i] = p; return p; } diff --git a/lib/vmalloc/vmclear.c b/lib/vmalloc/vmclear.c index fc9806711..d512cebbd 100644 --- a/lib/vmalloc/vmclear.c +++ b/lib/vmalloc/vmclear.c @@ -18,17 +18,16 @@ */ int vmclear(Vmalloc_t *vm) { size_t i; - Vmdata_t *vd = vm->data; /* free all allocated pointers */ - for (i = 0; i < vd->size; ++i) { - free(vd->allocated[i]); + for (i = 0; i < vm->size; ++i) { + free(vm->allocated[i]); } /* reset our metadata */ - free(vd->allocated); - vd->allocated = NULL; - vd->size = vd->capacity = 0; + free(vm->allocated); + vm->allocated = NULL; + vm->size = vm->capacity = 0; return 0; } diff --git a/lib/vmalloc/vmclose.c b/lib/vmalloc/vmclose.c index fcc21bd2b..d300370a1 100644 --- a/lib/vmalloc/vmclose.c +++ b/lib/vmalloc/vmclose.c @@ -17,7 +17,6 @@ ** Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94. */ int vmclose(Vmalloc_t *vm) { - Vmdata_t *vd = vm->data; int r; /* clear the region */ @@ -27,7 +26,6 @@ int vmclose(Vmalloc_t *vm) { } /* free the allocator itself */ - free(vd); free(vm); return 0; diff --git a/lib/vmalloc/vmhdr.h b/lib/vmalloc/vmhdr.h index 94ab9f756..d6d9a1524 100644 --- a/lib/vmalloc/vmhdr.h +++ b/lib/vmalloc/vmhdr.h @@ -232,10 +232,6 @@ extern "C" { Block_t *root; /* root of free tree */ Block_t *tiny[S_TINY]; /* small blocks */ Block_t *cache[S_CACHE + 1]; /* delayed free blocks */ - - void **allocated; /* pointers we have given out */ - size_t size; /* used entries in `allocated` */ - size_t capacity; /* available entries in `allocated` */ } Vmdata_t; /* private parts of Vmalloc_t */ diff --git a/lib/vmalloc/vmopen.c b/lib/vmalloc/vmopen.c index b0fca7646..bf959e07f 100644 --- a/lib/vmalloc/vmopen.c +++ b/lib/vmalloc/vmopen.c @@ -20,7 +20,7 @@ Vmalloc_t *vmopen(void) { Vmalloc_t *vm; - vm = malloc(sizeof(*vm)); + vm = calloc(1, sizeof(*vm)); if (vm == NULL) { return NULL; } @@ -29,11 +29,5 @@ Vmalloc_t *vmopen(void) { vm->meth.resizef = bestresize; vm->meth.freef = bestfree; - vm->data = calloc(1, sizeof(*vm->data)); - if (vm->data == NULL) { - free(vm); - return NULL; - } - return vm; }