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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top