nsprpub/pr/include/prmem.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/include/prmem.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,126 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 +** File: prmem.h
    1.11 +** Description: API to NSPR memory management functions
    1.12 +**
    1.13 +*/
    1.14 +#ifndef prmem_h___
    1.15 +#define prmem_h___
    1.16 +
    1.17 +#include "prtypes.h"
    1.18 +#include <stdlib.h>
    1.19 +
    1.20 +PR_BEGIN_EXTERN_C
    1.21 +
    1.22 +/*
    1.23 +** Thread safe memory allocation.
    1.24 +**
    1.25 +** NOTE: pr wraps up malloc, free, calloc, realloc so they are already
    1.26 +** thread safe (and are not declared here - look in stdlib.h).
    1.27 +*/
    1.28 +
    1.29 +/*
    1.30 +** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free have the same signatures
    1.31 +** as their libc equivalent malloc, calloc, realloc, and free, and have
    1.32 +** the same semantics.  (Note that the argument type size_t is replaced
    1.33 +** by PRUint32.)  Memory allocated by PR_Malloc, PR_Calloc, or PR_Realloc
    1.34 +** must be freed by PR_Free.
    1.35 +*/
    1.36 +
    1.37 +NSPR_API(void *) PR_Malloc(PRUint32 size);
    1.38 +
    1.39 +NSPR_API(void *) PR_Calloc(PRUint32 nelem, PRUint32 elsize);
    1.40 +
    1.41 +NSPR_API(void *) PR_Realloc(void *ptr, PRUint32 size);
    1.42 +
    1.43 +NSPR_API(void) PR_Free(void *ptr);
    1.44 +
    1.45 +/*
    1.46 +** The following are some convenience macros defined in terms of
    1.47 +** PR_Malloc, PR_Calloc, PR_Realloc, and PR_Free.
    1.48 +*/
    1.49 +
    1.50 +/***********************************************************************
    1.51 +** FUNCTION:	PR_MALLOC()
    1.52 +** DESCRIPTION:
    1.53 +**   PR_NEW() allocates an untyped item of size _size from the heap.
    1.54 +** INPUTS:  _size: size in bytes of item to be allocated
    1.55 +** OUTPUTS:	untyped pointer to the node allocated
    1.56 +** RETURN:	pointer to node or error returned from malloc().
    1.57 +***********************************************************************/
    1.58 +#define PR_MALLOC(_bytes) (PR_Malloc((_bytes)))
    1.59 +
    1.60 +/***********************************************************************
    1.61 +** FUNCTION:	PR_NEW()
    1.62 +** DESCRIPTION:
    1.63 +**   PR_NEW() allocates an item of type _struct from the heap.
    1.64 +** INPUTS:  _struct: a data type
    1.65 +** OUTPUTS:	pointer to _struct
    1.66 +** RETURN:	pointer to _struct or error returns from malloc().
    1.67 +***********************************************************************/
    1.68 +#define PR_NEW(_struct) ((_struct *) PR_MALLOC(sizeof(_struct)))
    1.69 +
    1.70 +/***********************************************************************
    1.71 +** FUNCTION:	PR_REALLOC()
    1.72 +** DESCRIPTION:
    1.73 +**   PR_REALLOC() re-allocates _ptr bytes from the heap as a _size
    1.74 +**   untyped item.
    1.75 +** INPUTS:	_ptr: pointer to node to reallocate
    1.76 +**          _size: size of node to allocate
    1.77 +** OUTPUTS:	pointer to node allocated
    1.78 +** RETURN:	pointer to node allocated
    1.79 +***********************************************************************/
    1.80 +#define PR_REALLOC(_ptr, _size) (PR_Realloc((_ptr), (_size)))
    1.81 +
    1.82 +/***********************************************************************
    1.83 +** FUNCTION:	PR_CALLOC()
    1.84 +** DESCRIPTION:
    1.85 +**   PR_CALLOC() allocates a _size bytes untyped item from the heap
    1.86 +**   and sets the allocated memory to all 0x00.
    1.87 +** INPUTS:	_size: size of node to allocate
    1.88 +** OUTPUTS:	pointer to node allocated
    1.89 +** RETURN:	pointer to node allocated
    1.90 +***********************************************************************/
    1.91 +#define PR_CALLOC(_size) (PR_Calloc(1, (_size)))
    1.92 +
    1.93 +/***********************************************************************
    1.94 +** FUNCTION:	PR_NEWZAP()
    1.95 +** DESCRIPTION:
    1.96 +**   PR_NEWZAP() allocates an item of type _struct from the heap
    1.97 +**   and sets the allocated memory to all 0x00.
    1.98 +** INPUTS:	_struct: a data type
    1.99 +** OUTPUTS:	pointer to _struct
   1.100 +** RETURN:	pointer to _struct
   1.101 +***********************************************************************/
   1.102 +#define PR_NEWZAP(_struct) ((_struct*)PR_Calloc(1, sizeof(_struct)))
   1.103 +
   1.104 +/***********************************************************************
   1.105 +** FUNCTION:	PR_DELETE()
   1.106 +** DESCRIPTION:
   1.107 +**   PR_DELETE() unallocates an object previosly allocated via PR_NEW()
   1.108 +**   or PR_NEWZAP() to the heap.
   1.109 +** INPUTS:	pointer to previously allocated object
   1.110 +** OUTPUTS:	the referenced object is returned to the heap
   1.111 +** RETURN:	void
   1.112 +***********************************************************************/
   1.113 +#define PR_DELETE(_ptr) { PR_Free(_ptr); (_ptr) = NULL; }
   1.114 +
   1.115 +/***********************************************************************
   1.116 +** FUNCTION:	PR_FREEIF()
   1.117 +** DESCRIPTION:
   1.118 +**   PR_FREEIF() conditionally unallocates an object previously allocated
   1.119 +**   vial PR_NEW() or PR_NEWZAP(). If the pointer to the object is
   1.120 +**   equal to zero (0), the object is not released.
   1.121 +** INPUTS:	pointer to previously allocated object
   1.122 +** OUTPUTS:	the referenced object is conditionally returned to the heap
   1.123 +** RETURN:	void
   1.124 +***********************************************************************/
   1.125 +#define PR_FREEIF(_ptr)	if (_ptr) PR_DELETE(_ptr)
   1.126 +
   1.127 +PR_END_EXTERN_C
   1.128 +
   1.129 +#endif /* prmem_h___ */

mercurial