]> Git Repo - J-u-boot.git/blob - common/malloc_simple.c
board: gateworks: venice: rename GW7905 to GW7500
[J-u-boot.git] / common / malloc_simple.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Simple malloc implementation
4  *
5  * Copyright (c) 2014 Google, Inc
6  */
7
8 #define LOG_CATEGORY LOGC_ALLOC
9
10 #include <log.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <asm/global_data.h>
14 #include <asm/io.h>
15 #include <valgrind/valgrind.h>
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 static void *alloc_simple(size_t bytes, int align)
20 {
21         ulong addr, new_ptr;
22         void *ptr;
23
24         addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
25         new_ptr = addr + bytes - gd->malloc_base;
26         log_debug("size=%lx, ptr=%lx, limit=%x: ", (ulong)bytes, new_ptr,
27                   gd->malloc_limit);
28         if (new_ptr > gd->malloc_limit) {
29                 log_err("alloc space exhausted\n");
30                 return NULL;
31         }
32
33         ptr = map_sysmem(addr, bytes);
34         gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
35
36         return ptr;
37 }
38
39 void *malloc_simple(size_t bytes)
40 {
41         void *ptr;
42
43         ptr = alloc_simple(bytes, 1);
44         if (!ptr)
45                 return ptr;
46
47         log_debug("%lx\n", (ulong)ptr);
48         VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
49
50         return ptr;
51 }
52
53 void *memalign_simple(size_t align, size_t bytes)
54 {
55         void *ptr;
56
57         ptr = alloc_simple(bytes, align);
58         if (!ptr)
59                 return ptr;
60         log_debug("aligned to %lx\n", (ulong)ptr);
61         VALGRIND_MALLOCLIKE_BLOCK(ptr, bytes, 0, false);
62
63         return ptr;
64 }
65
66 #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
67 void *calloc(size_t nmemb, size_t elem_size)
68 {
69         size_t size = nmemb * elem_size;
70         void *ptr;
71
72         ptr = malloc(size);
73         if (!ptr)
74                 return ptr;
75         memset(ptr, '\0', size);
76
77         return ptr;
78 }
79
80 #if IS_ENABLED(CONFIG_VALGRIND)
81 void free_simple(void *ptr)
82 {
83         VALGRIND_FREELIKE_BLOCK(ptr, 0);
84 }
85 #endif
86 #endif
87
88 void malloc_simple_info(void)
89 {
90         log_info("malloc_simple: %x bytes used, %x remain\n", gd->malloc_ptr,
91                  CONFIG_VAL(SYS_MALLOC_F_LEN) - gd->malloc_ptr);
92 }
This page took 0.030043 seconds and 4 git commands to generate.