English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 25 January 2017, 16:40   #1
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 3,944
Allocate/deallocate memory?

What's the best practice for allocating a chunk of memory in Blitz?

I suppose that arrays are contiguous, and of course they're entirely handled by Blitz (needing less manual handling) but I don't know what the effects of allocating an array of 100 kB or more could be.

Is it better to resort to AllocVec or some other system call?

Specifically, I need some place to load machine code from disk instead of INCBIN.
idrougge is offline  
Old 26 January 2017, 13:49   #2
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 4,900
The normal system calls are probably the best bet to do it properly, however Blitz uses Banks as its internal object equivalent if you want to use them:

InitBank 1, 100000, #MEMF_ANY ; Reserve a bank as object ID #1, 100,000 bytes in size, of any memory type

MyAddr.l = Bank(1) ; MyAddr now contains the address of Bank object ID #1

The advantage of this is that it's simple to load data from disk into the memory:

LoadBank 1, "Filename.bin", #MEMF_ANY ; load the file into bank 1, which will be created at the required size

The memory type constants are in amigalibs.res.

Edit: Forgot about the freeing memory part, though it's just like any other Blitz object:

FreeBank 1

Last edited by Daedalus; 26 January 2017 at 14:05. Reason: Added freeing information
Daedalus is offline  
Old 26 January 2017, 15:10   #3
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 3,944
Thanks, and I should have actually read the manual too:
Quote:
Syntax: MemoryBlock=AllocMem(Size,Type)

Description:
Unlike calling Exec's AllocMem_ command directly Blitz2 will automatically
free any allocated memory when the program ends. Programmers are advised
to use the InitBank command.
So, free cleanup.
idrougge is offline  
Old 26 January 2017, 23:16   #4
Cylon
Registered User

 
Join Date: Oct 2014
Location: Europe
Posts: 466
Quote:
Originally Posted by Daedalus View Post
..., however Blitz uses Banks as its internal object equivalent if you want to use them...
Not completely wrong, but:

BB offers those commands since addition of Amosfunc.lib. Not before. It's for the switchers, only . (DO NOT RECOMMEND, PLEASE).

Please handle your memory allocations wise and professionally. Use the system, or the malloc() that hides inside all stringfuncs for example.
Cylon is offline  
Old 27 January 2017, 00:13   #5
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 3,944
If I was wise and professional, I wouldn't use Blitz Basic.
idrougge is offline  
Old 27 January 2017, 10:11   #6
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 4,900


Yeah, I did say it's probably best to use the system calls, but banks are just soooo convenient I've never had any problems with them, though, as with any Blitz Lib, it's impossible to say that there aren't bugs in there somewhere.
Daedalus is offline  
Old 03 February 2017, 09:55   #7
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 4,900
Just an update on this. I've been reworking the ADF-Blitzer code for low memory situations, and I've found that InitBank and LoadBank will crash if enough memory isn't available, rather than not initialising. So having a check for a valid address after initialisation is a waste of time - if you program's still running, your allocation was successful. I guess that fits with many more of Blitz's object initialisation commands, many of which also simply crash when they can't create the object.

More worryingly, Blitz's AllocMem() call does the same thing! So it has the nice automatic cleanup and all that, but instead of returning 0 when you don't have enough free RAM, it just crashes.

So I'll have to put a stronger emphasis on using the OS AllocMem_() call, since this is the only one that will fail gracefully in low memory situations.
Daedalus is offline  
Old 03 February 2017, 10:21   #8
idrougge
Registered User
 
Join Date: Sep 2007
Location: Stockholm
Posts: 3,944
I'll put that in my notes, thank you.
idrougge is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
68030 write-allocate mode TheDarkCoder Coders. General 9 20 August 2011 17:25
[SAS/C] Making malloc() allocate in fast RAM instead of chip RAM? 8bitbubsy Coders. General 3 07 August 2011 07:06
SWOS 96/97 - cannot allocate ShadowRam gingerbeardman project.WHDLoad 37 11 February 2011 17:49
WHDLOAD: Can't allocate Expmem (SKIDMARKS) Nostromo support.Games 9 15 October 2010 19:49
Could not allocate 'shadow ram'? NovaCoder project.WHDLoad 4 28 August 2008 01:09

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:48.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Page generated in 0.07966 seconds with 13 queries