Guest User

Untitled

a guest
May 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. const MAX_INSTANCES = 65535
  2. let freed = 0
  3. let count = 0
  4. let table = []
  5.  
  6. function alloc() {
  7. let ref = freed
  8. if (ref != 0) {
  9. freed = table[ref]
  10. } else {
  11. count = count + 1
  12. ref = count
  13. }
  14. if (ref > MAX_INSTANCES) {
  15. return 0
  16. }
  17. table[ref] = -1
  18. return ref
  19. }
  20.  
  21. function free(ref) {
  22. if (ref == 0) {
  23. return
  24. } else if (table[ref] != -1) {
  25. return
  26. }
  27. table[ref] = freed
  28. freed = ref
  29. }
Add Comment
Please, Sign In to add comment