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 | #if defined(_LUCENE_PRAGMA_ONCE) |
11 | # pragma once |
12 | #endif |
13 | |
14 | #include "lucenebase.h" |
15 | |
16 | //Macro for creating new objects |
17 | #if defined(LUCENE_ENABLE_MEMLEAKTRACKING) |
18 | #define _CLNEW new(__FILE__, __LINE__) |
19 | #define LUCENE_BASE public virtual CL_NS(debug)::LuceneBase |
20 | #elif defined(LUCENE_ENABLE_REFCOUNT) |
21 | #define _CLNEW new |
22 | #define LUCENE_BASE public virtual CL_NS(debug)::LuceneBase |
23 | #else |
24 | #define _CLNEW new |
25 | #define LUCENE_BASE public CL_NS(debug)::LuceneVoidBase |
26 | #define LUCENE_BASE_CHECK(obj) (obj)->dummy__see_mem_h_for_details |
27 | #endif |
28 | #define _CL_POINTER(x) (x==NULL?NULL:(x->__cl_addref()>=0?x:x)) //return a add-ref'd object |
29 | #define LUCENE_REFBASE public CL_NS(debug)::LuceneBase //this is the base of classes who *always* need refcounting |
30 | |
31 | #if defined(_DEBUG) |
32 | #if !defined(LUCENE_BASE_CHECK) |
33 | #define LUCENE_BASE_CHECK(x) |
34 | #endif |
35 | #else |
36 | #undef LUCENE_BASE_CHECK |
37 | #define LUCENE_BASE_CHECK(x) |
38 | #endif |
39 | |
40 | //Macro for creating new arrays |
41 | #ifdef LUCENE_ENABLE_MEMLEAKTRACKING |
42 | #define _CL_NEWARRAY(type,size) (type*)CL_NS(debug)::LuceneBase::__cl_voidpadd(new type[(size_t)size],__FILE__,__LINE__,(size_t)size); |
43 | #define _CLDELETE_ARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__); delete [] x; x=NULL;} |
44 | #define _CLDELETE_LARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__);delete [] x;} |
45 | #ifndef _CLDELETE_CARRAY |
46 | #define _CLDELETE_CARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__);delete [] x; x=NULL;} |
47 | #define _CLDELETE_LCARRAY(x) if (x!=NULL){CL_NS(debug)::LuceneBase::__cl_voidpremove((const void*)x,__FILE__,__LINE__);delete [] x;} |
48 | #endif |
49 | #else |
50 | #define _CL_NEWARRAY(type,size) new type[size] |
51 | #define _CLDELETE_ARRAY(x) if (x!=NULL){delete [] x; x=NULL;} |
52 | #define _CLDELETE_LARRAY(x) if (x!=NULL){delete [] x;} |
53 | #ifndef _CLDELETE_CARRAY |
54 | #define _CLDELETE_CARRAY(x) if (x!=NULL){delete [] x; x=NULL;} |
55 | #define _CLDELETE_LCARRAY(x) if (x!=NULL){delete [] x;} |
56 | #endif |
57 | #endif |
58 | //a shortcut for deleting a carray and all its contents |
59 | #define _CLDELETE_CARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE_CARRAY(x[xcda]);}_CLDELETE_ARRAY(x)}; |
60 | #define _CLDELETE_ARRAY_ALL(x) {if ( x!=NULL ){ for(int xcda=0;x[xcda]!=NULL;xcda++)_CLDELETE(x[xcda]);}_CLDELETE_ARRAY(x)}; |
61 | #ifndef _CLDELETE_CaARRAY |
62 | #define _CLDELETE_CaARRAY _CLDELETE_CARRAY |
63 | #define _CLDELETE_LCaARRAY _CLDELETE_LCARRAY |
64 | #endif |
65 | |
66 | //Macro for deleting |
67 | #ifdef LUCENE_ENABLE_REFCOUNT |
68 | #define _CLDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; x=NULL; } |
69 | #define _CLLDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; } |
70 | #else |
71 | #define _CLDELETE(x) if (x!=NULL){ LUCENE_BASE_CHECK(x); delete x; x=NULL; } |
72 | #define _CLLDELETE(x) if (x!=NULL){ LUCENE_BASE_CHECK(x); delete x; } |
73 | #endif |
74 | |
75 | //_CLDECDELETE deletes objects which are *always* refcounted |
76 | #define _CLDECDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; x=NULL; } |
77 | #define _CLLDECDELETE(x) if (x!=NULL){ CND_PRECONDITION((x)->__cl_refcount>=0,"__cl_refcount was < 0"); if ((x)->__cl_decref() <= 0)delete x; } |
78 | |
79 | //_VDelete should be used for deleting non-clucene objects. |
80 | //when using reference counting, _CLDELETE casts the object |
81 | //into a LuceneBase*. |
82 | #define _CLVDELETE(x) if(x!=NULL){delete x; x=NULL;} |
83 | |
84 | template<typename T> |
85 | class Array: LUCENE_BASE{ |
86 | public: |
87 | T* values; |
88 | size_t length; |
89 | |
90 | void deleteAll(){ |
91 | for (size_t i=0;i<length;i++) |
92 | _CLDELETE(values[i]); |
93 | _CLDELETE_ARRAY(values); |
94 | } |
95 | void deleteArray(){ |
96 | _CLDELETE_ARRAY(values); |
97 | } |
98 | |
99 | Array(){ |
100 | values = NULL; |
101 | length = 0; |
102 | } |
103 | Array(T* values, size_t length){ |
104 | this->values = values; |
105 | this->length = length; |
106 | } |
107 | Array(size_t length){ |
108 | this->values = _CL_NEWARRAY(T,length); |
109 | this->length = length; |
110 | } |
111 | ~Array(){} |
112 | |
113 | const T operator[](size_t _Pos) const |
114 | { |
115 | if (length <= _Pos){ |
116 | _CLTHROWA(CL_ERR_IllegalArgument,"vector subscript out of range" ); |
117 | } |
118 | return (*(values + _Pos)); |
119 | } |
120 | T operator[](size_t _Pos) |
121 | { |
122 | if (length <= _Pos){ |
123 | _CLTHROWA(CL_ERR_IllegalArgument,"vector subscript out of range" ); |
124 | } |
125 | return (*(values + _Pos)); |
126 | } |
127 | |
128 | }; |
129 | |
130 | #endif //_lucene_debug_lucenebase_ |
131 | |