Snippet Alloc

Sevion

The DIY Ninja
Reaction score
413
Alloc

Version 1.09​

Requirements
- JASS NewGen

JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ Alloc ~~ By Sevion ~~ Version 1.09 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//  What is Alloc?
//         - Alloc implements an intuitive allocation method for array structs
//
//    =Pros=
//         - Efficient.
//         - Simple.
//         - Less overhead than regular structs.
//
//    =Cons=
//         - Must use array structs (hardly a con).
//         - Must manually call OnDestroy.
//         - Must use Delegates for inheritance.
//         - No default values for variables (use onInit instead).
//         - No array members (use another Alloc struct as a linked list or type declaration).
//
//    Methods:
//         - struct.allocate()
//         - struct.deallocate()
//
//           These methods are used just as they should be used in regular structs.
//
//    Modules:
//         - Alloc
//           Implements the most basic form of Alloc. Includes only create and destroy
//           methods.
//
//  Details:
//         - Less overhead than regular structs
//
//         - Use array structs when using Alloc. Put the implement at the top of the struct.
//
//         - Alloc operates almost exactly the same as default structs in debug mode with the exception of onDestroy.
//
//  How to import:
//         - Create a trigger named Alloc.
//         - Convert it to custom text and replace the whole trigger text with this.
//
//  Thanks:
//         - Nestharus for the method of allocation and suggestions on further merging.
//         - Bribe for suggestions like the static if and method names.
//         - PurgeandFire111 for some suggestions like the merging of Alloc and AllocX as well as OnDestroy stuff.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library Alloc    
    module Alloc
        private static integer instanceCount = 0
        private thistype recycle
    
        static method allocate takes nothing returns thistype
            local thistype this
    
            if (thistype(0).recycle == 0) then
                debug if (instanceCount == 8190) then
                    debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to allocate too many instances!")
                    debug return 0
                debug endif
                set instanceCount = instanceCount + 1
                set this = instanceCount
            else
                set this = thistype(0).recycle
                set thistype(0).recycle = thistype(0).recycle.recycle
            endif

            debug set this.recycle = -1
    
            return this
        endmethod
    
        method deallocate takes nothing returns nothing
            debug if (this.recycle != -1) then
                debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Alloc ERROR: Attempted to deallocate an invalid instance at [" + I2S(this) + "]!")
                debug return
            debug endif

            set this.recycle = thistype(0).recycle
            set thistype(0).recycle = this
        endmethod
    endmodule
endlibrary

Demonstration:
JASS:
scope test initializer dotest
    private struct teststruct extends array
        implement Alloc

        private string data

        public static method create takes nothing returns thistype
            //call BJDebugMsg("Created")
            return thistype.allocate()
        endmethod
        
        public method test takes nothing returns nothing
            call BJDebugMsg("Instance: " + I2S(this))
        endmethod

        public method destroy takes nothing returns nothing
            call BJDebugMsg("Destroying: " + I2S(this))
            call this.deallocate()
        endmethod
    endstruct
    
    private function dotest takes nothing returns nothing
        local teststruct a = teststruct.create()
        local teststruct b = teststruct.create()
        local teststruct c = teststruct.create()
        local teststruct d = teststruct.create()
        
        call a.test()
        call b.test()
        call c.test()
        call d.test()
        
        call a.destroy()
        call b.destroy()
        call c.destroy()
        call d.destroy()
        
        set a = teststruct.create()
        set b = teststruct.create()
        set c = teststruct.create()
        set d = teststruct.create()
        
        call a.test()
        call b.test()
        call c.test()
        call d.test()
    endfunction
endscope

Should be faster than all other structs. This should become the new struct standard :thup:

Updates
- Version 1.09: Moved the overflow check for some efficiency.
- Version 1.08: More debug fixes.
- Version 1.07: Fixed some debug things.
- Version 1.06: Reverted debug changes.
- Version 1.05: Fixed some debug things.
- Version 1.04: Removed OnDestroy.
- Version 1.03: Removed a boolean and made things a bit more efficient.
- Version 1.02: Minor updates. Removed AllocX and AllocXS.
- Version 1.01: Minor updates and renamed functions.
- Version 1.00: Release.
 

Sevion

The DIY Ninja
Reaction score
413
I will add the static if.

What kind of safety did you have in mind? I can't think of anything off the top of my head.

Why allocate/deallocate over create/destroy? To allow for the user to have their own? I suppose. Will do.

It's only supposed to offer a more efficient way of making structs than regular structs. What else is there to add?

That's a list, this is only supposed to be the allocate/deallocate.
 

Bribe

vJass errors are legion
Reaction score
67
Safety would fall under the category of double-free protection, protection against allocating over 8190

Right now this would throw a syntax error: this.OnDestroy.exists

needs to be thistype.OnDestroy.exists
 

Sevion

The DIY Ninja
Reaction score
413
Fixed the static if error.

Will add some safeties.

