2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-11-11 00:16:43 +00:00
|
|
|
/*
|
|
|
|
* Simple malloc implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
2018-11-18 15:14:26 +00:00
|
|
|
#define LOG_CATEGORY LOGC_ALLOC
|
|
|
|
|
2014-11-11 00:16:43 +00:00
|
|
|
#include <common.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2014-11-11 00:16:43 +00:00
|
|
|
#include <malloc.h>
|
2015-03-22 22:08:59 +00:00
|
|
|
#include <mapmem.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2014-11-11 00:16:43 +00:00
|
|
|
#include <asm/io.h>
|
2022-03-23 18:04:49 +00:00
|
|
|
#include <valgrind/valgrind.h>
|
2014-11-11 00:16:43 +00:00
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2018-11-18 15:14:26 +00:00
|
|
|
static void *alloc_simple(size_t bytes, int align)
|
2014-11-11 00:16:43 +00:00
|
|
|
{
|
2018-11-18 15:14:26 +00:00
|
|
|
ulong addr, new_ptr;
|
2014-11-11 00:16:43 +00:00
|
|
|
void *ptr;
|
|
|
|
|
2018-11-18 15:14:26 +00:00
|
|
|
addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
|
|
|
|
new_ptr = addr + bytes - gd->malloc_base;
|
2022-02-03 18:51:37 +00:00
|
|
|
log_debug("size=%lx, ptr=%lx, limit=%lx: ", (ulong)bytes, new_ptr,
|
2018-11-18 15:14:26 +00:00
|
|
|
gd->malloc_limit);
|
2016-03-07 02:27:55 +00:00
|
|
|
if (new_ptr > gd->malloc_limit) {
|
2018-11-18 15:14:26 +00:00
|
|
|
log_err("alloc space exhausted\n");
|
2015-02-04 12:05:50 +00:00
|
|
|
return NULL;
|
2016-03-07 02:27:55 +00:00
|
|
|
}
|
2018-11-18 15:14:26 +00:00
|
|
|
|
|
|
|
ptr = map_sysmem(addr, bytes);
|
2014-11-11 00:16:43 +00:00
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
|
2015-09-08 23:52:46 +00:00
|
|
|
|
2014-11-11 00:16:43 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-11-18 15:14:26 +00:00
|
|
|
void *malloc_simple(size_t bytes)
|
2015-05-12 20:55:06 +00:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2018-11-18 15:14:26 +00:00
|
|
|
ptr = alloc_simple(bytes, 1);
|
|
|
|
if (!ptr)
|
|
|
|
return ptr;
|
2017-01-27 16:39:18 +00:00
|
|
|
|
2018-11-18 15:14:26 +00:00
|
|
|
log_debug("%lx\n", (ulong)ptr);
|
2022-03-23 18:04:49 +00:00
|
|
|
VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
|
2018-11-18 15:14:26 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *memalign_simple(size_t align, size_t bytes)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = alloc_simple(bytes, align);
|
|
|
|
if (!ptr)
|
|
|
|
return ptr;
|
|
|
|
log_debug("aligned to %lx\n", (ulong)ptr);
|
2022-03-23 18:04:49 +00:00
|
|
|
VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
|
2015-09-08 23:52:46 +00:00
|
|
|
|
2015-05-12 20:55:06 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-09-13 12:45:15 +00:00
|
|
|
#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
|
2014-11-11 00:16:43 +00:00
|
|
|
void *calloc(size_t nmemb, size_t elem_size)
|
|
|
|
{
|
|
|
|
size_t size = nmemb * elem_size;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = malloc(size);
|
2018-11-18 15:14:26 +00:00
|
|
|
if (!ptr)
|
|
|
|
return ptr;
|
|
|
|
memset(ptr, '\0', size);
|
2014-11-11 00:16:43 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
2022-03-23 18:04:49 +00:00
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_VALGRIND)
|
|
|
|
void free_simple(void *ptr)
|
|
|
|
{
|
|
|
|
VALGRIND_FREELIKE_BLOCK(ptr, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-11 00:16:43 +00:00
|
|
|
#endif
|
2018-11-18 15:14:26 +00:00
|
|
|
|
|
|
|
void malloc_simple_info(void)
|
|
|
|
{
|
|
|
|
log_info("malloc_simple: %lx bytes used, %lx remain\n", gd->malloc_ptr,
|
|
|
|
CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);
|
|
|
|
}
|