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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
32 #include <cstring>
33 #include <cstdlib>
34 
35 using namespace KIGFX;
36 
38  VERTEX_CONTAINER( aSize ), m_freePtr( 0 )
39 {
40  m_vertices = static_cast<VERTEX*>( malloc( aSize * sizeof( VERTEX ) ) );
41  memset( m_vertices, 0x00, aSize * sizeof( VERTEX ) );
42 }
43 
44 
46 {
47  free( m_vertices );
48 }
49 
50 
52 {
53  // Nothing has to be done, as the noncached container
54  // does not care about VERTEX_ITEMs ownership
55 }
56 
57 
58 VERTEX* NONCACHED_CONTAINER::Allocate( unsigned int aSize )
59 {
60  if( m_freeSpace < aSize )
61  {
62  // Double the space
63  VERTEX* newVertices = static_cast<VERTEX*>( realloc( m_vertices,
64  m_currentSize * 2 *
65  sizeof(VERTEX) ) );
66 
67  if( newVertices != NULL )
68  {
69  m_vertices = newVertices;
71  m_currentSize *= 2;
72  }
73  else
74  {
75  return NULL;
76  }
77  }
78 
79  VERTEX* freeVertex = &m_vertices[m_freePtr];
80 
81  // Move to the next free chunk
82  m_freePtr += aSize;
83  m_freeSpace -= aSize;
84 
85  return freeVertex;
86 }
87 
88 
90 {
91  m_freePtr = 0;
93 }
virtual VERTEX * Allocate(unsigned int aSize) override
Function Delete() erases the selected item.
virtual void SetItem(VERTEX_ITEM *aItem) override
Function Allocate() returns allocated space (possibly resizing the used memory chunk or allocating a ...
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:56
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()
VERTEX * m_vertices
State flags.
virtual ~NONCACHED_CONTAINER()
Function SetItem() sets the item in order to modify or finishes its current modifications.
NONCACHED_CONTAINER(unsigned int aSize=defaultInitSize)
unsigned int m_freeSpace
How big is the current container, expressed in vertices.
virtual void Clear() override
Function GetSize() returns amount of vertices currently stored in the container.
Class to store instances of VERTEX without caching.