//Do not change SpanCount() without making sure it gives the correct result for use in GetSpanVector() int ON_CurveProxy::SpanCount() const { if (!m_real_curve) return 0; int rsc = m_real_curve->SpanCount(); ON_Interval domain = m_real_curve->Domain(); if (m_real_curve_domain == domain) return rsc; double* rsv = (double*)onmalloc((rsc+1)*sizeof(double)); if (!rsv) return 0; if (!m_real_curve->GetSpanVector(rsv)){ onfree((void*)rsv); return 0; } int i=0; int sc = 0; while (i <= rsc && rsv[i] <= m_real_curve_domain[0]) i++; while (i <= rsc && rsv[i] < m_real_curve_domain[1]){ sc++; i++; } sc++; onfree((void*)rsv); return sc; }
bool ON_CompressStream::Begin() { if ( 0 != m_implementation ) { onfree(m_implementation); m_implementation = 0; } // zero these because the same instance of an // ON_CompressStream class may be used multiple times. m_in_size = 0; m_out_size = 0; m_in_crc = 0; m_out_crc = 0; struct ON_ZlibImplementation* imp = (struct ON_ZlibImplementation*)onmalloc(sizeof(*imp)); memset(&imp->m_strm,0,sizeof(imp->m_strm)); if ( Z_OK != deflateInit( &imp->m_strm, Z_BEST_COMPRESSION ) ) { onfree(imp); return false; } m_implementation = imp; return true; }
////////// // If t is in the domain of the surface, GetSpanVectorIndex() returns the // span vector index "i" such that span_vector[i] <= t <= span_vector[i+1]. // The "side" parameter determines which span is selected when t is at the // end of a span. // //virtual ON_BOOL32 ON_Surface::GetSpanVectorIndex( int dir, // 0 gets first parameter's domain, 1 gets second parameter's domain double t, // [IN] t = evaluation parameter int side, // [IN] side 0 = default, -1 = from below, +1 = from above int* span_vector_i, // [OUT] span vector index ON_Interval* span_domain // [OUT] domain of the span containing "t" ) const { ON_BOOL32 rc = false; int i; int span_count = SpanCount(dir); if ( span_count > 0 ) { double* span_vector = (double*)onmalloc((span_count+1)*sizeof(span_vector[0])); rc = GetSpanVector( dir, span_vector ); if (rc) { i = ON_NurbsSpanIndex( 2, span_count, span_vector, t, side, 0 ); if ( i >= 0 && i <= span_count ) { if ( span_vector_i ) *span_vector_i = i; if ( span_domain ) span_domain->Set( span_vector[i], span_vector[i+1] ); } else rc = false; } onfree(span_vector); } return rc; }
ON_3dPoint ON_BezierCage::PointAt( ON_3dPoint rst ) const { ON_3dPoint pt; if ( m_dim <= 3 ) { pt.x = 0.0; pt.y = 0.0; pt.z = 0.0; Evaluate(rst.x,rst.y,rst.z,0,3,&pt.x); } else { double stack_buffer[16]; double* v; size_t sizeof_buffer = m_dim*sizeof(*v); v = (sizeof_buffer <= sizeof(stack_buffer)) ? stack_buffer : (double*)onmalloc(sizeof_buffer); v[0] = 0.0; v[1] = 0.0; v[2] = 0.0; Evaluate(rst.x,rst.y,rst.z,0,m_dim,v); pt.x = v[0]; pt.y = v[1]; pt.z = v[2]; if ( v != stack_buffer ) onfree(v); } return pt; }
int ON_Workspace::KeepMemory( void* p ) { int rc = false; if ( p ) { struct ON_Workspace_MBLK* pPrevBlk = NULL; struct ON_Workspace_MBLK* pBlk = m_pMemBlk; while ( pBlk ) { if ( pBlk->pMem == p ) { // Remove pBlk from list so ~ON_Workspace() won't onfree() its memory // and any future GrowMemory...() or KeepMemory() calls won't have // to search past it. pBlk->pMem = NULL; if ( pPrevBlk ) { pPrevBlk->pNext = pBlk->pNext; } else { m_pMemBlk = pBlk->pNext; } onfree( pBlk ); rc = true; break; } pPrevBlk = pBlk; pBlk = pBlk->pNext; } } return rc; }
CRhinoCommand::result CCommandSampleTriangulatePolygon::RunCommand( const CRhinoCommandContext& context ) { CRhinoGetObject go; go.SetCommandPrompt( L"Select closed planar polygon to triangulate" ); go.SetGeometryFilter( CRhinoGetObject::curve_object ); go.SetGeometryFilter( CRhinoGetObject::closed_curve ); go.EnableSubObjectSelect( FALSE ); go.GetObjects( 1, 1 ); if( go.CommandResult() != CRhinoCommand::success ) return go.CommandResult(); const CRhinoObjRef& ref = go.Object(0); ON_3dPointArray vertices; const ON_PolylineCurve* pc = ON_PolylineCurve::Cast( ref.Curve() ); if( pc ) { vertices = pc->m_pline; } else { const ON_NurbsCurve* nc = ON_NurbsCurve::Cast( ref.Curve() ); if( nc ) nc->IsPolyline( &vertices ); } if( vertices.Count() < 5 ) { RhinoApp().Print( L"Curve not polygon with at least four sides.\n" ); return CRhinoCommand::nothing; } int* triangles = (int*)onmalloc( (vertices.Count()-3) * sizeof(int) * 3 ); if( 0 == triangles ) return CRhinoCommand::failure; // out of memory memset( triangles, 0, (vertices.Count()-3) * sizeof(int) * 3 ); int rc = RhinoTriangulate3dPolygon( vertices.Count()-1, 3, (const double*)vertices.Array(), 3, triangles); if( 0 == rc ) { int i; for( i = 0; i < vertices.Count()-3; i++ ) { ON_Polyline pline; pline.Append( vertices[triangles[i * 3]] ); pline.Append( vertices[triangles[i * 3 + 1]] ); pline.Append( vertices[triangles[i * 3 + 2]] ); pline.Append( pline[0] ); context.m_doc.AddCurveObject( pline ); } context.m_doc.Redraw(); } onfree( triangles ); return CRhinoCommand::success; }
ON_Matrix::~ON_Matrix() { if ( 0 != m_Mmem ) { onfree(m_Mmem); m_Mmem = 0; } m_row_offset = 0; m_col_offset = 0; struct DBLBLK* p = (struct DBLBLK*)m_cmem; m_cmem = 0; while(0 != p) { struct DBLBLK* next = p->next; onfree(p); p = next; } }
void ON_MeshNgonList::Destroy() { m_ngons_count = 0; m_ngons_capacity = 0; if ( 0 != m_ngons ) { onfree(m_ngons); m_ngons = 0; } struct ON_NGON_MEMBLK* p = m_memblk_list; m_memblk_list = 0; while(p) { struct ON_NGON_MEMBLK* next = p->next; onfree(p); p = next; } }
ON_CompressStream::~ON_CompressStream() { if ( 0 != m_implementation ) { onfree(m_implementation); m_implementation = 0; } }
void ON_String::Destroy() { ON_aStringHeader* p = Header(); if ( p != pEmptyStringHeader && p->ref_count > 0 ) { p->ref_count--; if ( p->ref_count == 0 ) onfree(p); } Create(); }
void ON_String::CopyToArray( int w_count, const wchar_t* w ) { // convert UTF-16 string to UTF-8 string int c_count = w2c_size( w_count, w ); char* c = (char*)onmalloc(c_count+1); memset( c, 0, c_count+1 ); const int c_length = w2c( w_count, w, c_count, c ); c[c_length] = 0; CopyToArray( c_count, c ); onfree(c); }
ON_BOOL32 ON_Surface::Ev1Der( // returns false if unable to evaluate double s, double t, // evaluation parameters ON_3dPoint& point, ON_3dVector& ds, ON_3dVector& dt, int side, // optional - determines which side to evaluate from // 0 = default // 1 from NE quadrant // 2 from NW quadrant // 3 from SW quadrant // 4 from SE quadrant int* hint // optional - evaluation hint (int[2]) used to speed // repeated evaluations ) const { ON_BOOL32 rc = false; const int dim = Dimension(); double ws[3*32]; double* v; point.x = 0.0; point.y = 0.0; point.z = 0.0; ds.x = 0.0; ds.y = 0.0; ds.z = 0.0; dt.x = 0.0; dt.y = 0.0; dt.z = 0.0; if ( dim <= 32 ) { v = ws; } else { v = (double*)onmalloc(3*dim*sizeof(*v)); } rc = Evaluate( s, t, 1, dim, v, side, hint ); point.x = v[0]; ds.x = v[dim]; dt.x = v[2*dim]; if ( dim > 1 ) { point.y = v[1]; ds.y = v[dim+1]; dt.y = v[2*dim+1]; if ( dim > 2 ) { point.z = v[2]; ds.z = v[dim+2]; dt.z = v[2*dim+2]; if ( dim > 32 ) onfree(v); } } return rc; }
void ON_FixedSizePool::Destroy() { void* p; void* next; next = m_first_block; memset(this,0,sizeof(*this)); for ( p = next; 0 != p; p = next ) { next = *((void**)p); onfree(p); } }
void ON_Workspace::KeepAllMemory() { struct ON_Workspace_MBLK* p; struct ON_Workspace_MBLK* pNext = m_pMemBlk; m_pMemBlk = 0; while ( pNext ) { p = pNext; pNext = pNext->pNext; p->pMem = 0; // caller want to manage this heap onfree( p ); } }
ON_wString::ON_wString( unsigned char c, int repeat_count ) { Create(); if ( repeat_count > 0 ) { char* s = (char*)onmalloc((repeat_count+1)*sizeof(*s)); s[repeat_count] = 0; memset( s, c, repeat_count*sizeof(*s) ); CopyToArray( repeat_count, s ); onfree(s); m_s[repeat_count] = 0; Header()->string_length = repeat_count; } }
bool ON_BezierCage::Write(ON_BinaryArchive& archive) const { bool rc = archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,0); if (rc) { rc = archive.WriteInt(m_dim); if(rc) rc = archive.WriteInt(m_is_rat); if (rc) rc = archive.WriteInt(m_order[0]); if (rc) rc = archive.WriteInt(m_order[1]); if (rc) rc = archive.WriteInt(m_order[2]); int i,j,k; const int cv_dim = m_is_rat?(m_dim+1):m_dim; double* bogus_cv = 0; for(i = 0; i < m_order[0] && rc; i++) { for(j = 0; j < m_order[1] && rc; j++) { for ( k = 0; k < m_order[2] && rc; k++) { const double* cv = CV(i,j,k); if ( !cv ) { if ( 0 == bogus_cv ) { bogus_cv = (double*)onmalloc(cv_dim*sizeof(*bogus_cv)); for ( int n = 0; n < cv_dim; n++ ) bogus_cv[n] = ON_UNSET_VALUE; } cv = bogus_cv; } rc = archive.WriteDouble(cv_dim,cv); } } } if ( 0 != bogus_cv ) onfree(bogus_cv); if ( !archive.EndWrite3dmChunk() ) { rc = false; } } return rc; }
void ON_CompressedBuffer::Destroy() { if ( m_buffer_compressed ) onfree(m_buffer_compressed); m_sizeof_uncompressed = 0; m_sizeof_compressed = 0; m_crc_uncompressed = 0; m_crc_compressed = 0; m_method = 0; m_sizeof_element = 0; m_buffer_compressed = 0; m_buffer_compressed_capacity = 0; }
void ON_Workspace::Destroy() { struct ON_Workspace_FBLK* pFileBlk = m_pFileBlk; while ( pFileBlk ) { if ( pFileBlk->pFile ) fclose( pFileBlk->pFile ); pFileBlk = pFileBlk->pNext; } m_pFileBlk = 0; struct ON_Workspace_MBLK* pNext = m_pMemBlk; struct ON_Workspace_MBLK* p = NULL; while ( pNext ) { p = pNext; pNext = pNext->pNext; if ( p->pMem ) { onfree(p->pMem); p->pMem = NULL; } onfree( p ); } m_pMemBlk = 0; }
void ON_BezierCage::Destroy() { if ( m_cv && m_cv_capacity > 0 ) onfree(m_cv); m_cv_capacity = 0; m_cv_stride[0] = 0; m_cv_stride[1] = 0; m_cv_stride[2] = 0; m_cv = 0; m_dim = 0; m_is_rat = 0; m_order[0] = 0; m_order[1] = 0; m_order[2] = 0; }
void ON_Matrix::Destroy() { m = 0; m_row_count = 0; m_col_count = 0; m_rowmem.SetCount(0); if ( 0 != m_Mmem ) { // pointer passed to Create( row_count, col_count, M, bDestructorFreeM ) // when bDestructorFreeM = true. onfree(m_Mmem); m_Mmem = 0; } m_row_offset = 0; m_col_offset = 0; struct DBLBLK* cmem = (struct DBLBLK*)m_cmem; m_cmem = 0; while( 0 != cmem ) { struct DBLBLK* next_cmem = cmem->next; onfree(cmem); cmem = next_cmem; } }
void ON_hsort(void *base, size_t nel, size_t width, int (*compar)(const void*,const void*)) { size_t i_end,k; unsigned char work_memory[work_size], *e_tmp, *e_end; if (nel < 2) return; k = nel >> 1; i_end = nel-1; e_end = ((unsigned char*)base) + i_end*width; e_tmp = (width > work_size) ? (unsigned char*)onmalloc(width) : work_memory; for (;;) { if (k) { --k; memcpy(e_tmp,((unsigned char*)base)+k*width,width); /* e_tmp = e[k]; */ } else { memcpy(e_tmp,e_end,width); /* e_tmp = e[i_end]; */ memcpy(e_end,base,width); /* e[i_end] = e[0]; */ if (!(--i_end)) { memcpy(base,e_tmp,width); /* e[0] = e_tmp; */ break; } e_end -= width; } { size_t i, j; unsigned char *e_i, *e_j; i = k; j = (k<<1) + 1; e_i = ((unsigned char*)base) + i*width; while (j <= i_end) { e_j = ((unsigned char*)base) + j*width; if (j < i_end && compar(e_j,e_j+width)<0 /*e[j] < e[j + 1] */) {j++; e_j += width;} if (compar(e_tmp,e_j)<0 /* e_tmp < e[j] */) { memcpy(e_i,e_j,width); /* e[i] = e[j]; */ i = j; e_i = e_j; j = (j<<1) + 1; } else j = i_end + 1; } memcpy(e_i,e_tmp,width); /* e[i] = e_tmp; */ } } if (width > work_size) onfree(e_tmp); }
void* onrealloc( void* memblock, size_t sz ) { if ( 0 == memblock ) { return onmalloc(sz); } if ( 0 == sz ) { onfree(memblock); return 0; } #if defined(ON_REALLOC_BROKEN) /* use malloc() and memcpy() instead of buggy realloc() */ void* p; const size_t memblocksz = _msize(memblock); if ( sz <= memblocksz ) { /* shrink */ if ( memblocksz <= 28 || 8*sz >= 7*memblocksz ) { /* don't bother reallocating */ p = memblock; } else { /* allocate smaller block */ p = malloc(sz); if ( p ) { memcpy( p, memblock, sz ); free(memblock); } } } else if ( sz > memblocksz ) { /* grow */ p = malloc(sz); if ( p ) { memcpy( p, memblock, memblocksz ); free(memblock); } } return p; #else return realloc( memblock, sz ); #endif }
bool ON_SolveSVD( int row_count, int col_count, double const * const * U, const double* invW, double const * const * V, const double* B, double*& X ) { int i, j; double *Y; const double* p0; double workY[128], x; if ( row_count < 1 || col_count < 1 || 0 == U || 0 == invW || 0 == V || 0 == B) return false; if ( 0 == X ) X = (double*)onmalloc(col_count*sizeof(X[0])); Y = (col_count > 128) ? ( (double*)onmalloc(col_count*sizeof(*Y)) ) : workY; for (i = 0; i < col_count; i++) { double y = 0.0; for (j = 0; j < row_count; j++) y += U[j][i] * *B++; B -= row_count; Y[i] = invW[i] * y; } for (i = 0; i < col_count; i++) { p0 = V[i]; j = col_count; x = 0.0; while (j--) x += *p0++ * *Y++; Y -= col_count; X[i] = x; } if (Y != workY) onfree(Y); return true; }
int ON_wString::Find( const char* s ) const { int rc = -1; if ( s && s[0] && !IsEmpty() ) { const int s_count = (int)strlen(s); // the (int) is for 64 bit size_t conversion wchar_t* w = (wchar_t*)onmalloc( (s_count+2)*sizeof(w[0]) ); c2w( s_count, s, s_count+1, w ); const wchar_t* p; p = wcsstr( m_s, w ); if ( p ) { rc = ((int)(p-m_s)); // the (int) cast is for 64 bit compilers } onfree( w ); } return rc; }
int ON_GetBase32Digits( const unsigned char* x, int x_count, unsigned char* base32_digits ) { int x_bit_count = 8*x_count; unsigned char mask, c; unsigned char bits[5] = {0,0,0,0,0}; unsigned int bits_count = 0; unsigned int base32_digit_count = 0; int i; if ( 0 == base32_digits || 0 == x || x_count <= 0 ) return 0; if ( x == base32_digits ) { unsigned char* tmp = (unsigned char*)onmalloc(x_count*sizeof(x[0])); if ( 0 == tmp ) return 0; memcpy(tmp,x,x_count*sizeof(x[0])); i = ON_GetBase32Digits(tmp,x_count,base32_digits); onfree(tmp); return i; } i = x_bit_count % 5; if ( i ) bits_count = 5-i; for ( i = 0; i < x_count; i++) { c = x[i]; for (mask = 128; 0 != mask; mask /= 2 ) { bits[bits_count++] = (0 != (c & mask)) ? 1 : 0; if ( 5 == bits_count ) { base32_digits[base32_digit_count++] = 16*bits[0] + 8*bits[1] + 4*bits[2] + 2*bits[3] + bits[4]; bits_count = 0; } } } return base32_digit_count; }
void* onrealloc_from_pool( ON_MEMORY_POOL* pool, void* memblock, size_t sz ) { void* p; if ( sz <= 0 ) { onfree(memblock); return 0; } if ( !memblock ) { return onmalloc_from_pool( pool, sz); } p = realloc(memblock,sz); return p; }
ON_UnknownUserData& ON_UnknownUserData::operator=(const ON_UnknownUserData& src) { if ( this != &src ) { m_sizeof_buffer = 0; if ( 0 != m_buffer ) { onfree(m_buffer); m_buffer = 0; } // ON_UserData::operator= handles setting m_userdata_copycount and // m_userdata_xform. ON_UserData::operator=(src); // For most kinds of user data except ON_UnknownUserData, // m_userdata_uuid is set by the constructor and should not // be altered by an operator=. However, for ON_UnknownUserData, // the value of m_userdata_uuid is varies because it is set // by the missing userdata class. So it has to be copied here. m_userdata_uuid = src.m_userdata_uuid; if ( m_userdata_copycount > 0 && src.m_sizeof_buffer > 0 && src.m_buffer ) { m_unknownclass_uuid = src.m_unknownclass_uuid; m_sizeof_buffer = src.m_sizeof_buffer; m_buffer = onmemdup( src.m_buffer, src.m_sizeof_buffer); m_3dm_version = src.m_3dm_version; } else { // The unknown user data is not supposed to copy m_userdata_uuid = ON_nil_uuid; m_unknownclass_uuid = ON_nil_uuid; m_sizeof_buffer = 0; m_buffer = 0; m_3dm_version = 0; } } return *this; }
int ON_wString::CompareNoCase( const char* s) const { int rc = 0; if ( s && s[0] ) { if ( IsEmpty() ) { rc = -1; } else { int c_count = w2c_size( Length(m_s), m_s ); char* c = (char*)onmalloc((c_count+1)*sizeof(*c)); w2c( Length(m_s), m_s, c_count, c ); c[c_count] = 0; rc = on_stricmp( c, s ); onfree(c); } } else { rc = IsEmpty() ? 0 : 1; } return rc; }
ON_BOOL32 ON_Surface::EvPoint( // returns false if unable to evaluate double s, double t, // evaluation parameters ON_3dPoint& point, int side, // optional - determines which side to evaluate from // 0 = default // 1 from NE quadrant // 2 from NW quadrant // 3 from SW quadrant // 4 from SE quadrant int* hint // optional - evaluation hint (int[2]) used to speed // repeated evaluations ) const { ON_BOOL32 rc = false; double ws[128]; double* v; if ( Dimension() <= 3 ) { v = &point.x; point.x = 0.0; point.y = 0.0; point.z = 0.0; } else if ( Dimension() <= 128 ) { v = ws; } else { v = (double*)onmalloc(Dimension()*sizeof(*v)); } rc = Evaluate( s, t, 0, Dimension(), v, side, hint ); if ( Dimension() > 3 ) { point.x = v[0]; point.y = v[1]; point.z = v[2]; if ( Dimension() > 128 ) onfree(v); } return rc; }
bool ON_OffsetSurfaceFunction::EvaluateDistance( double s, double t, int num_der, double* value ) const { const int vcnt = ((num_der+1)*(num_der+2))/2; int vi; for ( vi = 0; vi < vcnt; vi++ ) { value[vi] = 0; } bool rc = const_cast<ON_OffsetSurfaceFunction*>(this)->Initialize(); if (rc) { double barray[21]; double* bump_value = (vcnt > 21) ? (double*)onmalloc(vcnt*sizeof(bump_value[0])) : barray; const int bump_count = m_bumps.Count(); int bump_index, vi; for ( bump_index = 0; bump_index < bump_count; bump_index++ ) { m_bumps[bump_index].Evaluate( s, t, num_der, bump_value ); for ( vi = 0; vi < vcnt; vi++ ) { value[vi] += bump_value[vi]; } } if ( bump_value != barray ) onfree(bump_value); } return rc; }