From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2001-03-17 23:27:53
On Sat, Mar 17, 2001 at 03:47:35PM -0600, Jim Lawless wrote:
> I'm using CC65 for C64 coding. How does one detect the first unused
> byte of RAM? ( I'd like to define some variables, then use the rest
> of the C64's RAM as a continguous block of memory. )
There are several possible solutions. The first one would be to use a short
assembler file like this:
.export _freemem;
.import __BSS_RUN__, __BSS_SIZE__
_freemem = __BSS_RUN__ + __BSS_SIZE__
In C, you use
extern unsigned char freemem[];
freemem[0] is then the first byte of the free memory area.
Please note that you may not use malloc/free with this method, because these
routines will also use the free memory between the BSS and stack.
Which brings me to the second method: Just use malloc() and free() to allocate
memory from the heap (which is the memory not used by the program). Calling
unsigned char* Buf = malloc (256);
will allocate a block of 256 bytes (if possible) and place it's address into
Buf.
A third, dirty and non-portable method that is similar to the first one would
be to access the variables used to implement the heap:
extern unsigned char* _horg;
extern unsigned char* _hend;
_horg contains the address of the first byte of free memory, _hend contains
the last byte, so
unsigned freespace = (_hend - _horg);
will calculate the amount of free space.
Note that this method may stop working with any future compiler release, since
in theory you're not allowed to access reserved identifiers used by the
implementation. So I would avoid using this method for any serious program.
And: This method will also collide with the heap routines.
Regards
Uz
--
Ullrich von Bassewitz uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
This archive was generated by hypermail 2.1.3 : 2001-12-14 22:05:39 CET