Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <sys/mman.h>
  4. #include "alloc.h"
  5.  
  6.  
  7. /**
  8. * alloc.
  9. *
  10. * Allocate n bytes memory in the virtual address space.
  11. *
  12. * Example
  13. *
  14. * int* p = (int *)lalloc(sizeof(int));
  15. * *p = 1;
  16. * printf("%d\n", *p);
  17. * lfree(p, sizeof(int));
  18. *
  19. */
  20. void* lalloc(size_t bytes) {
  21. void* p = NULL;
  22.  
  23. p = mmap(
  24. 0,
  25. bytes,
  26. PROT_READ | PROT_WRITE,
  27. MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
  28. -1,
  29. 0
  30. );
  31.  
  32. return p;
  33. }
  34.  
  35. void lfree(void* p, size_t bytes) {
  36. munmap(p, bytes);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement