bool ON_CompressedBuffer::WriteChar( size_t count, const void* buffer ) { bool rc = true; if ( count > 0 && buffer ) { if ( count + m_sizeof_compressed > m_buffer_compressed_capacity ) { size_t delta = count + m_sizeof_compressed - m_buffer_compressed_capacity; if ( delta < 2048 ) delta = 2048; if ( delta < m_buffer_compressed_capacity/4 ) delta = m_buffer_compressed_capacity/4; m_buffer_compressed_capacity += delta; m_buffer_compressed = onrealloc(m_buffer_compressed,m_buffer_compressed_capacity); if ( !m_buffer_compressed ) { m_buffer_compressed_capacity = 0; m_sizeof_compressed = 0; return false; } } memcpy(((char*)m_buffer_compressed)+m_sizeof_compressed,buffer,count); m_sizeof_compressed += count; } else { rc = (0 == count); } return rc; }
void ON_String::ReserveArray( size_t array_capacity ) { ON_aStringHeader* p = Header(); const int capacity = (int) array_capacity; if ( p == pEmptyStringHeader ) { CreateArray(capacity); } else if ( p->ref_count > 1 ) { CreateArray(capacity); ON_aStringHeader* p1 = Header(); const int size = (capacity < p->string_length) ? capacity : p->string_length; if ( size > 0 ) { memcpy( p1->string_array(), p->string_array(), size*sizeof(*m_s) ); p1->string_length = size; } } else if ( capacity > p->string_capacity ) { p = (ON_aStringHeader*)onrealloc( p, sizeof(ON_aStringHeader) + (capacity+1)*sizeof(*m_s) ); m_s = p->string_array(); memset( &m_s[p->string_capacity], 0, (1+capacity-p->string_capacity)*sizeof(*m_s) ); p->string_capacity = capacity; } }
bool ON_BezierCage::Create( int dim, bool is_rat, int order0, int order1, int order2 ) { if ( m_cv_capacity < 1 ) m_cv = 0; m_dim = (dim>0) ? dim : 0; m_is_rat = is_rat ? 1 : 0; m_order[0] = (order0 >= 2) ? order0 : 0; m_order[1] = (order1 >= 2) ? order1 : 0; m_order[2] = (order2 >= 2) ? order2 : 0; m_cv_stride[2] = (m_dim > 0) ? m_dim+m_is_rat : 0; m_cv_stride[1] = m_cv_stride[2]*m_order[2]; m_cv_stride[0] = m_cv_stride[1]*m_order[1]; m_cv_capacity = m_cv_stride[0]*m_order[0]; m_cv = (double*)onrealloc( m_cv, m_cv_capacity*sizeof(m_cv[0]) ); return IsValid() ? true : false; }
bool ON_MeshNgonList::ReserveNgonCapacity(int capacity) { bool rc = true; if ( capacity > m_ngons_capacity ) { m_ngons = (ON_MeshNgon*)onrealloc(m_ngons,capacity*sizeof(m_ngons[0])); if ( 0 == m_ngons ) { m_ngons_capacity = 0; m_ngons_count = 0; rc = false; } else { m_ngons_capacity = capacity; } } return rc; }
///////////////////////////////////////////////////////////////// // Tools for managing CV and knot memory bool ON_BezierCage::ReserveCVCapacity( int capacity// number of doubles to reserve ) { if ( m_cv_capacity < capacity ) { if ( m_cv ) { if ( m_cv_capacity ) { m_cv = (double*)onrealloc( m_cv, capacity*sizeof(*m_cv) ); m_cv_capacity = (m_cv) ? capacity : 0; } // else user supplied m_cv[] array } else { m_cv = (double*)onmalloc( capacity*sizeof(*m_cv) ); m_cv_capacity = (m_cv) ? capacity : 0; } } return ( m_cv ) ? true : false; }
void ON_String::ShrinkArray() { ON_aStringHeader* p = Header(); if ( p != pEmptyStringHeader ) { if ( p->string_length < 1 ) { Destroy(); } else if ( p->ref_count > 1 ) { // shared string CreateArray(p->string_length); ON_aStringHeader* p1 = Header(); memcpy( m_s, p->string_array(), p->string_length*sizeof(*m_s)); p1->string_length = p->string_length; m_s[p1->string_length] = 0; } else if ( p->string_length < p->string_capacity ) { // onrealloc string p = (ON_aStringHeader*)onrealloc( p, sizeof(ON_aStringHeader) + (p->string_length+1)*sizeof(*m_s) ); p->string_capacity = p->string_length; m_s = p->string_array(); m_s[p->string_length] = 0; } } }
void* ON_Workspace::GrowMemory( void* p, size_t size ) { void* newp = NULL; if ( !p ) { newp = GetMemory(size); } else { struct ON_Workspace_MBLK* pBlk = m_pMemBlk; while ( pBlk ) { if ( pBlk->pMem == p ) { if ( size > 0 ) { newp = onrealloc(p,size); } else { newp = p; } pBlk->pMem = newp; break; } pBlk = pBlk->pNext; } } return newp; }
bool ON_CompressedBuffer::Compress( size_t sizeof__inbuffer, // sizeof uncompressed input data const void* inbuffer, // uncompressed input data int sizeof_element ) { Destroy(); //size_t compressed_size = 0; bool rc = false; if ( sizeof__inbuffer > 0 && 0 == inbuffer ) return false; if ( 0 == sizeof__inbuffer ) return true; // number of bytes of uncompressed data m_sizeof_uncompressed = sizeof__inbuffer; ON_CompressedBufferHelper helper; memset(&helper,0,sizeof(helper)); helper.action = 1; bool bToggleByteOrder = false; switch(sizeof_element) { case 2: case 4: case 8: if ( 0 == (sizeof__inbuffer%sizeof_element) ) { m_sizeof_element = sizeof_element; bToggleByteOrder = (ON::big_endian == ON::Endian()); } break; }; if ( bToggleByteOrder ) { ON_BinaryFile::ToggleByteOrder( (int)(sizeof__inbuffer/m_sizeof_element), m_sizeof_element, inbuffer, (void*)inbuffer ); } m_method = (sizeof__inbuffer > 128) ? 1 : 0; if ( m_method ) { if ( !CompressionInit(&helper) ) { CompressionEnd(&helper); m_method = 0; } else { m_buffer_compressed = onmalloc(sizeof__inbuffer/4); size_t sizeof_compressed = DeflateHelper( &helper, sizeof__inbuffer, inbuffer ); CompressionEnd(&helper); if ( sizeof_compressed > 0 && sizeof_compressed == m_sizeof_compressed ) { rc = true; if ( 2*m_buffer_compressed_capacity > 3*m_sizeof_compressed ) { // release memory we don't need m_buffer_compressed_capacity = m_sizeof_compressed; m_buffer_compressed = onrealloc(m_buffer_compressed,m_buffer_compressed_capacity); } } else { Destroy(); m_method = 0; } } } if ( 0 == m_method ) { // uncompressed m_buffer_compressed = onmalloc(sizeof__inbuffer); if ( m_buffer_compressed ) { m_sizeof_compressed = sizeof__inbuffer; m_buffer_compressed_capacity = sizeof__inbuffer; memcpy(m_buffer_compressed,inbuffer,sizeof__inbuffer); rc = true; } } if ( bToggleByteOrder ) { ON_BinaryFile::ToggleByteOrder( (int)(sizeof__inbuffer/m_sizeof_element), m_sizeof_element, inbuffer, (void*)inbuffer ); } if (rc) { m_crc_uncompressed = ON_CRC32( 0, sizeof__inbuffer, inbuffer ); m_crc_compressed = ON_CRC32( 0, m_sizeof_compressed, m_buffer_compressed ); } return rc; }
BOOL ON_UnknownUserData::Read( ON_BinaryArchive& file ) { m_buffer = onrealloc( m_buffer, m_sizeof_buffer ); m_3dm_version = file.Archive3dmVersion(); return file.ReadByte(m_sizeof_buffer,m_buffer); }