2013-06-18 14:20:29 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-07-22 08:41:12 +00:00
|
|
|
* @file cached_container.cpp
|
|
|
|
* @brief Class to store instances of VERTEX with caching. It allows storing VERTEX objects and
|
|
|
|
* associates them with VERTEX_ITEMs. This leads to a possibility of caching vertices data in the
|
|
|
|
* GPU memory and a fast reuse of that data.
|
2013-06-18 14:20:29 +00:00
|
|
|
*/
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#include <gal/opengl/cached_container.h>
|
|
|
|
#include <gal/opengl/vertex_manager.h>
|
|
|
|
#include <gal/opengl/vertex_item.h>
|
|
|
|
#include <gal/opengl/shader.h>
|
2013-11-11 09:48:49 +00:00
|
|
|
#include <confirm.h>
|
2013-06-18 14:20:29 +00:00
|
|
|
#include <wx/log.h>
|
2013-07-22 08:41:12 +00:00
|
|
|
#include <list>
|
2013-06-18 14:20:29 +00:00
|
|
|
#ifdef __WXDEBUG__
|
|
|
|
#include <profile.h>
|
|
|
|
#endif /* __WXDEBUG__ */
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
using namespace KIGFX;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
CACHED_CONTAINER::CACHED_CONTAINER( unsigned int aSize ) :
|
2013-09-12 07:44:57 +00:00
|
|
|
VERTEX_CONTAINER( aSize ), m_item( NULL )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
|
|
|
// In the beginning there is only free space
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( aSize, 0 ) );
|
2015-03-21 09:06:04 +00:00
|
|
|
|
|
|
|
// Do not have uninitialized members:
|
|
|
|
m_chunkSize = 0;
|
|
|
|
m_chunkOffset = 0;
|
|
|
|
m_itemSize = 0;
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
void CACHED_CONTAINER::SetItem( VERTEX_ITEM* aItem )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( aItem != NULL );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
m_item = aItem;
|
|
|
|
m_itemSize = m_item->GetSize();
|
|
|
|
m_chunkSize = m_itemSize;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
if( m_itemSize == 0 )
|
|
|
|
m_items.insert( m_item ); // The item was not stored before
|
2013-07-17 16:49:38 +00:00
|
|
|
else
|
2013-09-12 07:44:57 +00:00
|
|
|
m_chunkOffset = m_item->GetOffset();
|
|
|
|
|
|
|
|
#if CACHED_CONTAINER_TEST > 1
|
|
|
|
wxLogDebug( wxT( "Adding/editing item 0x%08lx (size %d)" ), (long) m_item, m_itemSize );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER::FinishItem()
|
|
|
|
{
|
|
|
|
wxASSERT( m_item != NULL );
|
|
|
|
wxASSERT( m_item->GetSize() == m_itemSize );
|
|
|
|
|
|
|
|
// Finishing the previously edited item
|
|
|
|
if( m_itemSize < m_chunkSize )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-09-12 07:44:57 +00:00
|
|
|
// There is some not used but reserved memory left, so we should return it to the pool
|
|
|
|
int itemOffset = m_item->GetOffset();
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
// Add the not used memory back to the pool
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( m_chunkSize - m_itemSize, itemOffset + m_itemSize ) );
|
2013-09-12 07:44:57 +00:00
|
|
|
m_freeSpace += ( m_chunkSize - m_itemSize );
|
|
|
|
// mergeFreeChunks(); // veery slow and buggy
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
2013-09-12 07:44:57 +00:00
|
|
|
|
|
|
|
#if CACHED_CONTAINER_TEST > 1
|
|
|
|
wxLogDebug( wxT( "Finishing item 0x%08lx (size %d)" ), (long) m_item, m_itemSize );
|
|
|
|
test();
|
2013-10-14 14:13:35 +00:00
|
|
|
m_item = NULL; // electric fence
|
2013-09-12 07:44:57 +00:00
|
|
|
#endif
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
VERTEX* CACHED_CONTAINER::Allocate( unsigned int aSize )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
wxASSERT( m_item != NULL );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
if( m_failed )
|
|
|
|
return NULL;
|
2013-07-01 11:20:48 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
if( m_itemSize + aSize > m_chunkSize )
|
2013-06-19 08:50:46 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
// There is not enough space in the currently reserved chunk, so we have to resize it
|
2013-06-19 08:50:46 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Reserve a bigger memory chunk for the current item and
|
|
|
|
// make it multiple of 3 to store triangles
|
|
|
|
m_chunkSize = ( 2 * m_itemSize ) + aSize + ( 3 - aSize % 3 );
|
|
|
|
// Save the current size before reallocating
|
|
|
|
m_chunkOffset = reallocate( m_chunkSize );
|
2013-06-19 08:50:46 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
if( m_chunkOffset > m_currentSize )
|
2013-06-19 08:50:46 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
m_failed = true;
|
|
|
|
return NULL;
|
2013-06-19 08:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
VERTEX* reserved = &m_vertices[m_chunkOffset + m_itemSize];
|
|
|
|
m_itemSize += aSize;
|
2013-09-12 07:44:57 +00:00
|
|
|
// Now the item officially possesses the memory chunk
|
2013-07-22 08:41:12 +00:00
|
|
|
m_item->setSize( m_itemSize );
|
2013-07-05 12:01:33 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// The content has to be updated
|
|
|
|
m_dirty = true;
|
2013-07-05 12:01:33 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 1
|
|
|
|
test();
|
|
|
|
#endif
|
2013-09-12 07:44:57 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 2
|
|
|
|
showFreeChunks();
|
|
|
|
showReservedChunks();
|
|
|
|
#endif
|
2013-07-05 12:01:33 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
return reserved;
|
2013-07-05 12:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
void CACHED_CONTAINER::Delete( VERTEX_ITEM* aItem )
|
2013-07-17 16:49:38 +00:00
|
|
|
{
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( aItem != NULL );
|
|
|
|
wxASSERT( m_items.find( aItem ) != m_items.end() );
|
|
|
|
|
|
|
|
int size = aItem->GetSize();
|
|
|
|
int offset = aItem->GetOffset();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 1
|
|
|
|
wxLogDebug( wxT( "Removing 0x%08lx (size %d offset %d)" ), (long) aItem, size, offset );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Insert a free memory chunk entry in the place where item was stored
|
|
|
|
if( size > 0 )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( size, offset ) );
|
2013-09-12 07:44:57 +00:00
|
|
|
m_freeSpace += size;
|
|
|
|
// Indicate that the item is not stored in the container anymore
|
|
|
|
aItem->setSize( 0 );
|
|
|
|
}
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
m_items.erase( aItem );
|
|
|
|
|
|
|
|
#if CACHED_CONTAINER_TEST > 1
|
|
|
|
test();
|
|
|
|
#endif
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
// Dynamic memory freeing, there is no point in holding
|
|
|
|
// a large amount of memory when there is no use for it
|
2015-08-03 19:12:00 +00:00
|
|
|
if( m_freeSpace > ( 0.75 * m_currentSize ) && m_currentSize > m_initialSize )
|
2013-07-17 16:49:38 +00:00
|
|
|
{
|
2015-08-03 19:12:00 +00:00
|
|
|
resizeContainer( 0.5 * m_currentSize );
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
void CACHED_CONTAINER::Clear()
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
// Change size to the default one
|
|
|
|
m_vertices = static_cast<VERTEX*>( realloc( m_vertices,
|
|
|
|
m_initialSize * sizeof( VERTEX ) ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Reset state variables
|
|
|
|
m_freeSpace = m_initialSize;
|
|
|
|
m_currentSize = m_initialSize;
|
|
|
|
m_failed = false;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Set the size of all the stored VERTEX_ITEMs to 0, so it is clear that they are not held
|
|
|
|
// in the container anymore
|
2013-10-14 18:40:36 +00:00
|
|
|
ITEMS::iterator it;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
for( it = m_items.begin(); it != m_items.end(); ++it )
|
|
|
|
{
|
|
|
|
( *it )->setSize( 0 );
|
|
|
|
}
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
m_items.clear();
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Now there is only free space left
|
|
|
|
m_freeChunks.clear();
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( m_freeSpace, 0 ) );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
unsigned int CACHED_CONTAINER::reallocate( unsigned int aSize )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( aSize > 0 );
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 2
|
2013-09-12 07:44:57 +00:00
|
|
|
wxLogDebug( wxT( "Resize 0x%08lx from %d to %d" ), (long) m_item, m_itemSize, aSize );
|
2013-07-22 08:41:12 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-18 14:20:29 +00:00
|
|
|
// Is there enough space to store vertices?
|
|
|
|
if( m_freeSpace < aSize )
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
// Would it be enough to double the current space?
|
|
|
|
if( aSize < m_freeSpace + m_currentSize )
|
|
|
|
{
|
|
|
|
// Yes: exponential growing
|
|
|
|
result = resizeContainer( m_currentSize * 2 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-03 19:12:00 +00:00
|
|
|
// No: grow to the nearest greater power of 2
|
|
|
|
result = resizeContainer( pow( 2, ceil( log2( m_currentSize * 2 + aSize ) ) ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !result )
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
// Look for the free space chunk of at least given size
|
2013-10-14 18:40:36 +00:00
|
|
|
FREE_CHUNK_MAP::iterator newChunk = m_freeChunks.lower_bound( aSize );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
if( newChunk == m_freeChunks.end() )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-07-17 16:49:38 +00:00
|
|
|
// In the case when there is enough space to store the vertices,
|
|
|
|
// but the free space is not continous we should defragment the container
|
2013-06-18 14:20:29 +00:00
|
|
|
if( !defragment() )
|
2013-06-20 11:16:12 +00:00
|
|
|
return UINT_MAX;
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
// Update the current offset
|
|
|
|
m_chunkOffset = m_item->GetOffset();
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
// We can take the first free chunk, as there is only one after defragmentation
|
|
|
|
// and we can be sure that it provides enough space to store the object
|
2013-07-17 16:49:38 +00:00
|
|
|
newChunk = m_freeChunks.begin();
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
// Parameters of the allocated cuhnk
|
2013-07-22 08:41:12 +00:00
|
|
|
unsigned int chunkSize = newChunk->first;
|
2013-07-17 16:49:38 +00:00
|
|
|
unsigned int chunkOffset = newChunk->second;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
wxASSERT( chunkSize >= aSize );
|
2013-07-17 16:49:38 +00:00
|
|
|
wxASSERT( chunkOffset < m_currentSize );
|
|
|
|
|
|
|
|
// Check if the item was previously stored in the container
|
|
|
|
if( m_itemSize > 0 )
|
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 3
|
|
|
|
wxLogDebug( wxT( "Moving 0x%08x from 0x%08x to 0x%08x" ),
|
|
|
|
(int) m_item, oldChunkOffset, chunkOffset );
|
|
|
|
#endif
|
2013-07-17 16:49:38 +00:00
|
|
|
// The item was reallocated, so we have to copy all the old data to the new place
|
2015-08-03 19:12:00 +00:00
|
|
|
memcpy( &m_vertices[chunkOffset], &m_vertices[m_chunkOffset], m_itemSize * VertexSize );
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
// Free the space previously used by the chunk
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( m_itemSize > 0 );
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( m_itemSize, m_chunkOffset ) );
|
2013-07-17 16:49:38 +00:00
|
|
|
m_freeSpace += m_itemSize;
|
|
|
|
}
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
// Remove the allocated chunk from the free space pool
|
|
|
|
m_freeChunks.erase( newChunk );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
// If there is some space left, return it to the pool - add an entry for it
|
|
|
|
if( chunkSize > aSize )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( chunkSize - aSize, chunkOffset + aSize ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
m_freeSpace -= aSize;
|
2013-09-12 07:44:57 +00:00
|
|
|
// mergeFreeChunks(); // veery slow and buggy
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
m_item->setOffset( chunkOffset );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
return chunkOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
bool CACHED_CONTAINER::defragment( VERTEX* aTarget )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
|
|
|
wxLogDebug( wxT( "Defragmenting" ) );
|
2013-09-12 07:44:57 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
prof_counter totalTime;
|
2015-08-03 19:12:00 +00:00
|
|
|
prof_start( &totalTime );
|
2013-09-12 07:44:57 +00:00
|
|
|
#endif
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-06-18 14:20:29 +00:00
|
|
|
if( aTarget == NULL )
|
|
|
|
{
|
2013-07-17 16:49:38 +00:00
|
|
|
// No target was specified, so we have to reallocate our own space
|
2013-07-22 08:41:12 +00:00
|
|
|
aTarget = static_cast<VERTEX*>( malloc( m_currentSize * sizeof( VERTEX ) ) );
|
|
|
|
|
2013-06-18 14:20:29 +00:00
|
|
|
if( aTarget == NULL )
|
|
|
|
{
|
2013-11-11 09:48:49 +00:00
|
|
|
DisplayError( NULL, wxT( "Run out of memory" ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int newOffset = 0;
|
2013-10-14 18:40:36 +00:00
|
|
|
ITEMS::iterator it, it_end;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
for( it = m_items.begin(), it_end = m_items.end(); it != it_end; ++it )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
VERTEX_ITEM* item = *it;
|
|
|
|
int itemOffset = item->GetOffset();
|
|
|
|
int itemSize = item->GetSize();
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
// Move an item to the new container
|
2013-07-22 08:41:12 +00:00
|
|
|
memcpy( &aTarget[newOffset], &m_vertices[itemOffset], itemSize * VertexSize );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
// Update new offset
|
2013-07-17 16:49:38 +00:00
|
|
|
item->setOffset( newOffset );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
// Move to the next free space
|
|
|
|
newOffset += itemSize;
|
|
|
|
}
|
|
|
|
|
2013-06-28 08:47:41 +00:00
|
|
|
free( m_vertices );
|
2013-06-18 14:20:29 +00:00
|
|
|
m_vertices = aTarget;
|
|
|
|
|
|
|
|
// Now there is only one big chunk of free memory
|
|
|
|
m_freeChunks.clear();
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( m_freeSpace > 0 );
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( m_freeSpace, m_currentSize - m_freeSpace ) );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
2013-07-22 08:41:12 +00:00
|
|
|
prof_end( &totalTime );
|
|
|
|
|
|
|
|
wxLogDebug( wxT( "Defragmented the container storing %d vertices / %.1f ms" ),
|
2015-08-03 19:12:00 +00:00
|
|
|
m_currentSize - m_freeSpace, totalTime.msecs() );
|
2013-09-12 07:44:57 +00:00
|
|
|
#endif
|
2013-06-18 14:20:29 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
void CACHED_CONTAINER::mergeFreeChunks()
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
if( m_freeChunks.size() <= 1 ) // There are no chunks that can be merged
|
2013-07-17 16:49:38 +00:00
|
|
|
return;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-09-27 08:00:26 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
2013-07-22 08:41:12 +00:00
|
|
|
prof_counter totalTime;
|
2015-08-03 19:12:00 +00:00
|
|
|
prof_start( &totalTime );
|
2013-09-12 07:44:57 +00:00
|
|
|
#endif
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
// Reversed free chunks map - this one stores chunk size with its offset as the key
|
2013-10-14 18:40:36 +00:00
|
|
|
std::list<CHUNK> freeChunks;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
FREE_CHUNK_MAP::const_iterator it, it_end;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
for( it = m_freeChunks.begin(), it_end = m_freeChunks.end(); it != it_end; ++it )
|
|
|
|
{
|
|
|
|
freeChunks.push_back( std::make_pair( it->second, it->first ) );
|
|
|
|
}
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
m_freeChunks.clear();
|
|
|
|
freeChunks.sort();
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
std::list<CHUNK>::const_iterator itf, itf_end;
|
2013-07-17 16:49:38 +00:00
|
|
|
unsigned int offset = freeChunks.front().first;
|
|
|
|
unsigned int size = freeChunks.front().second;
|
|
|
|
freeChunks.pop_front();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
for( itf = freeChunks.begin(), itf_end = freeChunks.end(); itf != itf_end; ++itf )
|
|
|
|
{
|
|
|
|
if( itf->first == offset + size )
|
|
|
|
{
|
|
|
|
// These chunks can be merged, so just increase the current chunk size and go on
|
|
|
|
size += itf->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// These chunks cannot be merged
|
|
|
|
// So store the previous one
|
|
|
|
m_freeChunks.insert( std::make_pair( size, offset ) );
|
|
|
|
// and let's check the next chunk
|
|
|
|
offset = itf->first;
|
|
|
|
size = itf->second;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the last one
|
|
|
|
m_freeChunks.insert( std::make_pair( size, offset ) );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-09-27 08:00:26 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
2013-07-22 08:41:12 +00:00
|
|
|
prof_end( &totalTime );
|
|
|
|
|
2015-08-03 19:12:00 +00:00
|
|
|
wxLogDebug( wxT( "Merged free chunks / %.1f ms" ), totalTime.msecs() );
|
2013-09-12 07:44:57 +00:00
|
|
|
#endif
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
test();
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
bool CACHED_CONTAINER::resizeContainer( unsigned int aNewSize )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( aNewSize != m_currentSize );
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
|
|
|
wxLogDebug( wxT( "Resizing container from %d to %d" ), m_currentSize, aNewSize );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VERTEX* newContainer;
|
2013-07-01 11:20:48 +00:00
|
|
|
|
2013-06-18 14:20:29 +00:00
|
|
|
if( aNewSize < m_currentSize )
|
|
|
|
{
|
2013-07-17 16:49:38 +00:00
|
|
|
// Shrinking container
|
2013-06-18 14:20:29 +00:00
|
|
|
// Sanity check, no shrinking if we cannot fit all the data
|
2013-07-17 16:49:38 +00:00
|
|
|
if( reservedSpace() > aNewSize )
|
2013-06-18 14:20:29 +00:00
|
|
|
return false;
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
newContainer = static_cast<VERTEX*>( malloc( aNewSize * sizeof( VERTEX ) ) );
|
|
|
|
|
2013-07-01 11:20:48 +00:00
|
|
|
if( newContainer == NULL )
|
|
|
|
{
|
2013-11-11 09:48:49 +00:00
|
|
|
DisplayError( NULL, wxT( "Run out of memory" ) );
|
2013-07-01 11:20:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defragment directly to the new, smaller container
|
|
|
|
defragment( newContainer );
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
// We have to correct freeChunks after defragmentation
|
|
|
|
m_freeChunks.clear();
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( aNewSize - reservedSpace() > 0 );
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( aNewSize - reservedSpace(), reservedSpace() ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-17 16:49:38 +00:00
|
|
|
// Enlarging container
|
2013-07-22 08:41:12 +00:00
|
|
|
newContainer = static_cast<VERTEX*>( realloc( m_vertices, aNewSize * sizeof( VERTEX ) ) );
|
|
|
|
|
2013-07-01 11:20:48 +00:00
|
|
|
if( newContainer == NULL )
|
|
|
|
{
|
2013-11-11 09:48:49 +00:00
|
|
|
DisplayError( NULL, wxT( "Run out of memory" ) );
|
2013-07-01 11:20:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
// Add an entry for the new memory chunk at the end of the container
|
2013-10-14 18:40:36 +00:00
|
|
|
m_freeChunks.insert( CHUNK( aNewSize - m_currentSize, m_currentSize ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 16:49:38 +00:00
|
|
|
m_vertices = newContainer;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
m_freeSpace += ( aNewSize - m_currentSize );
|
2013-06-18 14:20:29 +00:00
|
|
|
m_currentSize = aNewSize;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#ifdef CACHED_CONTAINER_TEST
|
|
|
|
void CACHED_CONTAINER::showFreeChunks()
|
|
|
|
{
|
2015-08-03 19:12:00 +00:00
|
|
|
FREE_CHUNK_MAP::iterator it;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
wxLogDebug( wxT( "Free chunks:" ) );
|
|
|
|
|
|
|
|
for( it = m_freeChunks.begin(); it != m_freeChunks.end(); ++it )
|
|
|
|
{
|
|
|
|
unsigned int offset = getChunkOffset( *it );
|
|
|
|
unsigned int size = getChunkSize( *it );
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( size > 0 );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
wxLogDebug( wxT( "[0x%08x-0x%08x] (size %d)" ),
|
|
|
|
offset, offset + size - 1, size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER::showReservedChunks()
|
|
|
|
{
|
2015-08-03 19:12:00 +00:00
|
|
|
ITEMS::iterator it;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
wxLogDebug( wxT( "Reserved chunks:" ) );
|
|
|
|
|
|
|
|
for( it = m_items.begin(); it != m_items.end(); ++it )
|
|
|
|
{
|
|
|
|
VERTEX_ITEM* item = *it;
|
|
|
|
unsigned int offset = item->GetOffset();
|
|
|
|
unsigned int size = item->GetSize();
|
2013-09-12 07:44:57 +00:00
|
|
|
wxASSERT( size > 0 );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
wxLogDebug( wxT( "[0x%08x-0x%08x] @ 0x%08lx (size %d)" ),
|
|
|
|
offset, offset + size - 1, (long) item, size );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER::test()
|
|
|
|
{
|
|
|
|
// Free space check
|
|
|
|
unsigned int freeSpace = 0;
|
2015-08-03 19:12:00 +00:00
|
|
|
FREE_CHUNK_MAP::iterator itf;
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
for( itf = m_freeChunks.begin(); itf != m_freeChunks.end(); ++itf )
|
|
|
|
freeSpace += getChunkSize( *itf );
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
wxASSERT( freeSpace == m_freeSpace );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
// Reserved space check
|
|
|
|
/*unsigned int reservedSpace = 0;
|
2015-08-03 19:12:00 +00:00
|
|
|
ITEMS::iterator itr;
|
2013-07-22 08:41:12 +00:00
|
|
|
for( itr = m_items.begin(); itr != m_items.end(); ++itr )
|
|
|
|
reservedSpace += ( *itr )->GetSize();
|
|
|
|
reservedSpace += m_itemSize; // Add the current chunk size
|
|
|
|
|
|
|
|
wxASSERT( ( freeSpace + reservedSpace ) == m_currentSize );*/
|
|
|
|
|
|
|
|
// Overlapping check TBD
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|
2013-10-14 14:13:35 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#endif /* CACHED_CONTAINER_TEST */
|