This chapter describes the Plug-in API functions that allocate and free memory as needed by the plug-in.
This chapter describes the Plug-in API functions that allocate and free memory as needed by the plug-in.Because plug-ins share memory space with Communicator, they can take advantage of any customized memory-allocation scheme the browser has. Browser memory schemes may be more efficient than standard OS memory functions, and can give the browser flexibility in the way it manages memory. In addition, the plug-in usually has the option of using its own memory functions. The methods that handle memory belong to the Netscape group of methods.
NPN_MemAllocfunction instead of the standard
mallocfunction to allocate dynamic memory. Using
NPN_MemAllocoffers several advantages to the plug-in.
NPN_MemAllocautomatically frees cached information if necessary to fulfill a request for memory, calls to
NPN_MemAllocmay succeed where direct calls to
NewPtrfail. § The
method has the following syntax:
void *NPN_MemAlloc (uint32 size);
size parameter is an unsigned long integer that represents the amount of memory, in bytes, to allocate in Communicator's memory space. This function returns a pointer to the allocated memory or
null if not enough memory is available.
NPN_MemFree method deallocates a block of memory that was allocated using
NPN_MemFree does not free memory allocated by other means.
void NPN_MemFree (void *ptr);
ptr parameter represents a block of memory previously allocated using
[Top] [Allocating and Freeing Memory]
NPN_MemFlushmethod frees a specified amount of memory. Normally, plug-ins should use
NPN_MemAlloc, which automatically frees nonessential memory if necessary to fulfill the request. For Communicator 4.0 and later versions, this function is not necessary for the Mac OS platform;
NPN_MemAllocnow performs memory flushing internally. You need to use
NPN_MemFlushonly when it is not possible to call
NPN_MemAlloc, for example, when calling system methods that allocate memory indirectly. If
NPN_MemAllocis called, calls to
NPN_MemFlushhave no effect. For example, suppose that the plug-in calls
NewGWorld, and that the call fails because of insufficient memory. The plug-in should try calling
NPN_MemFlushto free enough memory. If
NPN_MemFlushreturns a value indicating that enough memory was freed, the plug-in can call
NPN_MemFlushis particularly important to systems with small amounts of RAM and with virtual memory turned off. To request that Communicator free as much memory as possible, call
NPN_MemFlushrepeatedly until it returns 0.
uint32(uint32 size); The
sizeparameter is an unsigned long integer that represents the amount of memory, in bytes, to free in Communicator's memory space. This function returns the amount of freed memory, in bytes, or 0 if no memory could be freed. [Top] [Flushing Memory (Mac OS only)]
Last Updated: 01/15/97 16:36:57