1 /*------------------------------------------------------------------------------
2 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
3 *
4 * Distributable under the terms of either the Apache License (Version 2.0) or
5 * the GNU Lesser General Public License, as specified in the COPYING file.
6 ------------------------------------------------------------------------------*/
7 #ifndef _lucene_debug_mem_h
8 #define _lucene_debug_mem_h
9 
10 //todo: this is a hack...
11 #ifndef CND_PRECONDITION
12   #include <assert.h>
13 	#define CND_PRECONDITION(x,y) assert(x)
14 #endif
15 
16 //Macro for creating new objects
17 #if defined(LUCENE_ENABLE_REFCOUNT)
18    #define _CLNEW new
19 #else
20    #define _CLNEW new
21 #endif
22 #define _CL_POINTER(x) (x==NULL?NULL:(x->__cl_addref()>=0?x:x)) //return a add-ref'd object
23 #define _CL_DECREF(x) ((x)==NULL?NULL:((x)->__cl_decref()>=0?(x):(x))) //return a add-ref'd object
24 #define _CL_LDECREF(x) if ((x)!=NULL) (x)->__cl_decref();
25 
26 //Macro for creating new arrays
27 #define _CL_NEWARRAY(type,size) (type*)calloc(size, sizeof(type))
28 #define _CLDELETE_ARRAY(x) {free(x); x=NULL;}
29 #define _CLDELETE_LARRAY(x) {free(x);}
30 #ifndef _CLDELETE_CARRAY
31 	#define _CLDELETE_CARRAY(x) {free(x); x=NULL;}
32 	#define _CLDELETE_LCARRAY(x) {free(x);}
33 #endif
34 
35 //a shortcut for deleting a carray and all its contents
36 #define _CLDELETE_CARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE_CARRAY(x[xcda]);}_CLDELETE_ARRAY(x)};
37 #define _CLDELETE_LCARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE_LCARRAY(x[xcda]);}_CLDELETE_LARRAY(x)};
38 #define _CLDELETE_CaARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE_CaARRAY(x[xcda]);}_CLDELETE_ARRAY(x)};
39 #define _CLDELETE_ARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE(x[xcda]);}_CLDELETE_ARRAY(x)};
40 #ifndef _CLDELETE_CaARRAY
41 		#define _CLDELETE_CaARRAY _CLDELETE_CARRAY
42 		#define _CLDELETE_LCaARRAY _CLDELETE_LCARRAY
43 #endif
44 
45 //Macro for deleting
46 #ifdef LUCENE_ENABLE_REFCOUNT
47 	#define _CLDELETE(x) if (x!=NULL){ CND_PRECONDITION(_LUCENE_ATOMIC_INT_GET((x)->__cl_refcount)>=0,"__cl_refcount was < 0"); if (_LUCENE_ATOMIC_INT_GET((x)->__cl_decref()) <= 0)delete x; x=NULL; }
48 	#define _CLLDELETE(x) if (x!=NULL){ CND_PRECONDITION(_LUCENE_ATOMIC_INT_GET((x)->__cl_refcount)>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; }
49 #else
50 	#define _CLDELETE(x) {delete x;x=NULL;}
51 	#define _CLLDELETE(x) {delete x;}
52 #endif
53 
54 //_CLDECDELETE deletes objects which are *always* refcounted
55 #define _CLDECDELETE(x) if (x!=NULL){ CND_PRECONDITION(_LUCENE_ATOMIC_INT_GET((x)->__cl_refcount)>=0,"__cl_refcount was < 0"); _LUCENE_ATOMIC_DECDELETE(&(x)->__cl_refcount, x); x=NULL; }
56 #define _CLLDECDELETE(x) if (x!=NULL){ CND_PRECONDITION(_LUCENE_ATOMIC_INT_GET((x)->__cl_refcount)>=0,"__cl_refcount was < 0"); _LUCENE_ATOMIC_DECDELETE(&(x)->__cl_refcount, x); }
57 #define _LUCENE_ATOMIC_DECDELETE(theInteger, theObject) { if ( _LUCENE_ATOMIC_DEC(theInteger) == 0) delete theObject;}
58 
59 //_VDelete should be used for deleting non-clucene objects.
60 //when using reference counting, _CLDELETE casts the object
61 //into a LuceneBase*.
62 #define _CLVDELETE(x) {delete x;x=NULL;}
63 
64 #endif //_lucene_debug_mem_h
65