2013-06-18 14:20:29 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-08-08 12:25:03 +00:00
|
|
|
* Copyright 2013-2017 CERN
|
2021-02-16 22:25:27 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-08-18 14:17:16 +00:00
|
|
|
*
|
2013-06-18 14:20:29 +00:00
|
|
|
* @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>
|
2016-05-02 14:12:16 +00:00
|
|
|
#include <gal/opengl/utils.h>
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
#include <list>
|
2020-05-02 07:43:06 +00:00
|
|
|
#include <algorithm>
|
2016-05-02 14:12:16 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2013-06-18 14:20:29 +00:00
|
|
|
#ifdef __WXDEBUG__
|
2016-05-02 14:12:16 +00:00
|
|
|
#include <wx/log.h>
|
2013-06-18 14:20:29 +00:00
|
|
|
#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 ) :
|
2021-02-16 22:25:27 +00:00
|
|
|
VERTEX_CONTAINER( aSize ),
|
2021-02-22 13:15:09 +00:00
|
|
|
m_item( nullptr ),
|
2021-02-16 22:25:27 +00:00
|
|
|
m_chunkSize( 0 ),
|
|
|
|
m_chunkOffset( 0 ),
|
|
|
|
m_maxIndex( 0 )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
|
|
|
// In the beginning there is only free space
|
2016-05-02 14:12:17 +00:00
|
|
|
m_freeChunks.insert( std::make_pair( aSize, 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
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
assert( aItem != nullptr );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
unsigned int itemSize = aItem->GetSize();
|
2021-02-16 22:25:27 +00:00
|
|
|
m_item = aItem;
|
2016-05-02 14:12:17 +00:00
|
|
|
m_chunkSize = itemSize;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
// Get the previously set offset if the item was stored previously
|
|
|
|
m_chunkOffset = itemSize > 0 ? aItem->GetOffset() : -1;
|
2013-09-12 07:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER::FinishItem()
|
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
assert( m_item != nullptr );
|
2016-05-02 14:12:17 +00:00
|
|
|
|
|
|
|
unsigned int itemSize = m_item->GetSize();
|
2013-09-12 07:44:57 +00:00
|
|
|
|
|
|
|
// Finishing the previously edited item
|
2016-05-02 14:12:17 +00:00
|
|
|
if( 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
|
2016-05-02 14:12:17 +00:00
|
|
|
addFreeChunk( itemOffset + itemSize, m_chunkSize - itemSize );
|
2013-09-12 07:44:57 +00:00
|
|
|
// mergeFreeChunks(); // veery slow and buggy
|
2017-09-09 16:00:58 +00:00
|
|
|
|
|
|
|
m_maxIndex = std::max( itemOffset + itemSize, m_maxIndex );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
2013-09-12 07:44:57 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
if( itemSize > 0 )
|
|
|
|
m_items.insert( m_item );
|
|
|
|
|
2021-02-22 13:15:09 +00:00
|
|
|
m_item = nullptr;
|
2016-05-02 14:12:17 +00:00
|
|
|
m_chunkSize = 0;
|
|
|
|
m_chunkOffset = 0;
|
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 1
|
|
|
|
test();
|
|
|
|
#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
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
assert( m_item != nullptr );
|
2017-08-08 12:25:03 +00:00
|
|
|
assert( IsMapped() );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
if( m_failed )
|
2021-02-22 13:15:09 +00:00
|
|
|
return nullptr;
|
2013-07-01 11:20:48 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
unsigned int itemSize = m_item->GetSize();
|
|
|
|
unsigned int newSize = itemSize + aSize;
|
|
|
|
|
|
|
|
if( newSize > 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
|
2016-05-02 14:12:17 +00:00
|
|
|
if( !reallocate( newSize ) )
|
2013-06-19 08:50:46 +00:00
|
|
|
{
|
2013-07-22 08:41:12 +00:00
|
|
|
m_failed = true;
|
2021-02-22 13:15:09 +00:00
|
|
|
return nullptr;
|
2013-06-19 08:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
VERTEX* reserved = &m_vertices[m_chunkOffset + itemSize];
|
2016-05-02 14:12:16 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
// Now the item officially possesses the memory chunk
|
2016-05-02 14:12:17 +00:00
|
|
|
m_item->setSize( newSize );
|
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
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
2013-07-22 08:41:12 +00:00
|
|
|
test();
|
|
|
|
#endif
|
2013-09-12 07:44:57 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 2
|
|
|
|
showFreeChunks();
|
2016-05-02 14:12:16 +00:00
|
|
|
showUsedChunks();
|
2013-09-12 07:44:57 +00:00
|
|
|
#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
|
|
|
{
|
2021-02-22 13:15:09 +00:00
|
|
|
assert( aItem != nullptr );
|
2016-05-02 14:12:17 +00:00
|
|
|
assert( m_items.find( aItem ) != m_items.end() || aItem->GetSize() == 0 );
|
|
|
|
|
|
|
|
int size = aItem->GetSize();
|
|
|
|
|
|
|
|
if( size == 0 )
|
2021-02-16 22:25:27 +00:00
|
|
|
return; // Item is not stored here
|
2013-09-12 07:44:57 +00:00
|
|
|
|
|
|
|
int offset = aItem->GetOffset();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-09-12 07:44:57 +00:00
|
|
|
// Insert a free memory chunk entry in the place where item was stored
|
2016-05-02 14:12:17 +00:00
|
|
|
addFreeChunk( offset, 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 );
|
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
2013-09-12 07:44:57 +00:00
|
|
|
test();
|
|
|
|
#endif
|
2013-07-17 16:49:38 +00:00
|
|
|
|
2016-03-25 08:33:45 +00:00
|
|
|
// This dynamic memory freeing optimize memory usage, but in fact can create
|
2019-08-20 17:22:30 +00:00
|
|
|
// out of memory issues because freeing and reallocation large chunks of memory
|
|
|
|
// can create memory fragmentation and no room to reallocate large chunks
|
2016-03-25 08:33:45 +00:00
|
|
|
// after many free/reallocate cycles during a session using the same complex board
|
|
|
|
// So it can be disable.
|
2016-04-04 10:41:48 +00:00
|
|
|
// Currently: it is disable to avoid "out of memory" issues
|
|
|
|
#if 0
|
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
|
|
|
{
|
2016-05-02 14:12:16 +00:00
|
|
|
defragmentResize( 0.5 * m_currentSize );
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|
2016-03-25 08:33:45 +00:00
|
|
|
#endif
|
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
|
|
|
{
|
2016-04-04 11:54:55 +00:00
|
|
|
m_freeSpace = m_currentSize;
|
2017-09-09 16:00:58 +00:00
|
|
|
m_maxIndex = 0;
|
2013-07-22 08:41:12 +00:00
|
|
|
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
|
2016-04-04 11:54:55 +00:00
|
|
|
for( ITEMS::iterator it = m_items.begin(); it != m_items.end(); ++it )
|
2013-07-22 08:41:12 +00:00
|
|
|
( *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();
|
2016-05-02 14:12:17 +00:00
|
|
|
m_freeChunks.insert( std::make_pair( m_freeSpace, 0 ) );
|
2013-07-22 08:41:12 +00:00
|
|
|
}
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
bool CACHED_CONTAINER::reallocate( unsigned int aSize )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
2016-05-02 14:12:16 +00:00
|
|
|
assert( aSize > 0 );
|
2017-08-08 12:25:03 +00:00
|
|
|
assert( IsMapped() );
|
2013-09-12 07:44:57 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
unsigned int itemSize = m_item->GetSize();
|
|
|
|
|
2016-08-18 15:18:13 +00:00
|
|
|
// Find a free space chunk >= aSize
|
|
|
|
FREE_CHUNK_MAP::iterator newChunk = m_freeChunks.lower_bound( aSize );
|
|
|
|
|
2013-06-18 14:20:29 +00:00
|
|
|
// Is there enough space to store vertices?
|
2016-08-18 15:18:13 +00:00
|
|
|
if( newChunk == m_freeChunks.end() )
|
2013-06-18 14:20:29 +00:00
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
|
|
|
|
// Would it be enough to double the current space?
|
|
|
|
if( aSize < m_freeSpace + m_currentSize )
|
|
|
|
{
|
|
|
|
// Yes: exponential growing
|
2016-05-02 14:12:16 +00:00
|
|
|
result = defragmentResize( m_currentSize * 2 );
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-03 19:12:00 +00:00
|
|
|
// No: grow to the nearest greater power of 2
|
2016-05-02 14:12:16 +00:00
|
|
|
result = defragmentResize( pow( 2, ceil( log2( m_currentSize * 2 + aSize ) ) ) );
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !result )
|
2016-05-02 14:12:17 +00:00
|
|
|
return false;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2016-08-18 15:18:13 +00:00
|
|
|
newChunk = m_freeChunks.lower_bound( aSize );
|
|
|
|
assert( newChunk != m_freeChunks.end() );
|
|
|
|
}
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
// Parameters of the allocated chunk
|
2021-02-16 22:25:27 +00:00
|
|
|
unsigned int newChunkSize = getChunkSize( *newChunk );
|
2016-05-02 14:12:17 +00:00
|
|
|
unsigned int newChunkOffset = getChunkOffset( *newChunk );
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
assert( newChunkSize >= aSize );
|
|
|
|
assert( newChunkOffset < m_currentSize );
|
2013-07-17 16:49:38 +00:00
|
|
|
|
|
|
|
// Check if the item was previously stored in the container
|
2016-05-02 14:12:17 +00:00
|
|
|
if( itemSize > 0 )
|
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
|
2017-09-09 18:40:00 +00:00
|
|
|
memcpy( &m_vertices[newChunkOffset], &m_vertices[m_chunkOffset], itemSize * VERTEX_SIZE );
|
2013-07-17 16:49:38 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
// Free the space used by the previous chunk
|
|
|
|
addFreeChunk( m_chunkOffset, m_chunkSize );
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
// Remove the new allocated chunk from the free space pool
|
2013-07-17 16:49:38 +00:00
|
|
|
m_freeChunks.erase( newChunk );
|
2016-05-02 14:12:16 +00:00
|
|
|
m_freeSpace -= newChunkSize;
|
2013-06-18 14:20:29 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
m_chunkSize = newChunkSize;
|
|
|
|
m_chunkOffset = newChunkOffset;
|
|
|
|
|
|
|
|
m_item->setOffset( m_chunkOffset );
|
|
|
|
|
|
|
|
return true;
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 12:25:03 +00:00
|
|
|
void CACHED_CONTAINER::defragment( VERTEX* aTarget )
|
|
|
|
{
|
|
|
|
// Defragmentation
|
|
|
|
ITEMS::iterator it, it_end;
|
2021-02-16 22:25:27 +00:00
|
|
|
int newOffset = 0;
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
for( VERTEX_ITEM* item : m_items )
|
|
|
|
{
|
2021-02-16 22:25:27 +00:00
|
|
|
int itemOffset = item->GetOffset();
|
|
|
|
int itemSize = item->GetSize();
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
// Move an item to the new container
|
2017-09-09 18:40:00 +00:00
|
|
|
memcpy( &aTarget[newOffset], &m_vertices[itemOffset], itemSize * VERTEX_SIZE );
|
2017-08-08 12:25:03 +00:00
|
|
|
|
|
|
|
// Update new offset
|
|
|
|
item->setOffset( newOffset );
|
|
|
|
|
|
|
|
// Move to the next free space
|
|
|
|
newOffset += itemSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move the current item and place it at the end
|
|
|
|
if( m_item->GetSize() > 0 )
|
|
|
|
{
|
|
|
|
memcpy( &aTarget[newOffset], &m_vertices[m_item->GetOffset()],
|
2017-09-09 18:40:00 +00:00
|
|
|
m_item->GetSize() * VERTEX_SIZE );
|
2017-08-08 12:25:03 +00:00
|
|
|
m_item->setOffset( newOffset );
|
|
|
|
m_chunkOffset = newOffset;
|
|
|
|
}
|
2017-09-09 16:00:58 +00:00
|
|
|
|
|
|
|
m_maxIndex = usedSpace();
|
2017-08-08 12:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
#ifdef __WXDEBUG__
|
2016-12-31 12:00:24 +00:00
|
|
|
PROF_COUNTER totalTime;
|
2016-05-02 14:12:17 +00:00
|
|
|
#endif /* __WXDEBUG__ */
|
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 )
|
|
|
|
{
|
2019-12-05 15:41:21 +00:00
|
|
|
freeChunks.emplace_back( it->second, it->first );
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|
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;
|
2021-02-16 22:25:27 +00:00
|
|
|
unsigned int offset = freeChunks.front().first;
|
|
|
|
unsigned int size = freeChunks.front().second;
|
2013-07-17 16:49:38 +00:00
|
|
|
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;
|
2021-02-16 22:25:27 +00:00
|
|
|
size = itf->second;
|
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
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
#if CACHED_CONTAINER_TEST > 0
|
2013-07-22 08:41:12 +00:00
|
|
|
test();
|
2016-05-02 14:12:16 +00:00
|
|
|
#endif
|
2013-06-18 14:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
void CACHED_CONTAINER::addFreeChunk( unsigned int aOffset, unsigned int aSize )
|
|
|
|
{
|
|
|
|
assert( aOffset + aSize <= m_currentSize );
|
|
|
|
assert( aSize > 0 );
|
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
m_freeChunks.insert( std::make_pair( aSize, aOffset ) );
|
2016-05-02 14:12:16 +00:00
|
|
|
m_freeSpace += aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
void CACHED_CONTAINER::showFreeChunks()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
void CACHED_CONTAINER::showUsedChunks()
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CACHED_CONTAINER::test()
|
|
|
|
{
|
2016-05-02 14:12:17 +00:00
|
|
|
#ifdef __WXDEBUG__
|
2013-07-22 08:41:12 +00:00
|
|
|
// Free space check
|
2021-02-16 22:25:27 +00:00
|
|
|
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
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
assert( freeSpace == m_freeSpace );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
// Used space check
|
2021-02-16 22:25:27 +00:00
|
|
|
unsigned int used_space = 0;
|
2015-08-03 19:12:00 +00:00
|
|
|
ITEMS::iterator itr;
|
2021-02-16 22:25:27 +00:00
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
for( itr = m_items.begin(); itr != m_items.end(); ++itr )
|
2017-08-08 13:30:23 +00:00
|
|
|
used_space += ( *itr )->GetSize();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2016-05-02 14:12:17 +00:00
|
|
|
// If we have a chunk assigned, then there must be an item edited
|
|
|
|
assert( m_chunkSize == 0 || m_item );
|
|
|
|
|
|
|
|
// Currently reserved chunk is also counted as used
|
2017-08-08 13:30:23 +00:00
|
|
|
used_space += m_chunkSize;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2017-08-08 13:30:23 +00:00
|
|
|
assert( ( m_freeSpace + used_space ) == m_currentSize );
|
2016-05-02 14:12:17 +00:00
|
|
|
|
|
|
|
// Overlapping check TODO
|
|
|
|
#endif /* __WXDEBUG__ */
|
2013-07-17 16:49:38 +00:00
|
|
|
}
|