Added some. Will probably need someone's help on the double-free protection. Right now it only protects if you attempt to destroy the struct again immediately after you destroyed it the first time.
 

tooltiperror

Super Moderator
Reaction score
231
I've been meaning to write this for a really, really long time ... but you probably did it much better than I would have ever done :)

Why does it matter where you implement it? I thought JASSHelper was smart enough to move it around.
 

Bribe

vJass errors are legion
Reaction score
67
JassHelper doesn't move anything around. Take a look at any library using the AIDS struct and you'll see plenty of function evaluations because vJass isn't smart enough to do a top-sort.
 

Sevion

The DIY Ninja
Reaction score
413
It's safest to explicitly demand users to implement things at a specific place so avoid any breaks in anything in these situations.
 

Sevion

The DIY Ninja
Reaction score
413
Some minor changes and some major changes.

Made the alloc method even better. It's a tad slower, but uses fewer globals (better in the long run).

Merged all three modules into a single module.

Fixed double-free protection (debug mode only).
 

Bribe

vJass errors are legion
Reaction score
67
I don't advise thistype[0] - rather thistype(0) - a lot of different ways to (usefully) override the [] operator and ruin that command.
 

Bribe

vJass errors are legion
Reaction score
67
The deallocate method doesn't properly check for double-free. If you see, it returns when the index is allocated. It should rather be "debug if not isAllocated[this]"

You're also still using the [] operator in one of your thistype(0) lines.

Also, instead of delegating a boolean array, when an index is allocated you could set its "recycle" to -1 as Vexorian does, as the efficiency doesn't matter in debug mode.

With the changes I'm referring to, it would look like this:

JASS:

    module Alloc
        private static integer instanceCount = 0
        private thistype recycle
    
        static method allocate takes nothing returns thistype
            local thistype this
            
            debug if (instanceCount == 8190) then
                debug call BJDebugMsg("Alloc ERROR: Attempted to allocate too many instances!")
                debug return 0
            debug endif
    
            if (thistype(0).recycle == 0) then
                set this = instanceCount + 1
                set instanceCount = this
            else
                set this = thistype(0).recycle
                set thistype(0).recycle = this.recycle
            endif
    
            debug set this.recycle = -1
    
            return this
        endmethod
        
        method deallocate takes nothing returns nothing
            debug if (this.recycle != -1) then
                debug call BJDebugMsg("Alloc ERROR: Attempted to double free index [" + I2S(this) + "]")
                debug return
            debug endif
    
            static if (thistype.OnDestroy.exists) then
                call this.OnDestroy()
            endif
            set this.recycle = thistype(0).recycle
            set thistype(0).recycle = this
        endmethod
    endmodule
 

Nestharus

o-o
Reaction score
84
Ah, very nice thinking Bribe =).

But this still needs a fixer ; (.

JASS:

            static if (thistype.OnDestroy.exists) then
                call this.OnDestroy()
            endif


Also, why not just use DisplayTimedTextToPlayer =P. I know it's silly, but might as well ;D.

To make error messages easier to tell apart from regular messages, you should make them all caps. You could also give them spiffy colors since most string messages aren't colored ;D, but most people don't color their error messages so don't know about that one ;P.
 

Sevion

The DIY Ninja
Reaction score
413
I still haven't gotten around to doing anything with that. I still only partially understand what you're saying about it.

I didn't use it because I was too lazy to type it out and most people don't care anyhow.

I think the error messages are fine with just a glaring "ERROR" slapped into it. Oh and that exclamation point makes it loud.
 

Nestharus

o-o
Reaction score
84
For an extended struct (not a struct that extends a struct), from vjass (it does what I talk about)
JASS:

function sc__a_deallocate takes integer this returns nothing
    if this==null then
        return
    elseif (si__a_V[this]!=-1) then
        return
    endif
    set f__arg_this=this
    call TriggerEvaluate(st__a_onDestroy[si__a_type[this]]) //AHH!!
    set si__a_V[this]=si__a_F
    set si__a_F=this
endfunction
 

SanKakU

Member
Reaction score
21
^^ hmm...

this is similar to structs that come with vjass?

less overhead means what exactly? map performance when using structs is better?

if so,this might be very useful.
 

Sevion

The DIY Ninja
Reaction score
413
They're similar to structs, yes.

Less overhead means there's less background code generated than regular structs.

Yes, it's very useful.

You get efficiency of array structs and the syntax of regular structs that people are used to.
 

tooltiperror

Super Moderator
Reaction score
231
But you have to use delegates for inheritance, which you should mention as a con (no inheritance is a con I believe)

And you can't have default values for members in an array struct.
 

Solmyr

Ultra Cool Member
Reaction score
30
You cannot have array members either (even if you could, they wouldn't work properly). If, for some weird reason, you need them, you should use dynamic arrays instead. I guess you should mention all the drawbacks in the documentation.

Also, I think that [ljass]onDeallocate()[/ljass] is better than [ljass]OnDestroy()[/ljass].
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top