Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

AllocT No New Posts Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

AllocT
on: February 25, 2012, 06:02:34 AM
Category: Variables
Language: vJASS

Related Topics or Resources



by

Code: jass
  1. library AllocT /* v1.0.2.0
  2. *************************************************************************************
  3. *
  4. *       */uses/*
  5. *
  6. *               */ ErrorMessage /*      https://wc3modding.info/4608/errormessage/
  7. *               */ Table                /*      https://wc3modding.info/4611/snippet-new-table/
  8. *
  9. *************************************************************************************
  10. *
  11. *       Minimizes code generation and global variables while maintaining
  12. *   excellent performance.
  13. *
  14. *   Uses hashtable instead of array, which drastically reduces performance
  15. *   but uncaps the instance limit. Should use with table fields instead of
  16. *   array fields.
  17. *
  18. *       local thistype this = recycler[0]
  19. *
  20. *       if (recycler[this] == 0) then
  21. *           set recycler[0] = this + 1
  22. *       else
  23. *           set recycler[0] = recycler[this]
  24. *       endif
  25. *
  26. ************************************************************************************
  27. *
  28. *       module AllocT
  29. *
  30. *               static method allocate takes nothing returns thistype
  31. *               method deallocate takes nothing returns nothing
  32. *
  33. *               readonly boolean isAllocated
  34. *
  35. *               debug static method calculateMemoryUsage takes nothing returns integer
  36. *               debug static method getAllocatedMemoryAsString takes nothing returns string
  37. *
  38. ************************************************************************************/
  39.         module AllocT
  40.         /*
  41.         *   stack
  42.         */
  43.                 private static Table recycler
  44.        
  45.         /*
  46.         *   list of allocated memory
  47.         */
  48.         debug private static Table allocatedNext
  49.         debug private static Table allocatedPrev
  50.        
  51.         /*
  52.         *   free memory counter
  53.         */
  54.         debug private static integer usedMemory = 0
  55.                
  56.         /*
  57.         *   allocation
  58.         */
  59.                 static method allocate takes nothing returns thistype
  60.                         local thistype this = recycler[0]
  61.                        
  62.                         debug call ThrowError(this < 0, "AllocT", "allocate", "thistype", 0, "Overflow.")
  63.            
  64.             if (recycler[this] == 0) then
  65.                 set recycler[0] = this + 1
  66.             else
  67.                 set recycler[0] = recycler[this]
  68.             endif
  69.            
  70.             set recycler[this] = -1
  71.            
  72.             debug set usedMemory = usedMemory + 1
  73.            
  74.             debug set allocatedNext[this] = 0
  75.             debug set allocatedPrev[this] = allocatedPrev[0]
  76.             debug set allocatedNext[allocatedPrev[0]] = this
  77.             debug set allocatedPrev[0] = this
  78.                        
  79.                         return this
  80.                 endmethod
  81.                
  82.                 method deallocate takes nothing returns nothing
  83.                         debug call ThrowError(recycler[this] != -1, "AllocT", "deallocate", "thistype", this, "Attempted To Deallocate Null Instance.")
  84.                        
  85.                         set recycler[this] = recycler[0]
  86.                         set recycler[0] = this
  87.            
  88.             debug set usedMemory = usedMemory - 1
  89.            
  90.             debug set allocatedNext[allocatedPrev[this]] = allocatedNext[this]
  91.             debug set allocatedPrev[allocatedNext[this]] = allocatedPrev[this]
  92.                 endmethod
  93.                
  94.         /*
  95.         *   analysis
  96.         */
  97.         method operator isAllocated takes nothing returns boolean
  98.                         return recycler[this] == -1
  99.                 endmethod
  100.        
  101.                 static if DEBUG_MODE then
  102.                         static method calculateMemoryUsage takes nothing returns integer
  103.                                 return usedMemory
  104.                         endmethod
  105.                        
  106.                         static method getAllocatedMemoryAsString takes nothing returns string
  107.                                 local integer memoryCell = allocatedNext[0]
  108.                                 local string memoryRepresentation = null
  109.                                
  110.                                 loop
  111.                                         exitwhen memoryCell == 0
  112.                    
  113.                     if (memoryRepresentation == null) then
  114.                         set memoryRepresentation = I2S(memoryCell)
  115.                     else
  116.                         set memoryRepresentation = memoryRepresentation + ", " + I2S(memoryCell)
  117.                     endif
  118.                    
  119.                     set memoryCell = allocatedNext[memoryCell]
  120.                 endloop
  121.                    
  122.                                 return memoryRepresentation
  123.                         endmethod
  124.                 endif
  125.        
  126.         /*
  127.         *   initialization
  128.         */
  129.                 private static method onInit takes nothing returns nothing
  130.             set recycler = Table.create()
  131.             debug set allocatedNext = Table.create()
  132.             debug set allocatedPrev = Table.create()
  133.            
  134.                         set recycler[0] = 1
  135.                 endmethod
  136.         endmodule
  137. endlibrary
« Last Edit: December 19, 2017, 12:36:20 AM by moyack »



 

Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...