Advertisement
tinyevil

Untitled

Aug 17th, 2019
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. function allocate_1(size: uword): (ptr u8, uword) {
  2. var block_size: uword = size + sizeof(uword);
  3. var {p, s} = allocate_2(block_size);
  4. *(p as ptr uword) = block_size;
  5. return p + sizeof(uword), s - sizeof(uword);
  6. }
  7.  
  8. function reallocate_1(p: ptr u8, new_size: uword): (ptr u8, uword) {
  9. var block_p: ptr u8 = p - sizeof(uword);
  10. var block_size: uword = *(block_p as ptr uword);
  11. var new_block_size: uword = new_size + sizeof(uword);
  12. var {new_p, new_s} = reallocate_2(block_p, block_size, new_block_size);
  13. *(new_p as ptr uword) = new_block_size;
  14. memcpy(new_p + sizeof(uword), p, std::min(block_size - sizeof(uword), new_size));
  15. return new_p + sizeof(uword), new_s - sizeof(uword);
  16. }
  17.  
  18. function deallocate_1(p: ptr u8) {
  19. var block_p: ptr u8 = p - sizeof(uword);
  20. var block_size: uword = *(block_p as ptr uword);
  21. deallocate_2(block_p, block_size);
  22. }
  23.  
  24. function allocate_2(size: uword): (ptr u8, uword) {
  25. return allocate_1(size);
  26. }
  27.  
  28. function reallocate_2(p: ptr u8, old_size: uword, new_size: uword): (ptr u8, uword) {
  29. return reallocate_1(p, new_size);
  30. }
  31.  
  32. function deallocate_2(p: ptr u8, size: uword) {
  33. return deallocate_1(p);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement