2018-05-07 05:58:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-11-11 08:16:43 +08:00
|
|
|
/*
|
|
|
|
* Simple malloc implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <malloc.h>
|
2015-03-23 06:08:59 +08:00
|
|
|
#include <mapmem.h>
|
2014-11-11 08:16:43 +08:00
|
|
|
#include <asm/io.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
void *malloc_simple(size_t bytes)
|
|
|
|
{
|
|
|
|
ulong new_ptr;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
new_ptr = gd->malloc_ptr + bytes;
|
2016-03-07 10:27:55 +08:00
|
|
|
debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr,
|
2015-09-09 07:52:46 +08:00
|
|
|
gd->malloc_limit);
|
2016-03-07 10:27:55 +08:00
|
|
|
if (new_ptr > gd->malloc_limit) {
|
|
|
|
debug("space exhausted\n");
|
2015-02-04 20:05:50 +08:00
|
|
|
return NULL;
|
2016-03-07 10:27:55 +08:00
|
|
|
}
|
2014-11-11 08:16:43 +08:00
|
|
|
ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
|
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
|
2016-03-07 10:27:55 +08:00
|
|
|
debug("%lx\n", (ulong)ptr);
|
2015-09-09 07:52:46 +08:00
|
|
|
|
2014-11-11 08:16:43 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-05-13 04:55:06 +08:00
|
|
|
void *memalign_simple(size_t align, size_t bytes)
|
|
|
|
{
|
|
|
|
ulong addr, new_ptr;
|
|
|
|
void *ptr;
|
|
|
|
|
2015-08-15 03:26:43 +08:00
|
|
|
addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
|
2015-09-08 18:41:24 +08:00
|
|
|
new_ptr = addr + bytes - gd->malloc_base;
|
2017-01-28 00:39:18 +08:00
|
|
|
if (new_ptr > gd->malloc_limit) {
|
|
|
|
debug("space exhausted\n");
|
2015-05-13 04:55:06 +08:00
|
|
|
return NULL;
|
2017-01-28 00:39:18 +08:00
|
|
|
}
|
|
|
|
|
2015-05-13 04:55:06 +08:00
|
|
|
ptr = map_sysmem(addr, bytes);
|
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
|
2017-01-28 00:39:18 +08:00
|
|
|
debug("%lx\n", (ulong)ptr);
|
2015-09-09 07:52:46 +08:00
|
|
|
|
2015-05-13 04:55:06 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-09-13 20:45:15 +08:00
|
|
|
#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
|
2014-11-11 08:16:43 +08:00
|
|
|
void *calloc(size_t nmemb, size_t elem_size)
|
|
|
|
{
|
|
|
|
size_t size = nmemb * elem_size;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = malloc(size);
|
2018-08-16 15:50:32 +08:00
|
|
|
if (ptr)
|
|
|
|
memset(ptr, '\0', size);
|
2014-11-11 08:16:43 +08:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|