Snippet Garbage Collector

Nestharus

o-o
Reaction score
84
Nevermind, had done 50 different tests so I was pretty sure of myself, but just did another proving this wrong : \.

I had done about 2 hours worth of tests, so I was rather sure of myself, lol.

If I could delete this message I would ><

JASS:

library Garbage
/*Utility Information
===================================================================
Name: Garbage Collector
Version: 1.0
Author: Nestharus

Settings:
*///===================================================================
globals
    constant real GARBAGE_TIMEOUT = .1
endglobals
/*//===================================================================

Description: 
    What it does-
        This will create garbage collectors for types of handles. The reason I made
        this was because warcraft 3 has such a terrible garbage collector. It doesn&#039;t
        even work when you put in the right things. You can create unlimited handles
        of a type on the same thread or destroy unlimited handles of a type on the same
        thread, not both. You can only create/destroy one handle of a type on a thread.
        The purpose of this garbage collection utility is for use in cases where you might
        be creating and destroying multiple handles of a type in the same thread. I&#039;ve made
        and I&#039;ve seen quite a few systems that do this. What bugs out when you create/destroy
        multiple handles of the same type? The handle counter. If you create 5 handles and destroy
        5 handles on the same thread, the handle counter will go up 5 times but will only
        go down one time, the time for the last handle. This same issue occurs for thread
        allocation, and so executing multiple threads on the same thread will also merit the same
        results, that is, permanent memory leaks and more memory usage by warcraft 3. Threads are
        bigger than small handle counters also.
        
    How it does it-
        Whenever you want to destroy something, it destroys it later on a periodic timer. It
        adds the thing that needs to be destroyed to an array and increases the index by 1. Whenever
        the timer fires, it enters a loop and exits when the index is 0. What this means is that
        it splits up the creation and destruction of handles.
        
    When to use it-
        Whenever you are going to be creating and destroying multiple handles of the same type
        on the same thread.

Requirements: NA

Installation: NA

Variables:
-GARBAGE_TIMEOUT
    Determines how often the garbage collector runs. The more often,
    the more often things will be destroyed, but the more of a performance
    hit.

Functions:
------------------------------------------------------------------
-CREATE_GARBAGE_COLLECTOR(&quot;NAME&quot;, &quot;TYPE&quot;, &quot;REMOVAL&quot;)
    The create garbage collector text macro will create a garbage
    collector for a given type.
    //! runtextmacro CREATE_GARBAGE_COLLECTOR(&quot;Units&quot;, &quot;unit&quot;, &quot;RemoveUnit&quot;)
    
-$NAME$_Destroy($TYPE$)
    Will add something to the garbage collector of a type. It will
    be destroyed the next time the garbage collector runs.
    
    call Units_Destroy(u)
===================================================================*/
    //! textmacro CREATE_GARBAGE_COLLECTOR takes NAME, TYPE, REMOVAL
        scope $NAME$ initializer Initialization
            globals
                private timer garbageCollector = CreateTimer()
                private $TYPE$ array handles
                private integer handleIndex = 0
            endglobals
            
            public function Destroy takes $TYPE$ h returns nothing
                set handleIndex = handleIndex + 1
                set handles[handleIndex] = h
            endfunction
            
            private function GarbageCollector takes nothing returns nothing
                loop
                    exitwhen handleIndex == 0
                    call $REMOVAL$(handles[hIndex])
                    set handles[handleIndex] = null
                    set handleIndex = handleIndex - 1
                endloop
            endfunction
            
            private function Initialization takes nothing returns nothing
                call TimerStart(garbageCollector, GARBAGE_TIMEOUT, true, function GarbageCollector)
            endfunction
        endscope
    //! endtextmacro
endlibrary
 

Jesus4Lyf

Good Idea™
Reaction score
397
Well that was amusing. Graveyarded.

Indeed, creating and destroying handles works fine, but the handles get freed a moment after the handle is destroyed, not immediately. Hence destroying and then getting a handlecount will not include the handles recently destroyed, you need a wait of some sort first. :D
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top