Home Page Toolkit Overview Using GML User Input Services Finger Tracker Calibrator Frame Grabber Service protocol Obtaining GML Installing GML Licence Developer Documentation Tcl/Tk API The GML Canvas Image processing Tcl Scripts Library List of Classes List of Files C/C++ API List of Classes List of Files |
gml_Alloc.hGo to the documentation of this file.00001 /** @file gml_Alloc.h 00002 * 00003 * Memory allocation / deallocation for objects / structures. 00004 * 00005 * Copyright (c) 1995-2004 CLIPS-IMAG 00006 * 00007 * See the file "gml_LicenseTerms.txt" for information on usage and redistribution 00008 * of this file, and for a DISCLAIMER OF ALL WARRANTIES. 00009 * 00010 * Created on November, 1995 (FB). 00011 */ 00012 #ifndef __GML_ALLOC__ 00013 #define __GML_ALLOC__ 00014 00015 00016 #include "gml/base/gml_Types.h" 00017 #include "gml/base/gml_Errors.h" 00018 00019 #include <assert.h> 00020 #include <stdlib.h> 00021 #include <string.h> 00022 00023 #ifdef HAVE_DMALLOC 00024 #include "dmalloc.h" 00025 #endif 00026 00027 00028 /** 00029 * gml_Zero -- 00030 * 00031 * Set all bytes in a data structure to zero. 00032 * @param[in] ptr a pointer to the operand structure 00033 */ 00034 template <class T> 00035 inline 00036 void gml_Zero (T* ptr) 00037 { 00038 assert (ptr != (T*)NULL); 00039 bzero ((void*)ptr, sizeof (T)); 00040 } 00041 00042 00043 00044 /** 00045 * gml_ZeroArray -- 00046 * 00047 * Set all bytes of all structures in an array to zero. 00048 * @param[in] ptr a pointer to the operand array 00049 * @param[in] size the number of elements to zero 00050 */ 00051 template<class T> 00052 inline 00053 void gml_ZeroArray (T* ptr, gml_TBlockSize size) 00054 { 00055 assert (ptr != (T*)NULL); 00056 bzero ((void*)ptr, sizeof (T) * size); 00057 } 00058 00059 00060 /** 00061 * gml_Malloc -- 00062 * 00063 * Allocate an array of <size> structures. 00064 * 00065 * @param[out] ptr the pointer in which the newly allocated address will be stored 00066 * @param[in] size the number of structures to allocate 00067 */ 00068 template<class T> 00069 inline 00070 gml_TError gml_Malloc (T* &ptr, gml_TBlockSize size = 1) 00071 { 00072 ptr = (T*)malloc ((size_t)size * sizeof(T)); 00073 00074 if (ptr == (T*)NULL) 00075 return gml_gErrorMemFull; 00076 else 00077 return gml_cNoError; 00078 } 00079 00080 00081 /** 00082 * gml_MallocAndZero -- 00083 * 00084 * Allocate an array of <size> structures and zero it. 00085 * @param[out] ptr the pointer in which the newly allocated address will be stored 00086 * @param[in] size the number of structures to allocate and zero 00087 * @see gml_Malloc() and gml_ZeroArray(). 00088 */ 00089 template<class T> 00090 inline 00091 gml_TError gml_MallocAndZero (T* &ptr, gml_TBlockSize size = 1) 00092 { 00093 ptr = (T*)malloc ((size_t)size * sizeof(T)); 00094 00095 if (ptr == (T*)NULL) { 00096 return gml_gErrorMemFull; 00097 } else { 00098 gml_ZeroArray (ptr, size); 00099 return gml_cNoError; 00100 } 00101 } 00102 00103 00104 00105 /** 00106 * gml_Free -- 00107 * 00108 * Release resources claimed by gml_Malloc(). 00109 * The pointer <ptr> will be set to NULL. 00110 * @param[in,out] ptr the pointer to the data to free 00111 */ 00112 template<class T> 00113 inline 00114 void gml_Free (T* &ptr) 00115 { 00116 assert (ptr != (T*)NULL); 00117 00118 free ((void*)ptr); 00119 ptr = (T*)NULL; 00120 } 00121 00122 00123 00124 /** 00125 * gml_TestAndFree -- 00126 * 00127 * Release resources claimed by gml_Malloc(), iff <ptr> is not NULL. 00128 * @param[in,out] ptr the pointer to the data to free 00129 */ 00130 template<class T> 00131 inline 00132 void gml_TestAndFree (T* &ptr) 00133 { 00134 if (ptr != (T*)NULL) 00135 gml_Free (ptr); 00136 } 00137 00138 00139 /** 00140 * gml_New -- 00141 * 00142 * Create a new instance of class <T>. 00143 * @param[out] object will be set to the address of the new object. 00144 */ 00145 template<class T> 00146 inline 00147 gml_TError gml_New (T* &object) 00148 { 00149 gml_TError err = gml_cNoError; 00150 00151 object = new T; 00152 00153 if (object == (T*)NULL) 00154 err = gml_gErrorMemFull; 00155 00156 return err; 00157 } 00158 00159 00160 00161 /** 00162 * gml_NewArray -- 00163 * 00164 * Create a new array of instances of class <T>. 00165 * @param[out] object will be set to the address of the new array. 00166 * @param[in] size requested length of the array. 00167 */ 00168 template<class T> 00169 inline 00170 gml_TError gml_NewArray (T* &object, gml_TBlockSize size) 00171 { 00172 gml_TError err = gml_cNoError; 00173 00174 object = new T[size]; 00175 00176 if (object == (T*)NULL) 00177 err = gml_gErrorMemFull; 00178 00179 return err; 00180 } 00181 00182 00183 00184 /** 00185 * gml_NewAndInit -- 00186 * 00187 * Create a new instance of class <T> and call its <Init> method. 00188 * The <Init> method is called without arguments. 00189 * @param[out] object will be set to the address of the new object. 00190 */ 00191 template<class T> 00192 inline 00193 gml_TError gml_NewAndInit (T* &object) 00194 { 00195 gml_TError err = gml_cNoError; 00196 00197 err = gml_New (object); 00198 00199 if (err == gml_cNoError) { 00200 err = object->Init (); 00201 if (err != gml_cNoError) 00202 gml_DisposeAndDelete (object); 00203 } 00204 00205 return err; 00206 } 00207 00208 00209 /** 00210 * gml_NewAndInitArray -- 00211 * 00212 * Create a new array of instances of class <T>, and call their <Init> method. 00213 * @param[out] object will be set to the address of the new array. 00214 * @param[in] size requested length of the array. 00215 */ 00216 template<class T> 00217 inline 00218 gml_TError gml_NewAndInitArray (T* &object, gml_TBlockSize size) 00219 { 00220 gml_TError err = gml_cNoError; 00221 int i; 00222 00223 if ((err = gml_NewArray (object, size)) != gml_cNoError) 00224 return err; 00225 00226 for (i = 0; (err == gml_cNoError) && (i < size); i++) 00227 err = (object[i]).Init (); 00228 00229 if (err != gml_cNoError) { 00230 for (; i != 0; i--) 00231 (object[i-1]).Dispose (); 00232 gml_DeleteArray (object); 00233 } 00234 00235 return err; 00236 } 00237 00238 00239 00240 /** 00241 * gml_Delete -- 00242 * 00243 * Delete an instance of class <T>. 00244 * @param[in,out] object the address of the object; will be set to NULL. 00245 */ 00246 template<class T> 00247 inline 00248 void gml_Delete (T* &object) 00249 { 00250 assert (object != (T*)NULL); 00251 delete object; 00252 object = (T*)NULL; 00253 } 00254 00255 00256 /** 00257 * gml_DeleteArray -- 00258 * 00259 * Delete an array of instances of class <T>. 00260 * @param[in,out] object the address of the array; will be set to NULL. 00261 */ 00262 template<class T> 00263 inline 00264 void gml_DeleteArray (T* &object) 00265 { 00266 assert (object != (T*)NULL); 00267 delete[] object; 00268 object = (T*)NULL; 00269 } 00270 00271 00272 00273 /** 00274 * gml_DisposeAndDelete -- 00275 * 00276 * Same as gml_Delete(); 00277 * The object's <Dispose> method will be called first. 00278 */ 00279 template<class T> 00280 inline 00281 void gml_DisposeAndDelete (T* &object) 00282 { 00283 object->Dispose (); 00284 00285 gml_Delete (object); 00286 } 00287 00288 00289 /** 00290 * gml_DisposeAndDeleteArray -- 00291 * 00292 * Same as gml_DeleteArray(); 00293 * the objects' <Dispose> methods will be called first. 00294 */ 00295 template<class T> 00296 inline 00297 void gml_DisposeAndDeleteArray (T* &object, gml_TBlockSize size) 00298 { 00299 int i; 00300 00301 for (i = 0; i < size; i++) 00302 (object[i]).Dispose (); 00303 00304 gml_DeleteArray (object); 00305 } 00306 00307 00308 /** 00309 * gml_TestDisposeAndDelete -- 00310 * 00311 * Same as gml_DisposeAndDelete(); 00312 * nothing will be done if <object> is NULL. 00313 */ 00314 template<class T> 00315 inline 00316 void gml_TestDisposeAndDelete (T* &object) 00317 { 00318 if (object != (T*)NULL) 00319 gml_DisposeAndDelete (object); 00320 } 00321 00322 00323 /** 00324 * gml_TestDisposeAndDeleteArray -- 00325 * 00326 * Same as gml_DisposeAndDeleteArray() 00327 * nothing will be done if <object> is NULL. 00328 * 00329 */ 00330 template<class T> 00331 inline 00332 void gml_TestDisposeAndDeleteArray (T* &object, gml_TBlockSize size) 00333 { 00334 if (object != (T*)NULL) 00335 gml_DisposeAndDeleteArray (object, size); 00336 } 00337 00338 00339 /** 00340 * gml_TestAndDelete -- 00341 * 00342 * Same as gml_Delete(); 00343 * nothing will be done if <object> is NULL. 00344 * 00345 */ 00346 template<class T> 00347 inline 00348 void gml_TestAndDelete (T* &object) 00349 { 00350 if (object != (T*)NULL) 00351 gml_Delete (object); 00352 } 00353 00354 00355 /** 00356 * gml_TestAndDeleteArray -- 00357 * 00358 * Same as gml_DeleteArray(); 00359 * nothing will be done if <object> is NULL. 00360 * 00361 */ 00362 template<class T> 00363 inline 00364 void gml_TestAndDeleteArray (T* &object) 00365 { 00366 if (object != (T*)NULL) 00367 gml_DeleteArray (object); 00368 } 00369 00370 00371 00372 00373 /** 00374 * gml_GetGlobalObject -- 00375 * 00376 * Put in <ref> a reference to the global object 00377 * of the class that <ref> is a pointer to. If no global object of this 00378 * class was allocated yet, do it. 00379 * 00380 * @return gml_cNoError on success 00381 * @return gml_gErrorMemFull on failure, or an error code from the 00382 * <Init> initialisation method of the class. 00383 */ 00384 template<class T> 00385 inline 00386 gml_TError gml_GetGlobalObject (T* &ref) 00387 { 00388 gml_TError err = gml_cNoError; 00389 00390 // look if the global object have to be allocated 00391 if (T::fNumberOfRef == 0) 00392 // allocate a new object 00393 err = gml_NewAndInit (T::fGlobalRef); 00394 00395 if (err == gml_cNoError) { 00396 ref = T::fGlobalRef; 00397 T::fNumberOfRef++; 00398 } 00399 00400 return err; 00401 } 00402 00403 00404 00405 /** 00406 * gml_ReleaseGlobalObject -- 00407 * 00408 * If <ref> if not NULL, decrement the number 00409 * of references to the global object of the class that <ref> is a pointer to. 00410 * If this number becomes 0, dispose and delete the 00411 * global object. 00412 * @post <ref> is always NULL. 00413 */ 00414 template<class T> 00415 inline 00416 void gml_ReleaseGlobalObject (T* &ref) 00417 { 00418 if (ref != (T*)NULL) { 00419 00420 if (T::fNumberOfRef == 1) 00421 gml_DisposeAndDelete (T::fGlobalRef); 00422 00423 ref = (T*)NULL; 00424 00425 T::fNumberOfRef--; 00426 } 00427 } 00428 00429 00430 #endif 00431 00432
Generated on Tue Jun 12 14:03:27 2007 for gml by
Doxygen 1.5.2.
|
Contact: julien (dot) letessier (at) gmail (dot) com.
Copyright (c) 2000-2007 CLIPS-IMAG Laboratory, Grenoble, France. All rights reserved. W3CXHTML 1.0 W3CCSS 2.0 |