tralloc

A C library that adds size information to memory allocations.
git clone git://src.gearsix.net/tralloc
Log | Files | Refs | Atom | README

commit 6673b3b3ca47c805b8a6966bd9ae6e09636a34ca
parent bbf4640c55b73b8c24eb4c5e8a98b8aad841acca
Author: gearsix <gearsix@tuta.io>
Date:   Fri, 26 Aug 2022 15:37:02 +0100

added original-draft.c

Diffstat:
Aoriginal-draft.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+), 0 deletions(-)

diff --git a/original-draft.c b/original-draft.c @@ -0,0 +1,56 @@ +/* htrack.c + * example of how to track heap-heapsiz during runtime + * -gearsix + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static size_t heapsiz = 0; + +// mallocw (malloc wrapper) allocates nbytes + sizeof(size_t). +// the allocated number of bytes for the pointer is stored in [0], +// the memory block requested by the user is stored from [1]..[n]. +// note that ptr[1] is returned. +void *mallocw(size_t nbytes) +{ + nbytes += sizeof(size_t); + heapsiz += nbytes; + size_t *ptr = malloc(nbytes); + ptr[0] = nbytes; + return &ptr[1]; +} + +// sizeofw (sizeof wrapper) check [-1] for the size_t value set by +// mallocw. ptr must have been allocated by mallocw to avoid issues. +size_t sizeofw(void *ptr) +{ + return ((size_t *)ptr)[-1]; +} + +// freew (free wrapper) frees *ptr from [-1], will probably cause +// an issue if the ptr wasn't allocated by mallocw. +void freew(void *ptr) +{ + heapsiz -= sizeofw(ptr); + free((size_t *)ptr-1); +} + +int main(int argc, char *argv[]) +{ + printf("heapsiz: %lu\n", heapsiz); + + char *str = mallocw(6); + if (str == NULL) { + fprintf(stderr, "mallocw failed"); + return EXIT_FAILURE; + } + strcpy(str, "foobar"); + printf("str = '%s', strlen %d, sizeof %d\n", str, strlen(str), sizeofw(str)); + printf("heapsiz: %lu\n", heapsiz); + puts("freeing str..."); + freew(str); + printf("heapsiz: %lu\n", heapsiz); + + return EXIT_SUCCESS; +}