test_host_mem.c 890 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* Host backend for the malloc test: a fixed 32 MB, page-aligned arena.
  2. *
  3. * New pages are poisoned instead of zeroed, which catches an allocator that
  4. * accidentally relies on the kernel's zeroed initial mapping.
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #define HOST_ARENA_BYTES (32u * 1024u * 1024u)
  9. static unsigned char arena[HOST_ARENA_BYTES] __attribute__((aligned(4096)));
  10. static unsigned int mapped;
  11. void *__heap_map(unsigned int bytes)
  12. {
  13. if (bytes == 0 || bytes > HOST_ARENA_BYTES || mapped)
  14. return 0;
  15. mapped = bytes;
  16. memset(arena, 0xA5, bytes);
  17. return arena;
  18. }
  19. int __heap_grow(unsigned int total_bytes)
  20. {
  21. if (total_bytes > HOST_ARENA_BYTES || total_bytes < mapped)
  22. return -1;
  23. memset(arena + mapped, 0xA5, total_bytes - mapped);
  24. mapped = total_bytes;
  25. return 0;
  26. }
  27. void __heap_unmap(void)
  28. {
  29. mapped = 0;
  30. }
  31. unsigned int host_mem_mapped(void)
  32. {
  33. return mapped;
  34. }