Thread-secure user-space heap.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-08-31 21:36:44 +02:00
parent 0618b8a68f
commit a96aca09c1
1 changed files with 60 additions and 7 deletions

View File

@ -26,6 +26,7 @@
#if __STDC_HOSTED__ #if __STDC_HOSTED__
#include <error.h> #include <error.h>
#include <pthread.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#endif #endif
@ -195,7 +196,9 @@ inline size_t BSF(size_t Value)
struct Chunk; struct Chunk;
struct Trailer; struct Trailer;
#if defined(__is_sortix_kernel) #if __STDC_HOSTED__
pthread_mutex_t heaplock;
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_t heaplock; Sortix::kthread_mutex_t heaplock;
#endif #endif
@ -407,7 +410,9 @@ extern "C" void _init_heap()
wildernesssize = 0; wildernesssize = 0;
for ( size_t i = 0; i < NUMBINS; i++ ) { bins[i] = NULL; } for ( size_t i = 0; i < NUMBINS; i++ ) { bins[i] = NULL; }
bincontainschunks = 0; bincontainschunks = 0;
#if defined(__is_sortix_kernel) #if __STDC_HOSTED__
heaplock = PTHREAD_MUTEX_INITIALIZER;
#elif defined(__is_sortix_kernel)
heaplock = Sortix::KTHREAD_MUTEX_INITIALIZER; heaplock = Sortix::KTHREAD_MUTEX_INITIALIZER;
#endif #endif
} }
@ -445,8 +450,10 @@ static bool ExpandWilderness(size_t bytesneeded)
extern "C" void* malloc(size_t size) extern "C" void* malloc(size_t size)
{ {
#if defined(__is_sortix_kernel) #if __STDC_HOSTED__
Sortix::ScopedLock scopedlock(&heaplock); pthread_mutex_lock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_lock(&heaplock);
#endif #endif
#if 2 <= PARANOIA #if 2 <= PARANOIA
@ -516,6 +523,13 @@ extern "C" void* malloc(size_t size)
#endif #endif
uintptr_t result = ((uintptr_t) chunk) + sizeof(Chunk); uintptr_t result = ((uintptr_t) chunk) + sizeof(Chunk);
#if __STDC_HOSTED__
pthread_mutex_unlock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_unlock(&heaplock);
#endif
return (void*) result; return (void*) result;
} }
@ -525,6 +539,13 @@ extern "C" void* malloc(size_t size)
if ( wildernesssize < size && !ExpandWilderness(size) ) if ( wildernesssize < size && !ExpandWilderness(size) )
{ {
errno = ENOMEM; errno = ENOMEM;
#if __STDC_HOSTED__
pthread_mutex_unlock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_unlock(&heaplock);
#endif
return NULL; return NULL;
} }
@ -546,6 +567,13 @@ extern "C" void* malloc(size_t size)
#endif #endif
uintptr_t result = ((uintptr_t) chunk) + sizeof(Chunk); uintptr_t result = ((uintptr_t) chunk) + sizeof(Chunk);
#if __STDC_HOSTED__
pthread_mutex_unlock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_unlock(&heaplock);
#endif
return (void*) result; return (void*) result;
} }
@ -620,15 +648,27 @@ static void UnifyNeighbors(Chunk** chunk)
extern "C" void free(void* addr) extern "C" void free(void* addr)
{ {
#if defined(__is_sortix_kernel) #if __STDC_HOSTED__
Sortix::ScopedLock scopedlock(&heaplock); pthread_mutex_lock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_lock(&heaplock);
#endif #endif
#if 2 <= PARANOIA #if 2 <= PARANOIA
assert(ValidateHeap()); assert(ValidateHeap());
#endif #endif
if ( !addr) { return; } if ( !addr)
{
#if __STDC_HOSTED__
pthread_mutex_unlock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_unlock(&heaplock);
#endif
return;
}
Chunk* chunk = (Chunk*) ((uintptr_t) addr - sizeof(Chunk)); Chunk* chunk = (Chunk*) ((uintptr_t) addr - sizeof(Chunk));
#if __STDC_HOSTED__ #if __STDC_HOSTED__
if ( !IsGoodHeapPointer(addr, 1) || if ( !IsGoodHeapPointer(addr, 1) ||
@ -656,6 +696,13 @@ extern "C" void free(void* addr)
{ {
heapsize -= chunk->size; heapsize -= chunk->size;
wildernesssize += chunk->size; wildernesssize += chunk->size;
#if __STDC_HOSTED__
pthread_mutex_unlock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_unlock(&heaplock);
#endif
return; return;
} }
@ -664,6 +711,12 @@ extern "C" void free(void* addr)
#if 3 <= PARANOIA #if 3 <= PARANOIA
assert(ValidateHeap()); assert(ValidateHeap());
#endif #endif
#if __STDC_HOSTED__
pthread_mutex_unlock(&heaplock);
#elif defined(__is_sortix_kernel)
Sortix::kthread_mutex_unlock(&heaplock);
#endif
} }
// TODO: Implement this function properly. // TODO: Implement this function properly.