malloc_simple.c 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. void *memalign_simple(size_t align, size_t bytes)
  25. {
  26. ulong addr, new_ptr;
  27. void *ptr;
  28. addr = ALIGN(gd->malloc_base + gd->malloc_ptr, align);
  29. new_ptr = addr + bytes;
  30. if (new_ptr > gd->malloc_limit)
  31. return NULL;
  32. ptr = map_sysmem(addr, bytes);
  33. gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  34. return ptr;
  35. }
  36. #ifdef CONFIG_SYS_MALLOC_SIMPLE
  37. void *calloc(size_t nmemb, size_t elem_size)
  38. {
  39. size_t size = nmemb * elem_size;
  40. void *ptr;
  41. ptr = malloc(size);
  42. memset(ptr, '\0', size);
  43. return ptr;
  44. }
  45. #endif