heap_alloc.inc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2012 Zeex
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a
  4. // copy of this software and associated documentation files (the "Software"),
  5. // to deal in the Software without restriction, including without limitation
  6. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. // and/or sell copies of the Software, and to permit persons to whom the
  8. // Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. // DEALINGS IN THE SOFTWARE.
  20. #if defined HEAP_ALLOC_INC
  21. #endinput
  22. #endif
  23. #define HEAP_ALLOC_INC
  24. #define HeapAlloc HeapAllocCells
  25. // Allocates a block of (uninitialized) memory on the heap.
  26. stock HeapAllocBytes(nbytes) {
  27. new address;
  28. #emit lctrl 2
  29. #emit stor.s.pri address
  30. #emit load.s.alt nbytes
  31. #emit add
  32. #emit sctrl 2
  33. return address;
  34. }
  35. // Same as HeapAllocBytes() but operates on cells.
  36. stock HeapAllocCells(ncells) {
  37. return HeapAllocBytes(ncells * 4);
  38. }
  39. // Releases memory allocated with HeapAlloc().
  40. stock HeapRelease(address) {
  41. #emit load.s.pri address
  42. #emit sctrl 2
  43. return address;
  44. }