malloc_simple.c 696 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 <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. if (new_ptr > gd->malloc_limit)
  18. panic("Out of pre-reloc memory");
  19. ptr = map_sysmem(gd->malloc_base + gd->malloc_ptr, bytes);
  20. gd->malloc_ptr = ALIGN(new_ptr, sizeof(new_ptr));
  21. return ptr;
  22. }
  23. #ifdef CONFIG_SYS_MALLOC_SIMPLE
  24. void *calloc(size_t nmemb, size_t elem_size)
  25. {
  26. size_t size = nmemb * elem_size;
  27. void *ptr;
  28. ptr = malloc(size);
  29. memset(ptr, '\0', size);
  30. return ptr;
  31. }
  32. #endif