From 64f72cc20f183a092c9e433d160c9d470a63e2d8 Mon Sep 17 00:00:00 2001 From: nekobit Date: Mon, 17 Oct 2022 20:13:32 +0000 Subject: [PATCH] Very cool and useful memory autism FossilOrigin-Name: 190c1b16d26a67e361d2666e795734667ce9bb12ba4c234af0a1c1b70d06b040 --- src/memory.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/memory.h | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/memory.c create mode 100644 src/memory.h diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..e26acac --- /dev/null +++ b/src/memory.c @@ -0,0 +1,68 @@ +/* + * Treebird - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "memory.h" + +#ifdef __GLIBC__ +#include +#endif + +int tb_memory_allocated_est; +int tb_memory_allocated; +int tb_memory_free; + +int tb_memory_times_allocated; +int tb_memory_times_freed; + +#ifdef __GLIBC__ +void propagate_memory() +{ + struct mallinfo2 inf = mallinfo2(); + + tb_memory_free = inf.fordblks; + tb_memory_allocated = inf.uordblks; +} +#endif // __GLIBC__ + +void* tb_malloc(size_t size) +{ + tb_memory_allocated_est += size; + ++tb_memory_times_allocated; + return malloc(size); +} + +// Nothing.... +void* tb_calloc(size_t nmemb, size_t size) +{ + tb_memory_allocated_est += size * nmemb; + return calloc(nmemb, size); +} + +void* tb_realloc(void* ptr, size_t size) +{ + tb_memory_allocated_est += size; + return realloc(ptr, size); +} + +void tb_free(void* ptr) +{ + ++tb_memory_times_freed; + free(ptr); +} diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..615f1a9 --- /dev/null +++ b/src/memory.h @@ -0,0 +1,51 @@ +/* + * Treebird - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/* Everything seen here is info for the super curious, and does not really + have anything to do with Treebird :-) */ + +#ifndef TREEBIRD_MEMSTUF_H +#define TREEBIRD_MEMSTUF_H +#include + +extern int tb_memory_allocated_est; +extern int tb_memory_allocated; +extern int tb_memory_free; + +extern int tb_memory_times_allocated; +extern int tb_memory_times_freed; + +#if 0 +#define tb_malloc malloc +#define tb_calloc calloc +#define tb_realloc realloc +#define tb_free free +#endif // Nothing + +void* tb_malloc(size_t size); +void* tb_calloc(size_t nmemb, size_t size); +void* tb_realloc(void* ptr, size_t size); +void tb_free(void* ptr); + +#ifdef __GLIBC__ +void propagate_memory(); +#else +#define propagate_memory() ; +#endif + +#endif // TREEBIRD_MEMSTUF_H