X-Git-Url: https://git.librecmc.org/?p=oweals%2Fu-boot.git;a=blobdiff_plain;f=common%2Fmalloc_simple.c;h=eabbb70128b375286691489fe2248bb94b1296f1;hp=871b5444bd7d89e48eef21527d172927788d2e40;hb=b77d0292ca9f3ca69259dca7e2c5e193a403b289;hpb=a376702f761be51227bfc8d0ae06722ec40687d8 diff --git a/common/malloc_simple.c b/common/malloc_simple.c index 871b5444bd..eabbb70128 100644 --- a/common/malloc_simple.c +++ b/common/malloc_simple.c @@ -5,6 +5,8 @@ * Copyright (c) 2014 Google, Inc */ +#define LOG_CATEGORY LOGC_ALLOC + #include #include #include @@ -12,40 +14,47 @@ DECLARE_GLOBAL_DATA_PTR; -void *malloc_simple(size_t bytes) +static void *alloc_simple(size_t bytes, int align) { - ulong new_ptr; + ulong addr, new_ptr; void *ptr; - new_ptr = gd->malloc_ptr + bytes; - debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr, - gd->malloc_limit); + 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); if (new_ptr > gd->malloc_limit) { - debug("space exhausted\n"); + log_err("alloc space exhausted\n"); return NULL; } - ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes); + + ptr = map_sysmem(addr, bytes); gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); - debug("%lx\n", (ulong)ptr); return ptr; } -void *memalign_simple(size_t align, size_t bytes) +void *malloc_simple(size_t bytes) { - ulong addr, new_ptr; void *ptr; - addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align); - new_ptr = addr + bytes - gd->malloc_base; - if (new_ptr > gd->malloc_limit) { - debug("space exhausted\n"); - return NULL; - } + ptr = alloc_simple(bytes, 1); + if (!ptr) + return ptr; - ptr = map_sysmem(addr, bytes); - gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); - debug("%lx\n", (ulong)ptr); + 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); return ptr; } @@ -57,9 +66,16 @@ void *calloc(size_t nmemb, size_t elem_size) void *ptr; ptr = malloc(size); - if (ptr) - memset(ptr, '\0', size); + if (!ptr) + return ptr; + memset(ptr, '\0', size); return ptr; } #endif + +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); +}