2019-05-07 02:18:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
2019-11-11 23:45:21 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-05-07 02:18:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mem_alloc.h"
|
|
|
|
|
|
|
|
#if DEFAULT_MEM_ALLOCATOR == MEM_ALLOCATOR_EMS
|
|
|
|
|
|
|
|
#include "ems/ems_gc.h"
|
|
|
|
|
|
|
|
mem_allocator_t mem_allocator_create(void *mem, uint32_t size)
|
|
|
|
{
|
|
|
|
return gc_init_with_pool((char *) mem, size);
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
mem_allocator_t
|
|
|
|
mem_allocator_create_with_struct_and_pool(void *struct_buf,
|
|
|
|
uint32_t struct_buf_size,
|
|
|
|
void *pool_buf,
|
|
|
|
uint32_t pool_buf_size)
|
|
|
|
{
|
|
|
|
return gc_init_with_struct_and_pool((char *)struct_buf,
|
|
|
|
struct_buf_size,
|
|
|
|
pool_buf,
|
|
|
|
pool_buf_size);
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
void mem_allocator_destroy(mem_allocator_t allocator)
|
|
|
|
{
|
|
|
|
gc_destroy_with_pool((gc_handle_t) allocator);
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:52:33 +00:00
|
|
|
uint32
|
|
|
|
mem_allocator_get_heap_struct_size()
|
|
|
|
{
|
|
|
|
return gc_get_heap_struct_size();
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
void *
|
|
|
|
mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
|
|
|
|
{
|
2020-03-30 03:06:39 +00:00
|
|
|
return gc_alloc_vo((gc_handle_t) allocator, size);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 13:18:18 +00:00
|
|
|
void *
|
|
|
|
mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
|
|
|
|
{
|
2020-03-30 03:06:39 +00:00
|
|
|
return gc_realloc_vo((gc_handle_t) allocator, ptr, size);
|
2020-03-08 13:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
void mem_allocator_free(mem_allocator_t allocator, void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
2020-03-30 03:06:39 +00:00
|
|
|
gc_free_vo((gc_handle_t) allocator, ptr);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 02:49:40 +00:00
|
|
|
int
|
|
|
|
mem_allocator_migrate(mem_allocator_t allocator,
|
2020-10-22 10:52:33 +00:00
|
|
|
char *pool_buf_new, uint32 pool_buf_size)
|
2020-04-13 02:49:40 +00:00
|
|
|
{
|
|
|
|
return gc_migrate((gc_handle_t) allocator,
|
2020-10-22 10:52:33 +00:00
|
|
|
pool_buf_new, pool_buf_size);
|
2020-04-13 02:49:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 10:05:30 +00:00
|
|
|
bool
|
|
|
|
mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
|
2020-04-13 02:49:40 +00:00
|
|
|
{
|
2021-01-05 10:05:30 +00:00
|
|
|
return gc_is_heap_corrupted((gc_handle_t) allocator);
|
2020-04-13 02:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
#else /* else of DEFAULT_MEM_ALLOCATOR */
|
|
|
|
|
|
|
|
#include "tlsf/tlsf.h"
|
|
|
|
|
|
|
|
typedef struct mem_allocator_tlsf {
|
|
|
|
tlsf_t tlsf;
|
|
|
|
korp_mutex lock;
|
|
|
|
}mem_allocator_tlsf;
|
|
|
|
|
|
|
|
mem_allocator_t
|
|
|
|
mem_allocator_create(void *mem, uint32_t size)
|
|
|
|
{
|
|
|
|
mem_allocator_tlsf *allocator_tlsf;
|
|
|
|
tlsf_t tlsf;
|
|
|
|
char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
|
|
|
|
|
|
|
|
if (size < 1024) {
|
|
|
|
printf("Create mem allocator failed: pool size must be "
|
|
|
|
"at least 1024 bytes.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= mem_aligned - (char*)mem;
|
|
|
|
mem = (void*)mem_aligned;
|
|
|
|
|
|
|
|
tlsf = tlsf_create_with_pool(mem, size);
|
|
|
|
if (!tlsf) {
|
|
|
|
printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
|
|
|
|
if (!allocator_tlsf) {
|
|
|
|
printf("Create mem allocator failed: tlsf_malloc failed.\n");
|
|
|
|
tlsf_destroy(tlsf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
allocator_tlsf->tlsf = tlsf;
|
|
|
|
|
2020-03-16 08:43:57 +00:00
|
|
|
if (os_mutex_init(&allocator_tlsf->lock)) {
|
2019-05-07 02:18:18 +00:00
|
|
|
printf("Create mem allocator failed: tlsf_malloc failed.\n");
|
|
|
|
tlsf_free(tlsf, allocator_tlsf);
|
|
|
|
tlsf_destroy(tlsf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allocator_tlsf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mem_allocator_destroy(mem_allocator_t allocator)
|
|
|
|
{
|
|
|
|
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
|
|
|
|
tlsf_t tlsf = allocator_tlsf->tlsf;
|
|
|
|
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_destroy(&allocator_tlsf->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
tlsf_free(tlsf, allocator_tlsf);
|
|
|
|
tlsf_destroy(tlsf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
|
|
|
|
|
|
|
|
if (size == 0)
|
2020-03-08 13:18:18 +00:00
|
|
|
/* tlsf doesn't allow to allocate 0 byte */
|
|
|
|
size = 1;
|
2019-05-07 02:18:18 +00:00
|
|
|
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_lock(&allocator_tlsf->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
ret = tlsf_malloc(allocator_tlsf->tlsf, size);
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_unlock(&allocator_tlsf->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:18:18 +00:00
|
|
|
void *
|
|
|
|
mem_allocator_realloc(mem_allocator_t allocator, void *ptr, uint32_t size)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
/* tlsf doesn't allow to allocate 0 byte */
|
|
|
|
size = 1;
|
|
|
|
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_lock(&allocator_tlsf->lock);
|
2020-03-08 13:18:18 +00:00
|
|
|
ret = tlsf_realloc(allocator_tlsf->tlsf, ptr, size);
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_unlock(&allocator_tlsf->lock);
|
2020-03-08 13:18:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
void
|
|
|
|
mem_allocator_free(mem_allocator_t allocator, void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr) {
|
|
|
|
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_lock(&allocator_tlsf->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
tlsf_free(allocator_tlsf->tlsf, ptr);
|
2020-03-16 08:43:57 +00:00
|
|
|
os_mutex_unlock(&allocator_tlsf->lock);
|
2019-05-07 02:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 02:49:40 +00:00
|
|
|
int
|
|
|
|
mem_allocator_migrate(mem_allocator_t allocator,
|
|
|
|
mem_allocator_t allocator_old)
|
|
|
|
{
|
|
|
|
return tlsf_migrate((mem_allocator_tlsf *) allocator,
|
|
|
|
(mem_allocator_tlsf *) allocator_old);
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:18:18 +00:00
|
|
|
#endif /* end of DEFAULT_MEM_ALLOCATOR */
|
|
|
|
|