KiCad PCB EDA Suite
noncached_container.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2013 CERN
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
34 #include <cstring>
35 #include <cstdlib>
36 
37 using namespace KIGFX;
38 
40  VERTEX_CONTAINER( aSize ),
41  m_freePtr( 0 )
42 {
43  m_vertices = static_cast<VERTEX*>( malloc( aSize * sizeof( VERTEX ) ) );
44 
45  // Unfortunately we cannot remove the use of malloc here because realloc is used in
46  // the Allocate method below. The new operator behavior is mimicked here so that a
47  // malloc failure can be caught in the OpenGL initialization code further up the stack.
48  if( !m_vertices )
49  throw std::bad_alloc();
50 
51  memset( m_vertices, 0x00, aSize * sizeof( VERTEX ) );
52 }
53 
54 
56 {
57  free( m_vertices );
58 }
59 
60 
62 {
63  // Nothing has to be done, as the noncached container
64  // does not care about VERTEX_ITEMs ownership
65 }
66 
67 
68 VERTEX* NONCACHED_CONTAINER::Allocate( unsigned int aSize )
69 {
70  if( m_freeSpace < aSize )
71  {
72  // Double the space
73  VERTEX* newVertices =
74  static_cast<VERTEX*>( realloc( m_vertices, m_currentSize * 2 * sizeof( VERTEX ) ) );
75 
76  if( newVertices != nullptr )
77  {
78  m_vertices = newVertices;
80  m_currentSize *= 2;
81  }
82  else
83  {
84  throw std::bad_alloc();
85  }
86  }
87 
88  VERTEX* freeVertex = &m_vertices[m_freePtr];
89 
90  // Move to the next free chunk
91  m_freePtr += aSize;
92  m_freeSpace -= aSize;
93 
94  return freeVertex;
95 }
96 
97 
99 {
100  m_freePtr = 0;
102 }
virtual VERTEX * Allocate(unsigned int aSize) override
Return allocated space for the requested number of vertices associated with the current item (set wit...
virtual void SetItem(VERTEX_ITEM *aItem) override
Set the item for the further actions.
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:191
unsigned int m_freePtr
< Index of the free first space where a vertex can be stored
unsigned int m_currentSize
Store the initial size, so it can be resized to this on Clear()
unsigned int m_freeSpace
Current container size, expressed in vertices.
virtual void Clear() override
Remove all data stored in the container and restores its original state.
Class to store instances of VERTEX without caching.
NONCACHED_CONTAINER(unsigned int aSize=DEFAULT_SIZE)
Construct a non-cached container object.