2014-11-11 00:16:43 +00:00
|
|
|
/*
|
|
|
|
* Simple malloc implementation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Google, Inc
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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;
|
2015-09-08 23:52:46 +00:00
|
|
|
debug("%s: size=%zx, ptr=%lx, limit=%lx\n", __func__, bytes, new_ptr,
|
|
|
|
gd->malloc_limit);
|
2014-11-11 00:16:43 +00:00
|
|
|
if (new_ptr > gd->malloc_limit)
|
2015-02-04 12:05:50 +00:00
|
|
|
return NULL;
|
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));
|
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;
|
2015-05-12 20:55:06 +00:00
|
|
|
if (new_ptr > gd->malloc_limit)
|
|
|
|
return NULL;
|
|
|
|
ptr = map_sysmem(addr, bytes);
|
|
|
|
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_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);
|
|
|
|
memset(ptr, '\0', size);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|