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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <malloc.h>
|
2015-03-22 22:08:59 +00:00
|
|
|
#include <mapmem.h>
|
2014-11-11 00:16:43 +00: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 02:27:55 +00:00
|
|
|
debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr,
|
2015-09-08 23:52:46 +00:00
|
|
|
gd->malloc_limit);
|
2016-03-07 02:27:55 +00:00
|
|
|
if (new_ptr > gd->malloc_limit) {
|
|
|
|
debug("space exhausted\n");
|
2015-02-04 12:05:50 +00:00
|
|
|
return NULL;
|
2016-03-07 02:27:55 +00:00
|
|
|
}
|
2014-11-11 00:16:43 +00:00
|
|
|
ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
|
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
|
2016-03-07 02:27:55 +00:00
|
|
|
debug("%lx\n", (ulong)ptr);
|
2015-09-08 23:52:46 +00:00
|
|
|
|
2014-11-11 00:16:43 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2015-05-12 20:55:06 +00:00
|
|
|
void *memalign_simple(size_t align, size_t bytes)
|
|
|
|
{
|
|
|
|
ulong addr, new_ptr;
|
|
|
|
void *ptr;
|
|
|
|
|
2015-08-14 19:26:43 +00:00
|
|
|
addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
|
2015-09-08 10:41:24 +00:00
|
|
|
new_ptr = addr + bytes - gd->malloc_base;
|
2017-01-27 16:39:18 +00:00
|
|
|
if (new_ptr > gd->malloc_limit) {
|
|
|
|
debug("space exhausted\n");
|
2015-05-12 20:55:06 +00:00
|
|
|
return NULL;
|
2017-01-27 16:39:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 20:55:06 +00:00
|
|
|
ptr = map_sysmem(addr, bytes);
|
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
|
2017-01-27 16:39:18 +00:00
|
|
|
debug("%lx\n", (ulong)ptr);
|
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-08-16 07:50:32 +00:00
|
|
|
if (ptr)
|
|
|
|
memset(ptr, '\0', size);
|
2014-11-11 00:16:43 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|