malloc_simple.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. debug("%s: size=%zx, ptr=%lx, limit=%lx\n", __func__, bytes, new_ptr,
  19. gd->malloc_limit);
  20. if (new_ptr > gd->malloc_limit)
  21. return NULL;
  22. ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
  23. gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  24. return ptr;
  25. }
  26. void *memalign_simple(size_t align, size_t bytes)
  27. {
  28. ulong addr, new_ptr;
  29. void *ptr;
  30. addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
  31. new_ptr = addr + bytes - gd->malloc_base;
  32. if (new_ptr > gd->malloc_limit)
  33. return NULL;
  34. ptr = map_sysmem(addr, bytes);
  35. gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  36. return ptr;
  37. }
  38. #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
  39. void *calloc(size_t nmemb, size_t elem_size)
  40. {
  41. size_t size = nmemb * elem_size;
  42. void *ptr;
  43. ptr = malloc(size);
  44. memset(ptr, '\0', size);
  45. return ptr;
  46. }
  47. #endif