BugID:16731946:add lock for tlsf
#include "tlsf.h"
+static kmutex_t tlsf_mutex;
+static uint8_t once;
+
#if defined(__cplusplus)
#define tlsf_decl inline
#else
@@ -1097,6 +1100,11 @@ tlsf_t tlsf_create(void* mem)
tlsf_t tlsf_create_with_pool(void* mem, size_t bytes)
{
+ if (once == 0u) {
+ krhino_mutex_create(tlsf_mutex, "tlsf_mutex");
+ once = 1u;
+ }
+
tlsf_t tlsf = tlsf_create(mem);
tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());
return tlsf;
@@ -1113,7 +1121,7 @@ pool_t tlsf_get_pool(tlsf_t tlsf)
return tlsf_cast(pool_t, (char*)tlsf + tlsf_size());
}
-void* tlsf_malloc(tlsf_t tlsf, size_t size)
+void* _tlsf_malloc(tlsf_t tlsf, size_t size)
{
control_t* control = tlsf_cast(control_t*, tlsf);
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
@@ -1121,6 +1129,16 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
return block_prepare_used(control, block, adjust);
}
+void* tlsf_malloc(tlsf_t tlsf, size_t size)
+{
+ void *ret;
+ krhino_mutex_lock(&tlsf_mutex, RHINO_WAIT_FOREVER);
+ ret = _tlsf_malloc(tlsf, size);
+ krhino_mutex_unlock(&tlsf_mutex);
+
+ return ret;
+}
+
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
{
control_t* control = tlsf_cast(control_t*, tlsf);
@@ -1178,7 +1196,7 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
return block_prepare_used(control, block, adjust);
}
-void tlsf_free(tlsf_t tlsf, void* ptr)
+void _tlsf_free(tlsf_t tlsf, void* ptr)
{
/* Don't attempt to free a NULL pointer. */
if (ptr)
@@ -1193,6 +1211,14 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
}
}
+void tlsf_free(tlsf_t tlsf, void* ptr)
+{
+ krhino_mutex_lock(&tlsf_mutex, RHINO_WAIT_FOREVER);
+ _tlsf_free(tlsf, ptr);
+ krhino_mutex_unlock(&tlsf_mutex);
+}
+
+
/*
** The TLSF block information provides us with enough information to
** provide a reasonably intelligent implementation of realloc, growing or
