A89: Re: memory management routines.


[Prev][Next][Index][Thread]

A89: Re: memory management routines.




> I just finished writing a memory manager in C, providing support for the
> normal malloc(), calloc(), realloc(), and free(). Because it's entirely
> in C, it should compile for any of the ti calculators. This doesn't
> provide support for things like memory protection, however, because that
> gets into protected mode programming, which the z80 doesn't have, and i
> have no idea how to do on the m68k.

The TI-89/92+ have:
#define malloc HeapAllocPtr
#define free HeapFreePtr
#define HeapAvail  _ROM_CALL_8F
#define HeapAlloc  _ROM_CALL_90
#define HeapAllocESTACK  _ROM_CALL_91
#define HeapAllocHigh  _ROM_CALL_92
#define HeapAllocThrow  _ROM_CALL_93
#define HeapAllocHighThrow  _ROM_CALL_94
#define HeapCompress  _ROM_CALL_95
#define HeapDeref  _ROM_CALL_96
#define HeapFree  _ROM_CALL_97
#define HeapFreeIndir  _ROM_CALL_98
#define HLock  _ROM_CALL_99
#define HeapLock  _ROM_CALL_9A
#define HeapGetLock  _ROM_CALL_9B
#define HeapMax  _ROM_CALL_9C
#define HeapRealloc  _ROM_CALL_9D
#define HeapSize  _ROM_CALL_9E
#define HeapUnlock  _ROM_CALL_9F
#define HeapMoveHigh  _ROM_CALL_A0
#define HeapEnd  _ROM_CALL_A1
#define HeapAllocPtr  _ROM_CALL_A2
#define HeapFreePtr  _ROM_CALL_A3

This can, however, be very useful anyway, since with these routines we have full
control over what's happening... TI:s system happily moves stuff around.

> I'm sure something like this hasn't been written for the ti86, and I
> have no idea about the ti89.
>
> Anyways, i just thought these routines would be helpfull for all the
> programmers out there who use C compilers
> for their TI calculators.
> Documentation is in the file header.
>
> The thing is thouroghly commented, so it shouldn't be too hard to
> understand.
>
> --robin


--------------------------------------------------------------------------------


> /*
>
>   Machine Independant Memory Management
>   by Robin Kirkman
>   email: misty@drrobin.yi.org
>
>   These routines provide trust-based memory management for those of you
writing
>   code out there for bare systems. I wrote them for my ti89, which doesn't
have
>   these calls, to my knowledge. If it does, well, I'm sure these will be
usefull
>   for all those z80 programmers, cause the TI calcs w/ z80's i am -certain- do
>   not have this stuff. Happy Days!
>
>   This is just the standard malloc(), calloc(), realloc(), and free().
>
>   The one exception is _initialize_malloc(void *ram_start), which you need to
call.
>   ram_start is assumed to be the bottom of a memory block, which is assumed to
be
>   without an upper bound. If you don't call _initialize_malloc before you use
any
>   of these calls, then your system will crash very quickly.
>
>   Feel free to use these routines in anything you like. All I want in return
is a
>   copy of whatever you use them in.
>
>   This code is copyrighted Nov 21, 1999, by Robin Kirkman.
>   Modify it all you want, but you best be leaving my name in here ;)
>
>   Funtion Prototypes:
>
>   void _initlialize_malloc(void *ramstart);
>   void _set_start_ram(void *ramstart);
>   void _free_all();
>   void *malloc(size_t size);
>   void *calloc(num_t num, size_t size);
>   void *realloc(void *ptr, size_t size);
>   void *free(void *ptr);
>
>   _initialize_ram(void *ramstart);
>     Sets the starting address and clears any allocations.
>   _set_start_ram(void *ramstart);
>     Sets the starting ram. If you use this while your program is running, you
can
>     have different areas with different allocations.
>   _free_all();
>     Frees all allocated blocks.
>   malloc(size_t size);
>     Allocates size bytes, and returns a pointer to them. If size is zero, does
nothing.
>   calloc(num_t num, size_t size);
>     Allocates num*size bytes, and returns a pointer to them. If size or num is
zero, does nothing.
>   realloc(void *ptr, size_t size);
>     Reallocates the block at ptr to size bytes, and returns a new pointer. All
data is retained.
>     If ptr is null, then realloc acts like malloc.
>     If size is null, then realloc acts like free.
>   free(void *ptr);
>     Frees an allocated block. If the block is not found, free() returns a -1.
If found, returns null.
>     NOTE: This is different from ANSI! In ansi, free() is typed as void (not
void pointer), and it
>     has 'undefined behavior' (read: crashes) when ptr isn't an allocated
block.
>
>  */


 / Niklas Brunlid
Check out Prosit for the TI-89 / TI-92+ at http://prosit.ticalc.org
Random PQF Quote follows:

"This is Lord Mountjoy Quickfang Winterforth IV, the hottest dragon in the
city. It could burn your head clean off."
        -- Captain Vimes addresses a band of rioters
           (Terry Pratchett, Guards! Guards!)




Follow-Ups: References: