Blame view
common/malloc_simple.c
1.41 KB
83d290c56 SPDX: Convert all... |
1 |
// SPDX-License-Identifier: GPL-2.0+ |
c9356be30 dm: Split the sim... |
2 3 4 5 |
/* * Simple malloc implementation * * Copyright (c) 2014 Google, Inc |
c9356be30 dm: Split the sim... |
6 |
*/ |
7cbd2d2e3 malloc_simple: Ad... |
7 |
#define LOG_CATEGORY LOGC_ALLOC |
c9356be30 dm: Split the sim... |
8 9 |
#include <common.h> #include <malloc.h> |
0eb25b619 common: Make sure... |
10 |
#include <mapmem.h> |
c9356be30 dm: Split the sim... |
11 12 13 |
#include <asm/io.h> DECLARE_GLOBAL_DATA_PTR; |
7cbd2d2e3 malloc_simple: Ad... |
14 |
static void *alloc_simple(size_t bytes, int align) |
c9356be30 dm: Split the sim... |
15 |
{ |
7cbd2d2e3 malloc_simple: Ad... |
16 |
ulong addr, new_ptr; |
c9356be30 dm: Split the sim... |
17 |
void *ptr; |
7cbd2d2e3 malloc_simple: Ad... |
18 19 20 21 |
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); |
9a01cca75 malloc_simple: Ad... |
22 |
if (new_ptr > gd->malloc_limit) { |
7cbd2d2e3 malloc_simple: Ad... |
23 24 |
log_err("alloc space exhausted "); |
2c8571703 malloc_simple: Re... |
25 |
return NULL; |
9a01cca75 malloc_simple: Ad... |
26 |
} |
7cbd2d2e3 malloc_simple: Ad... |
27 28 |
ptr = map_sysmem(addr, bytes); |
c9356be30 dm: Split the sim... |
29 |
gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr)); |
836ac74c2 malloc_simple: Ad... |
30 |
|
c9356be30 dm: Split the sim... |
31 32 |
return ptr; } |
7cbd2d2e3 malloc_simple: Ad... |
33 |
void *malloc_simple(size_t bytes) |
b6bfb6ff9 Add a simple vers... |
34 |
{ |
b6bfb6ff9 Add a simple vers... |
35 |
void *ptr; |
7cbd2d2e3 malloc_simple: Ad... |
36 37 38 |
ptr = alloc_simple(bytes, 1); if (!ptr) return ptr; |
1923d54bf malloc_simple: Ad... |
39 |
|
7cbd2d2e3 malloc_simple: Ad... |
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
log_debug("%lx ", (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 ", (ulong)ptr); |
836ac74c2 malloc_simple: Ad... |
55 |
|
b6bfb6ff9 Add a simple vers... |
56 57 |
return ptr; } |
1eb0c03c2 malloc_simple: Ad... |
58 |
#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) |
c9356be30 dm: Split the sim... |
59 60 61 62 63 64 |
void *calloc(size_t nmemb, size_t elem_size) { size_t size = nmemb * elem_size; void *ptr; ptr = malloc(size); |
7cbd2d2e3 malloc_simple: Ad... |
65 66 67 |
if (!ptr) return ptr; memset(ptr, '\0', size); |
c9356be30 dm: Split the sim... |
68 69 70 71 |
return ptr; } #endif |
7cbd2d2e3 malloc_simple: Ad... |
72 73 74 75 76 77 78 |
void malloc_simple_info(void) { log_info("malloc_simple: %lx bytes used, %lx remain ", gd->malloc_ptr, CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr); } |