uboot/common/malloc_simple.c
<<
>>
Prefs
   1/*
   2 * Simple malloc implementation
   3 *
   4 * Copyright (c) 2014 Google, Inc
   5 *
   6 * SPDX-License-Identifier:     GPL-2.0+
   7 */
   8
   9#include <common.h>
  10#include <malloc.h>
  11#include <mapmem.h>
  12#include <asm/io.h>
  13
  14DECLARE_GLOBAL_DATA_PTR;
  15
  16void *malloc_simple(size_t bytes)
  17{
  18        ulong new_ptr;
  19        void *ptr;
  20
  21        new_ptr = gd->malloc_ptr + bytes;
  22        debug("%s: size=%zx, ptr=%lx, limit=%lx: ", __func__, bytes, new_ptr,
  23              gd->malloc_limit);
  24        if (new_ptr > gd->malloc_limit) {
  25                debug("space exhausted\n");
  26                return NULL;
  27        }
  28        ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
  29        gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  30        debug("%lx\n", (ulong)ptr);
  31
  32        return ptr;
  33}
  34
  35void *memalign_simple(size_t align, size_t bytes)
  36{
  37        ulong addr, new_ptr;
  38        void *ptr;
  39
  40        addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
  41        new_ptr = addr + bytes - gd->malloc_base;
  42        if (new_ptr > gd->malloc_limit)
  43                return NULL;
  44        ptr = map_sysmem(addr, bytes);
  45        gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  46
  47        return ptr;
  48}
  49
  50#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
  51void *calloc(size_t nmemb, size_t elem_size)
  52{
  53        size_t size = nmemb * elem_size;
  54        void *ptr;
  55
  56        ptr = malloc(size);
  57        memset(ptr, '\0', size);
  58
  59        return ptr;
  60}
  61#endif
  62