commit 1f4683802de2474d51ae449f81cb3c52336e8fe1
parent 4ce7f24be3fbdb53a1bf132e342e619ba3c782c5
Author: gearsix <gearsix@tuta.io>
Date: Thu, 16 Dec 2021 11:14:41 +0000
renamed htrack->tralloc; updated functions to tr_
Diffstat:
M | README.md | | | 60 | ++++++++++++++++++++++++++++++------------------------------ |
D | htrack.c | | | 67 | ------------------------------------------------------------------- |
D | htrack.h | | | 37 | ------------------------------------- |
M | test.c | | | 40 | ++++++++++++++++++++-------------------- |
A | tralloc.c | | | 67 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | tralloc.h | | | 35 | +++++++++++++++++++++++++++++++++++ |
6 files changed, 152 insertions(+), 154 deletions(-)
diff --git a/README.md b/README.md
@@ -1,5 +1,5 @@
-# htrack
+# tralloc
An ANSI-C libray for tracking heap-allocated memory.
@@ -17,7 +17,7 @@ allocations made without these wrappers. This is acceptable since
it's intended to be used for tracking allocations made by yourself.
There are also a few usability pitfalls: if you call some of the
-functions on pointers that **have not** been allocated by htrack
+functions on pointers that **have not** been allocated by tralloc
allocation functions, then it'll cause a memory error.
This behaviour is consistent with stdlib though. Make sure to read
function documentation.
@@ -34,48 +34,48 @@ cool idea I got carried away with.
## Guidelines
-- **Do not** call `htrack_realloc()`, `htrack_allocsiz()` or
-`htrack_free` on a pointer not allocated by one of the htrack stdlib
+- **Do not** call `tralloc_realloc()`, `tralloc_allocsiz()` or
+`tralloc_free` on a pointer not allocated by one of the tralloc stdlib
wrapper functions (same way you wouldn't call free on a pointer you
didn't allocate with stdlib). Otherwise you'll probably run into a
segfault.
-- Don't expect htrack to track memory allocated outside of it's own
-htrack functions.
-- You should be able to use the _htrack stdlib wrappers_ just as you
+- Don't expect tralloc to track memory allocated outside of it's own
+tralloc functions.
+- You should be able to use the _tralloc stdlib wrappers_ just as you
would use their stdlib counterparts without error.
## API
-**htrack_siz**
+**tralloc_siz**
- size_t htrack_siz();
+ size_t tralloc_siz();
-Returns the number of bytes heap-allocated by _htrack memory allocation_
+Returns the number of bytes heap-allocated by _tralloc memory allocation_
functions.
-**htrack_limit**
+**tralloc_limit**
- size_t htrack_limit();
+ size_t tralloc_limit();
-Returns the maximum limit set on `htrack_siz`.
+Returns the maximum limit set on `tralloc_siz`.
If 0 is returned, there is no limit (returns 0 by default).
-**htrack_setlimit**
+**tralloc_setlimit**
- size_t htrack_setlimit(size_t n);
+ size_t tralloc_setlimit(size_t n);
-Sets the maximum limit on `htrack_siz`.
-If this limit is reached, then htrack alloc functions will fail and
+Sets the maximum limit on `tralloc_siz`.
+If this limit is reached, then tralloc alloc functions will fail and
return NULL.
Returns `n` if successful and 0 if unsuccessful.
-**htrack_allocsiz**
+**tralloc_allocsiz**
- size_t htrack_allocsiz(void *ptr);
+ size_t tralloc_allocsiz(void *ptr);
Returns number of bytes allocated for `ptr`.
-> `ptr` **must** be a _htrack allocated pointer_.
+> `ptr` **must** be a _tralloc allocated pointer_.
### stdlib wrappers
@@ -87,25 +87,25 @@ tracked heapsiz accordingly.
This overhead is stored at index [-1] of the returned pointers and
used by functions within the library for tracking the heapsiz.
-**htrack_malloc**
+**tralloc_malloc**
- void *htrack_malloc(size_t n);
+ void *tralloc_malloc(size_t n);
-**htrack_calloc**
+**tralloc_calloc**
- void *htrack_calloc(size_t num, size_t n);
+ void *tralloc_calloc(size_t num, size_t n);
-**htrack_realloc**
+**tralloc_realloc**
- void *htrack_realloc(void *ptr, size_t n);
+ void *tralloc_realloc(void *ptr, size_t n);
-> `ptr` **must** be a pointer allocated by htrack.
+> `ptr` **must** be a pointer allocated by tralloc.
-**htrack_free**
+**tralloc_free**
- void htrack_free(void *ptr);
+ void tralloc_free(void *ptr);
-> `ptr` **must** be a pointer allocated by htrack.
+> `ptr` **must** be a pointer allocated by tralloc.
## Authors
diff --git a/htrack.c b/htrack.c
@@ -1,67 +0,0 @@
-#include "htrack.h"
-
-static size_t heapsiz = 0, heaplim = 0;
-
-size_t htrack_siz()
-{
- return heapsiz;
-}
-
-size_t htrack_limit()
-{
- return heaplim;
-}
-
-size_t htrack_setlimit(size_t n)
-{
- assert(heapsiz <= n);
- return (heaplim = n);
-}
-
-size_t htrack_allocsiz(void *ptr)
-{
- return ((size_t *)ptr)[-1];
-}
-
-void *htrack_malloc(size_t n)
-{
- size_t *ptr;
- assert(n);
- n += sizeof(size_t);
- if (heaplim > 0) assert(heapsiz + n <= heaplim);
- ptr = (size_t *)malloc(n);
- heapsiz += n;
- ptr[0] = n;
- return &ptr[1];
-}
-
-void *htrack_calloc(size_t num, size_t n)
-{
- size_t *ptr, psiz;
- assert(n && num);
- psiz = (num * n) + sizeof(size_t);
- if (heaplim > 0) assert(heapsiz + psiz <= heaplim);
- ptr = (size_t *)calloc(1, psiz);
- ptr[0] = psiz;
- return &ptr[1];
-}
-
-void *htrack_realloc(void *ptr, size_t n)
-{
- size_t *ret, m = htrack_allocsiz(ptr);
- n += sizeof(size_t);
- if (heaplim > 0) assert(heapsiz + (-m + n) <= heaplim);
- ret = (size_t *)realloc((size_t *)ptr-1, n);
- assert(ret != NULL);
- heapsiz += (-m + n);
- ret[0] = n;
- return &ret[1];
-}
-
-void htrack_free(void *ptr)
-{
- size_t n = ((size_t *)ptr)[-1];
- if (heaplim > 0) assert(heapsiz - n >= heaplim);
- heapsiz -= n;
- free((size_t *)ptr-1);
-}
diff --git a/htrack.h b/htrack.h
@@ -1,37 +0,0 @@
-/**
- * See README.md for documentation
-**/
-
-#ifndef STDLIBW
-#define STDLIBW
-
-#include <stdlib.h>
-#include <assert.h>
-
-/* num. bytes heap-allocated by htrack */
-size_t htrack_siz();
-
-/* the max. number of heap-allocated bytes htrack will allow */
-size_t htrack_limit();
-
-/* set the max. number of heap-allocated bytes htrack will allow, 0 = infinite */
-size_t htrack_setlimit(size_t n);
-
-/* The number of bytes allocated for `ptr`.
- * ptr MUST be allocated by htrack */
-size_t htrack_allocsiz(void *ptr);
-
-/**
- * stdlib wrappers - behave the same as their stdlib counterparts
-**/
-void *htrack_malloc(size_t n);
-
-void *htrack_calloc(size_t num, size_t n);
-
-/* ptr MUST be allocated by htrack */
-void *htrack_realloc(void *ptr, size_t n);
-
-/* ptr MUST be allocated by htrack */
-void htrack_free(void *ptr);
-
-#endif
diff --git a/test.c b/test.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
-#include "htrack.h"
+#include "tralloc.h"
void fail(const char *msg)
{
@@ -14,36 +14,36 @@ int main (int argc, char *argv[])
size_t siz;
char *str;
- printf("htrack_siz: %lu\n", htrack_siz());
- if (htrack_siz() != 0) fail("initial htrack_siz not 0\n");
- printf("htrack_limit: %lu\n", htrack_limit());
- if (htrack_limit() != 0) fail("initial htrack_setlimit not 0\n");
+ printf("tr_siz: %lu\n", tr_siz());
+ if (tr_siz() != 0) fail("initial tr_siz not 0\n");
+ printf("tr_limit: %lu\n", tr_limit());
+ if (tr_limit() != 0) fail("initial tr_setlimit not 0\n");
siz = 6;
- str = htrack_malloc(siz);
- printf("str = '%s', %zu bytes\n", str, htrack_allocsiz(str));
- if (str == NULL) fail("htrack_malloc failed\n");
- if (htrack_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid htrack_allocsiz value\n");
+ str = tr_malloc(siz);
+ printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
+ if (str == NULL) fail("tr_malloc failed\n");
+ if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
strcpy(str, "foobar");
if (strcmp(str, "foobar") != 0) fail("strcpy failed\n");
- printf("str = '%s', %zu bytes\n", str, htrack_allocsiz(str));
+ printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
siz = 12;
- str = htrack_realloc(str, siz);
- printf("str = '%s', %zu bytes\n", str, htrack_allocsiz(str));
- if (str == NULL) fail("htrack_realloc failed\n");
- if (htrack_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid htrack_allocsiz value\n");
+ str = tr_realloc(str, siz);
+ printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
+ if (str == NULL) fail("tr_realloc failed\n");
+ if (tr_allocsiz(str) != (siz + sizeof(size_t))) fail("invalid tr_allocsiz value\n");
strcpy(str, "foobarfoobar");
if (strcmp(str, "foobarfoobar") != 0) fail("strcpy failed\n");
- printf("str = '%s', %zu bytes\n", str, htrack_allocsiz(str));
+ printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
- htrack_free(str);
+ tr_free(str);
siz = 6;
- htrack_setlimit(10);
- str = htrack_calloc(2, siz); /* should cause assertion failure */
- printf("str = '%s', %zu bytes\n", str, htrack_allocsiz(str));
- htrack_free(str);
+ tr_setlimit(10);
+ str = tr_calloc(2, siz); /* should cause assertion failure */
+ printf("str = '%s', %zu bytes\n", str, tr_allocsiz(str));
+ tr_free(str);
return EXIT_SUCCESS;
diff --git a/tralloc.c b/tralloc.c
@@ -0,0 +1,67 @@
+#include "tralloc.h"
+
+static size_t heapsiz = 0, heaplim = 0;
+
+size_t tr_siz()
+{
+ return heapsiz;
+}
+
+size_t tr_limit()
+{
+ return heaplim;
+}
+
+size_t tr_setlimit(size_t n)
+{
+ assert(heapsiz <= n);
+ return (heaplim = n);
+}
+
+size_t tr_allocsiz(void *ptr)
+{
+ return ((size_t *)ptr)[-1];
+}
+
+void *tr_malloc(size_t n)
+{
+ size_t *ptr;
+ assert(n);
+ n += sizeof(size_t);
+ if (heaplim > 0) assert(heapsiz + n <= heaplim);
+ ptr = (size_t *)malloc(n);
+ heapsiz += n;
+ ptr[0] = n;
+ return &ptr[1];
+}
+
+void *tr_calloc(size_t num, size_t n)
+{
+ size_t *ptr, psiz;
+ assert(n && num);
+ psiz = (num * n) + sizeof(size_t);
+ if (heaplim > 0) assert(heapsiz + psiz <= heaplim);
+ ptr = (size_t *)calloc(1, psiz);
+ ptr[0] = psiz;
+ return &ptr[1];
+}
+
+void *tr_realloc(void *ptr, size_t n)
+{
+ size_t *ret, m = tr_allocsiz(ptr);
+ n += sizeof(size_t);
+ if (heaplim > 0) assert(heapsiz + (-m + n) <= heaplim);
+ ret = (size_t *)realloc((size_t *)ptr-1, n);
+ assert(ret != NULL);
+ heapsiz += (-m + n);
+ ret[0] = n;
+ return &ret[1];
+}
+
+void tr_free(void *ptr)
+{
+ size_t n = ((size_t *)ptr)[-1];
+ if (heaplim > 0) assert(heapsiz - n >= heaplim);
+ heapsiz -= n;
+ free((size_t *)ptr-1);
+}
diff --git a/tralloc.h b/tralloc.h
@@ -0,0 +1,35 @@
+/* See README.md for documentation
+ *
+ * IMPORTANT: Any `ptr` value used *must* be allocated by
+ * tralloc or you'll run into problems.
+ */
+
+#ifndef TRALLOC
+#define TRALLOC
+
+#include <stdlib.h>
+#include <assert.h>
+
+/* num. bytes heap-allocated by tralloc */
+size_t tr_siz();
+
+/* the max. number of heap-allocated bytes tralloc will allow */
+size_t tr_limit();
+
+/* set the max. number of heap-allocated bytes tralloc will allow, 0 = infinite */
+size_t tr_setlimit(size_t n);
+
+/* The number of bytes allocated for `ptr` */
+size_t tr_allocsiz(void *ptr);
+
+/* stdlib wrappers
+ * These behave the same as their stdlib counterparts */
+void *tr_malloc(size_t n);
+
+void *tr_calloc(size_t num, size_t n);
+
+void *tr_realloc(void *ptr, size_t n);
+
+void tr_free(void *ptr);
+
+#endif