malloc_simple.c 1.2 KB

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