aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib
diff options
context:
space:
mode:
Diffstat (limited to 'com32/lib')
-rw-r--r--com32/lib/free.c1
-rw-r--r--com32/lib/malloc.c30
-rw-r--r--com32/lib/realloc.c3
3 files changed, 33 insertions, 1 deletions
diff --git a/com32/lib/free.c b/com32/lib/free.c
index 0cf874b4..437a8fc0 100644
--- a/com32/lib/free.c
+++ b/com32/lib/free.c
@@ -31,6 +31,7 @@ __free_block(struct free_arena_header *ah)
} else {
/* Need to add this block to the free chain */
ARENA_TYPE_SET(ah->a.attrs, ARENA_TYPE_FREE);
+ ah->a.tag = NULL;
ah->next_free = __malloc_head.next_free;
ah->prev_free = &__malloc_head;
diff --git a/com32/lib/malloc.c b/com32/lib/malloc.c
index b0a8fe25..09a9989d 100644
--- a/com32/lib/malloc.c
+++ b/com32/lib/malloc.c
@@ -9,10 +9,15 @@
#include "init.h"
#include "malloc.h"
+/**
+ * The global tag used to label all the new allocations
+ */
+static void *__global_tag = NULL;
+
struct free_arena_header __malloc_head =
{
{
- (void*)0,
+ NULL,
ARENA_TYPE_HEAD,
&__malloc_head,
&__malloc_head,
@@ -49,6 +54,7 @@ static void __constructor init_memory_arena(void)
fp = (struct free_arena_header *)start;
ARENA_TYPE_SET(fp->a.attrs, ARENA_TYPE_FREE);
ARENA_SIZE_SET(fp->a.attrs, total_space - __stack_size);
+ fp->a.tag = NULL;
/* Insert into chains */
fp->a.next = fp->a.prev = &__malloc_head;
@@ -72,8 +78,10 @@ static void *__malloc_from_block(struct free_arena_header *fp, size_t size)
ARENA_TYPE_SET(nfp->a.attrs, ARENA_TYPE_FREE);
ARENA_SIZE_SET(nfp->a.attrs, fsize-size);
+ nfp->a.tag = NULL;
ARENA_TYPE_SET(fp->a.attrs, ARENA_TYPE_USED);
ARENA_SIZE_SET(fp->a.attrs, size);
+ fp->a.tag = __global_tag;
/* Insert into all-block chain */
nfp->a.prev = fp;
@@ -89,6 +97,7 @@ static void *__malloc_from_block(struct free_arena_header *fp, size_t size)
} else {
/* Allocate the whole block */
ARENA_TYPE_SET(fp->a.attrs, ARENA_TYPE_USED);
+ fp->a.tag = __global_tag;
/* Remove from free chain */
fp->next_free->prev_free = fp->prev_free;
@@ -174,6 +183,7 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
ARENA_TYPE_SET(nfp->a.attrs, ARENA_TYPE_FREE);
ARENA_SIZE_SET(nfp->a.attrs,
ARENA_SIZE_GET(fp->a.attrs) - ((uintptr_t)nfp - (uintptr_t)fp));
+ nfp->a.tag = NULL;
nfp->a.prev = fp;
nfp->a.next = fp->a.next;
nfp->prev_free = fp;
@@ -199,3 +209,21 @@ int posix_memalign(void **memptr, size_t alignment, size_t size) {
return 0;
}
+
+void *__mem_get_tag_global() {
+ return __global_tag;
+}
+
+void __mem_set_tag_global(void *tag) {
+ __global_tag = tag;
+}
+
+void *__mem_get_tag(void *memptr) {
+ struct arena_header *ah = (struct arena_header*)memptr - 1;
+ return ah->tag;
+}
+
+void __mem_set_tag(void *memptr, void *tag) {
+ struct arena_header *ah = (struct arena_header*)memptr - 1;
+ ah->tag = tag;
+}
diff --git a/com32/lib/realloc.c b/com32/lib/realloc.c
index a9b0ca4e..ed7fc4c0 100644
--- a/com32/lib/realloc.c
+++ b/com32/lib/realloc.c
@@ -58,6 +58,7 @@ void *realloc(void *ptr, size_t size)
nah = (struct free_arena_header *)((char *)ah + newsize);
ARENA_TYPE_SET(nah->a.attrs, ARENA_TYPE_FREE);
ARENA_SIZE_SET(nah->a.attrs, xsize - newsize);
+ nah->a.tag = NULL;
ARENA_SIZE_SET(ah->a.attrs, newsize);
/* Insert into block list */
@@ -91,6 +92,8 @@ void *realloc(void *ptr, size_t size)
newptr = malloc(size);
if (newptr) {
memcpy(newptr, ptr, min(size,oldsize));
+ /* Retain tag from the old block */
+ __mem_set_tag(newptr, __mem_get_tag(ptr));
free(ptr);
}
return newptr;