malloc_simple.c 695 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Simple malloc implementation
  3. *
  4. * Copyright (c) 2014 Google, Inc
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <mapmem.h>
  11. #include <asm/io.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. void *malloc_simple(size_t bytes)
  14. {
  15. ulong new_ptr;
  16. void *ptr;
  17. new_ptr = gd->malloc_ptr + bytes;
  18. if (new_ptr > gd->malloc_limit)
  19. return NULL;
  20. ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
  21. gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  22. return ptr;
  23. }
  24. #ifdef CONFIG_SYS_MALLOC_SIMPLE
  25. void *calloc(size_t nmemb, size_t elem_size)
  26. {
  27. size_t size = nmemb * elem_size;
  28. void *ptr;
  29. ptr = malloc(size);
  30. memset(ptr, '\0', size);
  31. return ptr;
  32. }
  33. #endif