• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

my alloc

Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
library AllocLib
/*
    AllocLib library
    version 1.1
    by edo494
   
    Alloc Example, includes debug messages with struct names that failed to load instance.
    
    Double free protection is present in debug and in release mode, so is overflow.
    However, messages are only print if debug mode is on.
   
*/
    module Alloc
        private static integer allocCount = 0
        private static integer array allocList
        static method allocate takes nothing returns thistype
            local thistype this = allocList[0]
            if this == 0 then
                if allocCount >= 8190 then
                    debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "cannot allocate struct thistype(overflow)")
                    return 0
                endif
                set this = allocCount + 1
                set allocCount = this
            else
                set allocList[0] = allocList[this]
            endif
            set allocList[this] = -1
            return this
        endmethod
        method deallocate takes nothing returns nothing
            if allocList[this] == -1 then
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "cannot deallocate struct thistype(already deallocated)")
                return
            endif
            set allocList[this] = allocList[0]
            set allocList[0] = this
        endmethod
    endmodule
endlibrary

Original:


JASS:
library AllocLib
/*
    AllocLib library
    version 1.0
    by edo494
    
    This alloc is as simple as you can get, without any debug or nondebug protection.
    
    The only problem that can rise is that you will get index higher then 8191.
    
    If you still want to use Nestharuses Alloc, or someone elses, you still can, the name is the
    same for this as for Nes' module, so removing this and implementing his will result in no 
    required modifications for any of the libraries made by me.
    
*/
    module Alloc
        private static integer count = 0
        private static integer array list
        static method allocate takes nothing returns thistype
            local thistype this = list[0]
            if this == 0 then
                set this = count + 1
                set count = this
            elseif this <= 8191 then
                set list[0] = list[this]
            else
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 10, "cannot allocate struct thistype(overflow)")
                return 0
            endif
            set list[this] = -1
            return this
        endmethod
        method deallocate takes nothing returns nothing
            if list[this] == -1 then
                return
            endif
            set list[this] = list[0]
            set list[0] = this
        endmethod
    endmodule
endlibrary

Last edited:
Top