2018-05-06 17:58:06 -04:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-11-10 17:16:43 -07:00
|
|
|
/*
|
|
|
|
* Simple malloc implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
2018-11-18 08:14:26 -07:00
|
|
|
#define LOG_CATEGORY LOGC_ALLOC
|
|
|
|
|
2014-11-10 17:16:43 -07:00
|
|
|
#include <common.h>
|
|
|
|
#include <malloc.h>
|
2015-03-22 17:08:59 -05:00
|
|
|
#include <mapmem.h>
|
2014-11-10 17:16:43 -07:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2018-11-18 08:14:26 -07:00
|
|
|
static void *alloc_simple(size_t bytes, int align)
|
2014-11-10 17:16:43 -07:00
|
|
|
{
|
2018-11-18 08:14:26 -07:00
|
|
|
ulong addr, new_ptr;
|
2014-11-10 17:16:43 -07:00
|
|
|
void *ptr;
|
|
|
|
|
2018-11-18 08:14:26 -07:00
|
|
|
addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
|
|
|
|
new_ptr = addr + bytes - gd->malloc_base;
|
|
|
|
log_debug("size=%zx, ptr=%lx, limit=%lx: ", bytes, new_ptr,
|
|
|
|
gd->malloc_limit);
|
2016-03-06 19:27:55 -07:00
|
|
|
if (new_ptr > gd->malloc_limit) {
|
2018-11-18 08:14:26 -07:00
|
|
|
log_err("alloc space exhausted\n");
|
2015-02-04 13:05:50 +01:00
|
|
|
return NULL;
|
2016-03-06 19:27:55 -07:00
|
|
|
}
|
2018-11-18 08:14:26 -07:00
|
|
|
|
|
|
|
ptr = map_sysmem(addr, bytes);
|
2014-11-10 17:16:43 -07:00
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
|
2015-09-08 17:52:46 -06:00
|
|
|
|
2014-11-10 17:16:43 -07:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2018-11-18 08:14:26 -07:00
|
|
|
void *malloc_simple(size_t bytes)
|
2015-05-12 14:55:06 -06:00
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
2018-11-18 08:14:26 -07:00
|
|
|
ptr = alloc_simple(bytes, 1);
|
|
|
|
if (!ptr)
|
|
|
|
return ptr;
|
2017-01-27 10:39:18 -06:00
|
|
|
|
2018-11-18 08:14:26 -07:00
|
|
|
log_debug("%lx\n", (ulong)ptr);
|
|
|
|
|
|
|
|
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);
|
2015-09-08 17:52:46 -06:00
|
|
|
|
2015-05-12 14:55:06 -06:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-09-13 14:45:15 +02:00
|
|
|
#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
|
2014-11-10 17:16:43 -07:00
|
|
|
void *calloc(size_t nmemb, size_t elem_size)
|
|
|
|
{
|
|
|
|
size_t size = nmemb * elem_size;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = malloc(size);
|
2018-11-18 08:14:26 -07:00
|
|
|
if (!ptr)
|
|
|
|
return ptr;
|
|
|
|
memset(ptr, '\0', size);
|
2014-11-10 17:16:43 -07:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-18 08:14:26 -07: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);
|
|
|
|
}
|