fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stddef.h>
  4. #include <unistd.h>
  5. #include <sys/mman.h>
  6. #include <vector>
  7.  
  8. void* malloc(size_t size) {
  9. write(STDOUT_FILENO, "malloc... ", 10);
  10. size += sizeof(size_t);
  11. int page_size = getpagesize();
  12. int rem = size % page_size;
  13. if (rem > 0) {
  14. size += page_size - rem;
  15. }
  16. void* addr = mmap(0, size, PROT_READ | PROT_WRITE,
  17. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  18. if (addr == MAP_FAILED) {
  19. write(STDOUT_FILENO, "fail\n", 5);
  20. return NULL;
  21. }
  22. write(STDOUT_FILENO, "ok\n", 3);
  23. *(size_t*)addr = size;
  24. return (size_t*)addr + 1;
  25. }
  26.  
  27. void free (void *ptr) {
  28. write(STDOUT_FILENO, "free... ", 8);
  29. size_t* real_ptr = (size_t*)ptr - 1;
  30. if (!munmap(real_ptr, *real_ptr)) {
  31. write(STDOUT_FILENO, "ok\n", 3);
  32. } else {
  33. write(STDOUT_FILENO, "fail\n", 5);
  34. }
  35. }
  36.  
  37.  
  38. int main() {
  39. std::vector<int> a;
  40. a.push_back(42);
  41. a.push_back(56);
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 4520KB
stdin
Standard input is empty
stdout
malloc... ok
malloc... ok
malloc... ok
free... ok
free... ok