00001 /* --------------------------------------------------------------------------- 00002 Phission : 00003 Realtime Vision Processing System 00004 00005 Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu) 00006 University of Massachusetts at Lowell, 00007 Laboratory for Artificial Intelligence and Robotics 00008 00009 This file is part of Phission. 00010 00011 Phission is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU Lesser General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 Phission is distributed in the hope that it will be useful, 00017 but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 GNU Lesser General Public License for more details. 00020 00021 You should have received a copy of the GNU Lesser General Public License 00022 along with Phission; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 ---------------------------------------------------------------------------*/ 00026 #ifndef _PHMEMORY_H 00027 #define _PHMEMORY_H 00028 00033 #if defined(HAVE_STRING_H) 00034 #include <string.h> 00035 #endif 00036 00037 /* ------------------------------------------------------------------------ */ 00055 #ifdef __cplusplus 00056 extern "C" 00057 { 00058 #endif 00059 00065 typedef void *(*ph_calloc_func) ( size_t nmemb,size_t size ); 00072 typedef void *(*ph_malloc_func) ( size_t size ); 00079 typedef void *(*ph_realloc_func)( void *ptr, size_t size ); 00080 00081 #ifdef bfin 00082 00086 void *bfin_realloc(void *ptr, size_t size); 00090 void *bfin_malloc(size_t size); 00091 #endif 00092 #ifdef __cplusplus 00093 } /* extern "C" { */ 00094 #endif 00095 00096 /* ------------------------------------------------------------------------ */ 00097 #define phMemset(dst,v,size) memset(dst,v,size) 00098 #define phMemcpy(dst,src,size) memcpy(dst,src,size) 00099 #define phMemmove(dst,src,size) memmove(dst,src,size) 00100 #define phCalloc(nmemb,size) calloc(nmemb, size) 00101 00102 #ifndef bfin 00103 #define phMalloc(size) malloc(size) 00104 #define phRealloc(ptr,size) realloc(ptr, size) 00105 #else 00106 #define phMalloc(size) bfin_malloc(size) 00107 #define phRealloc(ptr,size) bfin_realloc(ptr, size) 00108 #endif 00109 00110 /* ------------------------------------------------------------------------ */ 00111 #ifndef phFree 00112 00119 #define phFree(x) { if (x) { free(x); x = NULL; } } 00120 #endif 00121 00122 #ifndef phDelete 00123 00130 #define phDelete(x) { if (x) { delete x; x = NULL; } } 00131 #endif 00132 00133 #ifndef phDeletearr 00134 00142 #define phDeletearr(x) { if (x) { delete [] x; x = NULL; } } 00143 #endif 00144 00145 #ifndef phDeleteArray 00146 00147 #define phDeleteArray phDeletearr 00148 #endif 00149 00150 /* ------------------------------------------------------------------------ */ 00187 /* ------------------------------------------------------------------------ */ 00197 #define phDALLOC(ptr,cursize,nelems,cast) \ 00198 { \ 00199 uint32_t tempsize = nelems * sizeof(cast); \ 00200 if (ptr == NULL) \ 00201 { \ 00202 ptr = (cast *)phCalloc(nelems,sizeof(cast)); \ 00203 phCHECK_NULLPTR(ptr,"phCalloc", \ 00204 "phCalloc failed(nelems:%lu,size:%lu)",\ 00205 (unsigned long)nelems,\ 00206 (unsigned long)tempsize); \ 00207 cursize = tempsize; \ 00208 } \ 00209 else \ 00210 { \ 00211 if (cursize != tempsize)\ 00212 { \ 00213 ptr = (cast *)phRealloc(ptr,tempsize); \ 00214 phCHECK_NULLPTR(ptr,"phRealloc",\ 00215 "phRealloc failed(nelems:%lu,size:%lu)",\ 00216 (unsigned long)nelems,\ 00217 (unsigned long)tempsize); \ 00218 cursize = tempsize; \ 00219 } \ 00220 phMemset(ptr,0,cursize); \ 00221 } \ 00222 } 00223 #define phDALLOC_NELEMS phDALLOC 00224 00225 /* ------------------------------------------------------------------------ */ 00238 #define phDALLOC_GROW(ptr,cursize,nelems,cast) \ 00239 { \ 00240 uint32_t tempsize = nelems * sizeof(cast); \ 00241 if (ptr == NULL) \ 00242 { \ 00243 ptr = (cast *)phCalloc(nelems,sizeof(cast)); \ 00244 phCHECK_NULLPTR(ptr,"phMalloc", \ 00245 "phMalloc failed(nelems:%u,size:%u)",nelems,tempsize); \ 00246 cursize = tempsize; \ 00247 } \ 00248 else if (cursize < tempsize)\ 00249 { \ 00250 ptr = (cast *)phRealloc(ptr,tempsize); \ 00251 phCHECK_NULLPTR(ptr,"phRealloc", \ 00252 "phRealloc failed(nelems:%u,size:%u)",nelems,tempsize); \ 00253 cursize = tempsize; \ 00254 } \ 00255 } 00256 00257 #define phDALLOC_NELEMS_GROW phDALLOC_GROW 00258 00259 /* ------------------------------------------------------------------------ */ 00269 #define phDALLOC_RESIZE(ptr,cursize,nelems,cast) \ 00270 { \ 00271 uint32_t tempsize = nelems * sizeof(cast); \ 00272 if (ptr == NULL) \ 00273 { \ 00274 ptr = (cast *)phCalloc(nelems,sizeof(cast)); \ 00275 phCHECK_NULLPTR(ptr,"phMalloc", \ 00276 "phMalloc failed(nelems:%u,size:%u)",nelems,tempsize); \ 00277 cursize = tempsize; \ 00278 } \ 00279 else if (cursize != tempsize)\ 00280 { \ 00281 ptr = (cast *)phRealloc(ptr,tempsize); \ 00282 phCHECK_NULLPTR(ptr,"phRealloc", \ 00283 "phRealloc failed(nelems:%u,size:%u)",nelems,tempsize); \ 00284 cursize = tempsize; \ 00285 } \ 00286 } 00287 00288 #define phDALLOC_NELEMS_RESIZE phDALLOC_RESIZE 00289 00290 /* ------------------------------------------------------------------------ */ 00295 #define phDALLOC_SIZE(ptr,cursize,newsize,cast) \ 00296 { \ 00297 if (ptr == NULL) \ 00298 { \ 00299 ptr = (cast *)phMalloc(newsize); \ 00300 phCHECK_NULLPTR(ptr,"phMalloc","phMalloc failed(size:%u)",newsize); \ 00301 cursize = newsize; \ 00302 phMemset(ptr,0,cursize); \ 00303 } \ 00304 else if (cursize != newsize)\ 00305 { \ 00306 ptr = (cast *)phRealloc(ptr,newsize); \ 00307 phCHECK_NULLPTR(ptr,"phRealloc","phRealloc failed(size:%u)",newsize); \ 00308 cursize = newsize; \ 00309 phMemset(ptr,0,cursize); \ 00310 } \ 00311 } 00312 00313 /* ------------------------------------------------------------------------ */ 00323 #define phDALLOC_QUIET_VOIDRETURN(ptr,cursize,nelems,cast) \ 00324 { \ 00325 uint32_t tempsize = nelems * sizeof(cast); \ 00326 if (ptr == NULL) \ 00327 { \ 00328 ptr = (cast *)phCalloc(nelems,sizeof(cast)); \ 00329 if (ptr == NULL) \ 00330 { \ 00331 cursize = 0; \ 00332 return; \ 00333 } \ 00334 cursize = tempsize; \ 00335 } \ 00336 else if (cursize != tempsize) \ 00337 { \ 00338 ptr = (cast *)phRealloc(ptr,tempsize); \ 00339 if (ptr == NULL) \ 00340 { \ 00341 cursize = 0; \ 00342 return; \ 00343 } \ 00344 cursize = tempsize; \ 00345 } \ 00346 else \ 00347 { \ 00348 phMemset(ptr,0,cursize); \ 00349 } \ 00350 } 00351 00352 #define phDALLOC_QV phDALLOC_QUIET_VOIDRETURN 00353 /* End DynamicMemoryAllocationMacros */ /* End MemoryFunctionsandMacros */ 00356 00357 #endif /* _PHMEMORY_H */
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |