malloc_simple.c 1.2 KB

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