The Mozilla
Organization
Our Mission
Who We Are
Getting Involved
Community
Editorials
What's New
Development
Roadmap
Module Owners
Blue Sky
Projects
Status
Tools
Products
Source Code
Binaries
Documentation
License Terms
Bug Reports
Search
Feedback


[Contents][Previous] [Next][Last]


Chapter 23
Memory Management

This chapter describes the global functions you use to perform memory management. NSPR provides heap based memory management functions that map to familiar malloc(), calloc(), realloc(), and free().

Heap Functions and Macros

The functions PR_Malloc, PR_Calloc, PR_Realloc, PR_Free have the same function signatures as do the LIBC functions malloc, calloc, realloc, free respectively. They function similarly.

PR_Malloc

Syntax
#include <prmem.h>

PR_EXTERN(void *) PR_Malloc(PRUint32 size);
Parameter
size The size memory to be allocated.
Returns
A pointer to the allocated memory, or if the allocation attempt fails: NULL.
Description
PR_Malloc() allocates memory of the requested size from the heap.

PR_Calloc

Syntax
#include <prmem.h>

void *PR_Calloc(PRUint32 nelem, PRUint32 elsize);
Parameter
nelem The number of elements of size elsize to be allocated.
elsize The size of an individual element.
Returns
A pointer to the allocated memory, or if the allocation attempt fails: NULL.
Description
PR_Calloc() allocates nelem objects of size elsize. The all bytes in the allocated memory are cleared.

PR_Realloc

Syntax
#include <prmem.h>

void *PR_Realloc(void *ptr, PRUint32 size);
Parameter
ptr Pointer to the existing memory object being resized.
size The size of the new memory object.
Returns
A pointer to the allocated memory, or if the resize attempt fails: NULL.
Description
PR_Realloc() attempts to enlarge or shrink the memory element addressed by ptr to a new size. The contents of the memory addressed by ptr will be the same up to the lesser of its old size or new size unless the new memory object's address is different than the original address.

PR_Free

Syntax
#include <prmem.h>

void PR_Free(void *ptr);
Parameter
ptr The address of the memory to be freed.
Returns
void
Description
The memory addressed by ptr is returned to the heap.

PR_MALLOC

Syntax
#include <prmem.h>

void * PR_MALLOC(_bytes)
Parameter
_bytes Size of the requested memory object.
Returns
A pointer to an allocated memory object or NULL in case of error.
Description
Allocated memory from the heap.

PR_NEW

Syntax
#include <prmem.h>

(_struct *) PR_NEW(_struct)
Parameter
_struct The name of a structure type.
Returns
A pointer of type _struct *. or NULL on error.
Description
PR_NEW allocates memory whose size is SIZEOF(_struct) and returns a pointer to that memory.

PR_REALLOC

Syntax
#include <prmem.h>

void * PR_REALLOC(_ptr, _size)
Parameter
_ptr pointer of memory to be re-sized.
_size The requested new size.
Returns
A pointer to re-sized memory or NULL on error.
Description
PR_REALLOC() re-allocates memory: _ptr and sets its size to _size. See also PR_Realloc()

PR_CALLOC

Syntax
#include <prmem.h>

void * PR_CALLOC(_size)
Parameter
_size The size of memory to be allocated.
Returns
A pointer to the newly allocated memory or NULL on error.
Description
PR_CALLOC() allocates a single object of size _size from the heap and clears the memory.

PR_NEWZAP

Syntax
#include <prmem.h>

(_struct *) PR_NEWZAP(_struct)
Parameter
_struct The name of a structure.
Returns
A pointer to memory of size SIZEOF(_struct) whose contents are set to zero or NULL on error.
Description
PR_NEWZAP() allocates an element of memory from the heap and sets the content of that memory to zero.

PR_DELETE

Syntax
#include <prmem.h>

void PR_DELETE(_ptr)
Parameter
_ptr The address of memory to be returned to the heap.
Returns
void
Description
PR_DELETE() returns the memory pointed to by _ptr to the heap and then sets _ptr to NULL.

PR_FREEIF

Syntax
#include <prmem.h>

#define PR_FREEIF(_ptr)
Returns
void
Description
PR_FREEIF() conditionally returns memory to the heap. When _ptr is not NULL, memory is returned.



[Contents][Previous] [Next][Last]
Last Updated: Tue Jul 14 17:52:23 PDT 1998
Copyright © 1998 Netscape Communications Corporation



Copyright © 1998 The Mozilla Organization.