Advertisement
Guest User

Untitled

a guest
Nov 13th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. void *my_malloc(size_t size)
  2. {
  3. char *unaligned;
  4. char *aligned;
  5.  
  6. if (size == 0)
  7. return NULL;
  8.  
  9. unaligned = malloc(size+16);
  10. if (unaligned == NULL)
  11. return NULL;
  12.  
  13. aligned = (char *)((intptr_t)(unaligned + 16) & ~15);
  14. aligned[-1] = (char)(unaligned - aligned);
  15.  
  16. return aligned;
  17. }
  18.  
  19. void my_free(void *aligned)
  20. {
  21. if (aligned == NULL)
  22. return;
  23.  
  24. free(aligned - aligned[-1]);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement