Memory Management: 3.5 Using Global Memory: Accessing Data: An Example

Up: GEOS SDK TechDocs | Up | Prev: 3.4 Accessing Data in a Block | Next: 3.6 Data-Access Synchronization

Allocating and Using a Block shows how to allocate a block, lock it, access a word of data in the block, and unlock the block. This example shows the basic principles of using blocks.

Code Display 8-1 Allocating and Using a Block

/*
 * Variable Declarations
 */
MemHandle 	myBlockHandle;
char 	charArray[50], *blockBaseAddress;
/* First, we allocate a block of the desired size. Since we'll use the block right
 * away, we allocate the block already locked.
 */
myBlockhandle = MemAlloc(					/* MemAlloc returns the block handle */
			2048,		/* Allocate 2K of memory */
			HF_SWAPABLE,		/* HeapFlags: Make block swapable */
			HAF_ZERO_INIT|HAF_LOCK);			/* HeapAllocFlags: Initialize
						 * the memory to zero & lock it */
/* The block is already locked on the global heap. However, we do not have the
 * block's address; we just have its handle. Therefore, we need to call a routine
 * to dereference the handle. */
blockBaseAddress = (char *) MemDeref(myBlockHandle);						/* Returns a ptr to base of
						 * block */
/* Enter some data in the block */
strcpy(blockBaseAddress,
	"I can resist anything except temptation.\n   --Wilde"
/* We're done with the block for the moment, so we unlock it. */
MemUnlock(myBlockHandle); /* blockBaseAddress is now meaningless */
/* Here we do some other stuff . . . */
/* Now we want to use the block again. First we have to lock it. */
blockBaseAddress = (byte *) MemLock(myBlockHandle);						/* Returns a ptr to locked 
						 * block */
/* Read a string from the block: */
strcpy(charArray, blockBaseAddress);
/* We're done with the block, so we free it. Note that we can free the block
 * without unlocking it first.
 */
MemFree(myBlockhandle); /* myBlockHandle is now meaningless */

Up: GEOS SDK TechDocs | Up | Prev: 3.4 Accessing Data in a Block | Next: 3.6 Data-Access Synchronization