GEOS SDK TechDocs
|
|
3.3 Freeing Memory
|
3.5 Accessing Data: An Example
MemLock(), MemUnlock()
Because the memory manager is constantly reorganizing the global heap, applications must have a way of making sure a block stays put when they want to use it. Applications must also have a way of recalling swapped blocks when they are needed.
These requirements are met by the memory manager's locking scheme. Whenever you need to access data in a non-fixed block, you must first lock it. This will cause the memory manager to copy the block back into the global heap if it had been swapped; the memory manager will not move, swap, or discard a block while the block is locked.
Any block may be locked several times. Each lock increments the block's lock count (to a maximum of 255 locks per block), and each unlock decrements it. The memory manager can only move the block when the lock count is zero.
One warning about locking blocks: Do not try to lock a block which was allocated as fixed. Attempting to do so will result in a system error. If you need to translate a fixed-block handle to a pointer, call
MemDeref()
.
MemLock()
locks a block on the heap. It is passed the handle of the block; it returns a pointer to the start of the block on the heap. If the block has been discarded,
MemLock()
returns a null pointer.
Immediately after you are done using a block, you should unlock it by calling
MemUnlock()
. It is better to lock and unlock the same block several times than to retain control of it for an extended period, as locked blocks degrade the performance of the heap compaction mechanism. To unlock a block, call
MemUnlock()
, passing the handle of the block to be unlocked.
MemUnlock()
decrements the lock count.
A block may be locked by any of the threads run by its creator; if the block is sharable, it may be run by any other thread as well. There is nothing in the
MemLock()
routine to prevent different threads from locking a block at the same time, causing potential synchronization problems. For this reason, if threads will be sharing a block, they should use the synchronization routines (see Data-Access Synchronization
).
GEOS SDK TechDocs
|
|
3.3 Freeing Memory
|
3.5 Accessing Data: An Example