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

AllocQ No New Posts Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

AllocQ
on: March 01, 2012, 04:57:56 PM
Category: Variables
Language: vJASS

Related Topics or Resources



by

Code: jass
  1. library AllocQ /* v1.0.1.0
  2. *************************************************************************************
  3. *
  4. *       */ uses /*
  5. *
  6. *               */ ErrorMessage /*
  7. *
  8. *************************************************************************************
  9. *
  10. *       Maximizes speed by reducing local variable declarations and removing
  11. *   if-statement.
  12. *
  13. *   Uses a queue instead of a stack for recycler. Using a queue requires one
  14. *   extra variable declaration.
  15. *
  16. *       set alloc = recycler[0]
  17. *       set recycler[0] = recycler[alloc]
  18. *
  19. ************************************************************************************
  20. *
  21. *       module AllocQFast
  22. *
  23. *               Fields
  24. *               -------------------------
  25. *
  26. *                       readonly boolean isAllocated
  27. *
  28. *               Methods
  29. *               -------------------------
  30. *
  31. *                       static method allocate takes nothing returns thistype
  32. *                       method deallocate takes nothing returns nothing
  33. *
  34. *                       debug static method calculateMemoryUsage takes nothing returns integer
  35. *                       debug static method getAllocatedMemoryAsString takes nothing returns string
  36. *
  37. ************************************************************************************/
  38.         module AllocQ
  39.         /*
  40.         *   stack
  41.         */
  42.                 private static integer array recycler
  43.         private static integer alloc
  44.         private static integer last = 8191
  45.        
  46.         /*
  47.         *   list of allocated memory
  48.         */
  49.         debug private static integer array allocatedNext
  50.         debug private static integer array allocatedPrev
  51.        
  52.         /*
  53.         *   free memory counter
  54.         */
  55.         debug private static integer usedMemory = 0
  56.                
  57.         /*
  58.         *   allocation
  59.         */
  60.                 static method allocate takes nothing returns thistype
  61.                         set alloc = recycler[0]
  62.                        
  63.                         debug call ThrowError(alloc == 0, "AllocQ", "allocate", "thistype", 0, "Overflow.")
  64.            
  65.             set recycler[0] = recycler[alloc]
  66.            
  67.             set recycler[alloc] = -1
  68.            
  69.             debug set usedMemory = usedMemory + 1
  70.            
  71.             debug set allocatedNext[alloc] = 0
  72.             debug set allocatedPrev[alloc] = allocatedPrev[0]
  73.             debug set allocatedNext[allocatedPrev[0]] = alloc
  74.             debug set allocatedPrev[0] = alloc
  75.                        
  76.                         return alloc
  77.                 endmethod
  78.                
  79.                 method deallocate takes nothing returns nothing
  80.                         debug call ThrowError(recycler[this] != -1, "AllocQ", "deallocate", "thistype", this, "Attempted To Deallocate Null Instance.")
  81.                        
  82.                         set recycler[last] = this
  83.             set recycler[this] = 0
  84.                         set last = this
  85.            
  86.             debug set usedMemory = usedMemory - 1
  87.            
  88.             debug set allocatedNext[allocatedPrev[this]] = allocatedNext[this]
  89.             debug set allocatedPrev[allocatedNext[this]] = allocatedPrev[this]
  90.                 endmethod
  91.                
  92.         /*
  93.         *   analysis
  94.         */
  95.         method operator isAllocated takes nothing returns boolean
  96.                         return recycler[this] == -1
  97.                 endmethod
  98.        
  99.                 static if DEBUG_MODE then
  100.                         static method calculateMemoryUsage takes nothing returns integer
  101.                                 return usedMemory
  102.                         endmethod
  103.                        
  104.                         static method getAllocatedMemoryAsString takes nothing returns string
  105.                                 local integer memoryCell = allocatedNext[0]
  106.                                 local string memoryRepresentation = null
  107.                                
  108.                                 loop
  109.                                         exitwhen memoryCell == 0
  110.                    
  111.                     if (memoryRepresentation == null) then
  112.                         set memoryRepresentation = I2S(memoryCell)
  113.                     else
  114.                         set memoryRepresentation = memoryRepresentation + ", " + I2S(memoryCell)
  115.                     endif
  116.                    
  117.                     set memoryCell = allocatedNext[memoryCell]
  118.                 endloop
  119.                    
  120.                                 return memoryRepresentation
  121.                         endmethod
  122.                 endif
  123.        
  124.         /*
  125.         *   initialization
  126.         */
  127.                 private static method onInit takes nothing returns nothing
  128.                         local integer i = 0
  129.  
  130.                         set recycler[8191] = 0 //so that the array doesn't reallocate over and over again
  131.            
  132.                         loop
  133.                                 set recycler[i] = i + 1
  134.                                
  135.                                 exitwhen i == 8190
  136.                                 set i = i + 1
  137.                         endloop
  138.                 endmethod
  139.         endmodule
  140. endlibrary
« Last Edit: December 19, 2017, 12:30:11 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...