home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
dev
/
cmanual-3.0.lha
/
CManual
/
Intuition
/
Miscellaneous
/
Miscellaneous.doc
< prev
next >
Wrap
Text File
|
1993-10-12
|
34KB
|
1,007 lines
9 MISCELLANEOUS
9.1 INTRODUCTION
This is the last chapter about Intuition, and we will therefore
finish of by discussing some special features.
9.2 MEMORY
If you write C programs you will often need to allocate some
memory. This process is normally referred as "dynamic memory
allocation" and is used whenever your program needs some more
memory while it is running.
When you allocate memory you need to specify how much you want,
and what type of memory (Fast or Chip). "Exec" will then try to
allocated the memory, and will return a pointer to the memory,
or NULL if it could not find a block big enough.
Once you have allocated the memory, you can use it as much as
you want. You just have to remember that before the program
terminates it must have deallocated all allocated memory. If
you forget to deallocate the memory when you leave, it will
be lost, and the only way to get it back is to reboot.
9.2.1 ALLOCATE MEMORY
You usually use the function AllocMem() if you want to allocate
memory. You only need to specify how much memory you need, and
what type of memory (Chip, Fast etc), and AllocMem() will
return a pointer to the new memory, or NULL if no memory could
be allocated.
Synopsis: memory = AllocMem( size, type );
memory: (void *) Pointer to the new allocated memory, or NULL
if no memory could be allocated. Remember! Never use
memory which you have not successfully allocated.
size: (long) The size (in bytes) of the memory you want.
(AllocMem() always allocates memory in multiples of
eight bytes. So if you only ask for 9 bytes, Exec
would actually give you 16 Bytes (2*8).)
type: (long) You need to choose one of the three following
types of memory (see chapter 0 INTRODUCTION for more
information about Chip and Fast memory). (The flags
are declared in the header file "exec/memory.h".)
MEMF_CHIP Chip memory. This memory can be accessed
by both the main processor, as well as
the Chips. Graphics/Sound data MUST
therefore be placed in Chip memory. If it
does not matter what type of memory you
get (Fast or Chip), you should try to
allocate Fast memory before you allocate
Chip memory. (Chip memory is more valuable
than Fast memory.)
MEMF_FAST Fast memory. This memory can only be
accessed by the main processor. (Graphics
and Sound data can NOT be stored in Fast
memory, use Chip memory.) This memory is
normally a little bit faster than Chip
memory, since only the main processor is
working with it, and it is not disturbed
by the Chips.
MEMF_PUBLIC If it does not matter what type of memory
you get (you do not intend to use the
memory for Graphics/Sound data), you
should use Fast memory. However, all
Amigas do not have Fast memory, since
you need to by a memory expansion in
order to get it. If want to tell Exec
that you would like to use Fast memory if
there is any, else use Chip memory, you
should ask for MEMF_PUBLIC.
If you want the allocated memory to be cleared
(initialized to zeros), you should set the flag
MEMF_CLEAR.
If you need to allocate 150 bytes of Chip memory, you would
write:
/* Declare a pointer to the memory you are going to allocate: */
CPTR memory; /* CPTR means memory pointer, declared in the */
/* header file "exec/types.h". "exec/types.h" is */
/* automatically included when you include the */
/* header file "intuition/intuition.h". */
/* Allocate 150 bytes of Chip memory: */
memory = (CPTR) AllocMem( 150, MEMF_CHIP );
/* Have we allocated the memory successfully? */
if( memory == NULL )
/* ERROR! Have not allocated any memory! */
9.2.2 DEALLOCATE MEMORY
All memory that has been allocated MUST be deallocated before
your program terminates. If you forget to deallocate memory
which you have allocated, that memory would be forever lost.
The only way of freeing such memory is to reboot. (Not to be
recommended. We are not Demo-writers, are we?)
You free allocated memory by calling the function FreeMem():
Synopsis: FreeMem( memory, size );
memory (void *) Pointer to some memory which has previously
been allocated. Remember! Never deallocate memory,
which you have not allocated, and never use memory
which has been deallocated.
size (long) The size (in bytes) of the memory you want to
deallocate.
To deallocate the 150 bytes of Chip memory we have previously
allocated, we write:
/* Free 150 bytes of memory: */
FreeMem( memory, 150 );
9.2.3 REMEMBER MEMORY
If you allocate memory several times, and in different
quantities, it can be very hard to remember to deallocate all
memory correctly. However, do not despair. Intuition have a
solution for you. You can use the function AllocRemember()
which calls the function AllocMem(), but will also allocate
memory for a Remember structure which is automatically
initialized for you. Every time you allocate memory with the
AllocRemember() function, the Remember structures are linked
together, and the information about the memory (size and
position) is remembered. Once you want to deallocate the
memory, you only need to call the function FreeRemember(), and
all memory is deallocated for you automatically.
The Remember structure look like this:
struct Remember
{
struct Remember *NextRemember;
ULONG RememberSize;
UBYTE *Memory;
};
The AllocRemember() function is called like this: (Very similar
to the AllocMem() function.)
Synopsis: memory = AllocRemember( remember, size, type );
memory: (char *) Pointer to the new allocated memory, or NULL
if no memory could be allocated. Remember! Never use
memory which you have not successfully allocated.
remember: (struct Remember **) Address of a pointer to a
Remember structure. Before you call the AllocRemember()
function for the first time you should set this
pointer to NULL. (Note that it is a pointer to a
pointer!)
size: (long) The size (in bytes) of the memory you want.
(AllocMem() always allocates memory in multiples of
eight bytes. So if you only ask for 29 bytes, Exec
would actually give you 32 Bytes (4*8).)
type: (long) You need to choose one of the three following
types of memory (see chapter 0 INTRODUCTION for more
information about Chip and Fast memory):
MEMF_CHIP Chip memory. This memory can be accessed
by both the main processor, as well as
the Chips. Graphics/Sound data MUST
therefore be placed in Chip memory. If it
does not matter what type of memory you
get (Fast or Chip), you should try to
allocate Fast memory before you allocate
Chip memory. (Chip memory is more valuable
than Fast memory.)
MEMF_FAST Fast memory. This memory can only be
accessed by the main processor. (Graphics
and Sound data can NOT be stored in Fast
memory, use Chip memory.) This memory is
normally a little bit faster than Chip
memory, since only the main processor is
working with it, and it is not disturbed
by the Chips.
MEMF_PUBLIC If it does not matter what type of memory
you get (you do not intend to use the
memory for Graphic