opennurbs_workspace.cpp
Go to the documentation of this file.
00001 /* $NoKeywords: $ */
00002 /*
00003 //
00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
00006 // McNeel & Associates.
00007 //
00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
00011 //                              
00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
00013 //
00015 */
00016 
00017 #include "pcl/surface/3rdparty/opennurbs/opennurbs.h"
00018 
00019 ON_Workspace::ON_Workspace() 
00020 : m_pFileBlk(0)
00021 , m_pMemBlk(0)
00022 {}
00023 
00024 ON_Workspace::~ON_Workspace()
00025 {
00026   Destroy();
00027 }
00028 
00029 struct ON_Workspace_FBLK 
00030 {
00031   struct ON_Workspace_FBLK* pNext;
00032   FILE* pFile;
00033 } * m_pFileBlk;
00034 
00035 struct ON_Workspace_MBLK 
00036 {
00037   struct ON_Workspace_MBLK* pNext;
00038   void* pMem;
00039 } * m_pMemBlk;
00040 
00041 void ON_Workspace::Destroy()
00042 {
00043   struct ON_Workspace_FBLK* pFileBlk = m_pFileBlk;
00044   while ( pFileBlk ) {
00045     if ( pFileBlk->pFile )
00046       fclose( pFileBlk->pFile );
00047     pFileBlk = pFileBlk->pNext;
00048   }
00049   m_pFileBlk = 0;
00050 
00051   struct ON_Workspace_MBLK* pNext = m_pMemBlk;
00052   struct ON_Workspace_MBLK* p = NULL;
00053   while ( pNext ) {
00054     p = pNext;
00055     pNext = pNext->pNext;
00056     if ( p->pMem ) {
00057       onfree(p->pMem);
00058       p->pMem = NULL;
00059     }
00060     onfree( p );
00061   }
00062   m_pMemBlk = 0;
00063 }
00064 
00065 void* ON_Workspace::GetMemory( size_t size )
00066 {
00067   void* p = NULL;
00068   if ( size > 0 ) 
00069   {
00070     struct ON_Workspace_MBLK* pBlk = (struct ON_Workspace_MBLK*)onmalloc(sizeof(*pBlk));
00071     if ( pBlk ) 
00072     {
00073       pBlk->pMem = p = onmalloc(size);
00074       pBlk->pNext = m_pMemBlk;
00075       m_pMemBlk = pBlk;
00076     }
00077   }
00078   return p;
00079 }
00080 
00081 void* ON_Workspace::GrowMemory( void* p, size_t size )
00082 {
00083   void* newp = NULL;
00084   if ( !p ) {
00085     newp = GetMemory(size);
00086   }
00087   else {
00088     struct ON_Workspace_MBLK* pBlk = m_pMemBlk;
00089     while ( pBlk ) {
00090       if ( pBlk->pMem == p ) {
00091         if ( size > 0 ) {
00092           newp = onrealloc(p,size);
00093         }
00094         else {
00095           newp = p;
00096         }
00097         pBlk->pMem = newp;
00098         break;
00099       }
00100       pBlk = pBlk->pNext;
00101     }
00102   }
00103   return newp;
00104 }
00105 
00106 void ON_Workspace::KeepAllMemory()
00107 {
00108   struct ON_Workspace_MBLK* p;
00109   struct ON_Workspace_MBLK* pNext = m_pMemBlk;
00110   m_pMemBlk = 0;
00111   while ( pNext )
00112   {
00113     p = pNext;
00114     pNext = pNext->pNext;
00115     p->pMem = 0; // caller want to manage this heap
00116     onfree( p );
00117   }
00118 }
00119 
00120 int ON_Workspace::KeepMemory( void* p )
00121 {
00122   int rc = false;
00123   if ( p ) {
00124     struct ON_Workspace_MBLK* pPrevBlk = NULL;
00125     struct ON_Workspace_MBLK* pBlk = m_pMemBlk;
00126     while ( pBlk ) {
00127       if ( pBlk->pMem == p ) {
00128         // Remove pBlk from list so ~ON_Workspace() won't onfree() its memory
00129         // and any future GrowMemory...() or KeepMemory() calls won't have
00130         // to search past it.
00131         pBlk->pMem = NULL;
00132         if ( pPrevBlk ) {
00133           pPrevBlk->pNext = pBlk->pNext;
00134         }
00135         else {
00136           m_pMemBlk = pBlk->pNext;
00137         }
00138         onfree( pBlk );
00139         rc = true;
00140         break;
00141       }
00142       pPrevBlk = pBlk;
00143       pBlk = pBlk->pNext;
00144     }
00145   }
00146   return rc;
00147 }
00148 
00149 int* ON_Workspace::GetIntMemory( size_t size )
00150 {
00151   int* pi = (int*)(GetMemory(size*sizeof(*pi)));
00152   return pi;
00153 }
00154 
00155 double* ON_Workspace::GetDoubleMemory( size_t size )
00156 {
00157   double* pd = (double*)(GetMemory(size*sizeof(*pd)));
00158   return pd;
00159 }
00160 
00161 ON_3dPoint* ON_Workspace::GetPointMemory( size_t size )
00162 {
00163   ON_3dPoint* p3d = (ON_3dPoint*)(GetMemory(size*sizeof(*p3d)));
00164   return p3d;
00165 }
00166 
00167 ON_3dVector* ON_Workspace::GetVectorMemory( size_t size )
00168 {
00169   ON_3dVector* v3d = (ON_3dVector*)(GetMemory(size*sizeof(*v3d)));
00170   return v3d;
00171 }
00172 
00173 int** ON_Workspace::GetIntMemory( size_t row_count, size_t col_count )
00174 {
00175   int** p = 0;
00176   size_t i;
00177   if ( row_count > 0 && col_count > 0 )
00178   {
00179     p = (int**)GetMemory(row_count*(sizeof(*p) + col_count*sizeof(**p)));
00180     if ( p )
00181     {
00182       p[0] = (int*)(p+row_count);
00183       for( i = 1; i < row_count; i++ )
00184       {
00185         p[i] = p[i-1] + col_count;
00186       }
00187     }
00188   }
00189   return p;
00190 }
00191 
00192 double** ON_Workspace::GetDoubleMemory( size_t row_count, size_t col_count )
00193 {
00194   double** p = 0;
00195   size_t i;
00196   if ( row_count > 0 && col_count > 0 )
00197   {
00198     // i keeps doubles aligned
00199     i = (sizeof(*p) < sizeof(**p))
00200       ? (row_count + (row_count%2))
00201       : row_count;
00202     p = (double**)GetMemory(i*sizeof(*p) + row_count*col_count*sizeof(**p));
00203     if ( p )
00204     {
00205       p[0] = (double*)(p+i);
00206       for( i = 1; i < row_count; i++ )
00207       {
00208         p[i] = p[i-1] + col_count;
00209       }
00210     }
00211   }
00212   return p;
00213 }
00214 
00215 
00216 int* ON_Workspace::GrowIntMemory( int* p, size_t size )
00217 {
00218   int* pi = (int*)(GrowMemory(p,size*sizeof(*pi)));
00219   return pi;
00220 }
00221 
00222 double* ON_Workspace::GrowDoubleMemory( double* p, size_t size )
00223 {
00224   double* pd = (double*)(GrowMemory(p,size*sizeof(*pd)));
00225   return pd;
00226 }
00227 
00228 ON_3dPoint* ON_Workspace::GrowPointMemory( ON_3dPoint* p, size_t size )
00229 {
00230   ON_3dPoint* p3d = (ON_3dPoint*)(GrowMemory(p,size*sizeof(*p3d)));
00231   return p3d;
00232 }
00233 
00234 ON_3dVector* ON_Workspace::GrowVectorMemory( ON_3dVector* p, size_t size )
00235 {
00236   ON_3dVector* v3d = (ON_3dVector*)(GrowMemory(p,size*sizeof(*v3d)));
00237   return v3d;
00238 }
00239 
00240 
00241 FILE* ON_Workspace::OpenFile( const char* sFileName, const char* sMode ) 
00242 {
00243   FILE* pFile = ON::OpenFile( sFileName, sMode );
00244   if ( pFile ) 
00245   {
00246     struct ON_Workspace_FBLK* pFileBlk = (struct ON_Workspace_FBLK*)GetMemory( sizeof(*pFileBlk) );
00247     pFileBlk->pNext = m_pFileBlk;
00248     pFileBlk->pFile = pFile;
00249     m_pFileBlk = pFileBlk;
00250   }
00251   return pFile;
00252 }
00253 
00254 FILE* ON_Workspace::OpenFile( const wchar_t* sFileName, const wchar_t* sMode ) 
00255 {
00256   FILE* pFile = ON::OpenFile( sFileName, sMode );
00257   if ( pFile ) 
00258   {
00259     struct ON_Workspace_FBLK* pFileBlk = (struct ON_Workspace_FBLK*)GetMemory( sizeof(*pFileBlk) );
00260     pFileBlk->pNext = m_pFileBlk;
00261     pFileBlk->pFile = pFile;
00262     m_pFileBlk = pFileBlk;
00263   }
00264   return pFile;
00265 }
00266 
00267 int ON_Workspace::KeepFile( FILE* pFile )
00268 {
00269   int rc = false;
00270   if ( pFile ) {
00271     struct ON_Workspace_FBLK* pFileBlk = m_pFileBlk;
00272     while ( pFileBlk ) {
00273       if ( pFileBlk->pFile == pFile ) {
00274         pFileBlk->pFile = NULL;
00275         rc = true;
00276         break;
00277       }
00278       pFileBlk = pFileBlk->pNext;
00279     }
00280   }
00281   return rc;
00282 }


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:27:03