Back to index

lightning-sunbird  0.9+nobinonly
xpt_arena.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Simple arena allocator for xpt (which avoids using NSPR).
00040  */
00041 
00042 #ifndef __xpt_arena_h__
00043 #define __xpt_arena_h__
00044 
00045 #include "prtypes.h"
00046 #include <stdlib.h>
00047 
00048 
00049 /*
00050  * The linkage of XPT API functions differs depending on whether the file is
00051  * used within the XPT library or not.  Any source file within the XPT
00052  * library should define EXPORT_XPT_API whereas any client of the library
00053  * should not.
00054  */
00055 #ifdef EXPORT_XPT_API
00056 #define XPT_PUBLIC_API(t)    PR_IMPLEMENT(t)
00057 #define XPT_PUBLIC_DATA(t)   PR_IMPLEMENT_DATA(t)
00058 #else
00059 #ifdef _WIN32
00060 #    define XPT_PUBLIC_API(t)    __declspec(dllimport) t
00061 #    define XPT_PUBLIC_DATA(t)   __declspec(dllimport) t
00062 #else
00063 #    define XPT_PUBLIC_API(t)    PR_IMPLEMENT(t)
00064 #    define XPT_PUBLIC_DATA(t)   t
00065 #endif
00066 #endif
00067 #define XPT_FRIEND_API(t)    XPT_PUBLIC_API(t)
00068 #define XPT_FRIEND_DATA(t)   XPT_PUBLIC_DATA(t)
00069 
00070 PR_BEGIN_EXTERN_C
00071 
00072 /*
00073  * Simple Arena support. Use with caution!
00074  */ 
00075 
00076 typedef struct XPTArena XPTArena;
00077 
00078 XPT_PUBLIC_API(XPTArena *)
00079 XPT_NewArena(PRUint32 block_size, size_t alignment, const char* name);
00080 
00081 XPT_PUBLIC_API(void)
00082 XPT_DestroyArena(XPTArena *arena);
00083 
00084 XPT_PUBLIC_API(void)
00085 XPT_DumpStats(XPTArena *arena);
00086 
00087 XPT_PUBLIC_API(void *)
00088 XPT_ArenaMalloc(XPTArena *arena, size_t size);
00089 
00090 XPT_PUBLIC_API(char *)
00091 XPT_ArenaStrDup(XPTArena *arena, const char * s);
00092 
00093 XPT_PUBLIC_API(void)
00094 XPT_NotifyDoneLoading(XPTArena *arena);
00095 
00096 XPT_PUBLIC_API(void)
00097 XPT_ArenaFree(XPTArena *arena, void* block);
00098 
00099 /* --------------------------------------------------------- */
00100 
00101 #define XPT_MALLOC(_arena, _bytes) \
00102     XPT_ArenaMalloc((_arena), (_bytes))
00103 
00104 #ifdef DEBUG
00105 #define XPT_FREE(_arena, _ptr) \
00106     XPT_ArenaFree((_arena), (_ptr))
00107 #else
00108 #define XPT_FREE(_arena, _ptr) \
00109     ((void)0)
00110 #endif
00111 
00112 #define XPT_STRDUP(_arena, _s) \
00113     XPT_ArenaStrDup((_arena), (_s))
00114 
00115 #define XPT_CALLOC(_arena, _size) XPT_MALLOC((_arena), (_size))
00116 #define XPT_NEW(_arena, _struct) ((_struct *) XPT_MALLOC((_arena), sizeof(_struct)))
00117 #define XPT_NEWZAP(_arena, _struct) XPT_NEW((_arena), _struct)
00118 #define XPT_DELETE(_arena, _ptr) do{XPT_FREE((_arena), (_ptr)); ((_ptr)) = NULL;}while(0)
00119 #define XPT_FREEIF(_arena, _ptr) do{if ((_ptr)) XPT_FREE((_arena), (_ptr));}while(0)
00120 
00121 /* --------------------------------------------------------- */
00122 
00123 #ifdef DEBUG
00124 XPT_PUBLIC_API(void)
00125 XPT_AssertFailed(const char *s, const char *file, PRUint32 lineno);
00126 #define XPT_ASSERT(_expr) \
00127     ((_expr)?((void)0):XPT_AssertFailed(# _expr, __FILE__, __LINE__))
00128 #else
00129 #define XPT_ASSERT(_expr) ((void)0)
00130 #endif
00131 
00132 PR_END_EXTERN_C
00133 
00134 #endif /* __xpt_arena_h__ */