1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
/*
(c) Copyright 2012-2013 DirectFB integrated media GmbH
(c) Copyright 2001-2013 The world wide DirectFB Open Source Community (directfb.org)
(c) Copyright 2000-2004 Convergence (integrated media) GmbH
All rights reserved.
Written by Denis Oliver Kropp <dok@directfb.org>,
Andreas Shimokawa <andi@directfb.org>,
Marek Pikarski <mass@directfb.org>,
Sven Neumann <neo@directfb.org>,
Ville Syrjälä <syrjala@sci.fi> and
Claudio Ciccani <klan@users.sf.net>.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <direct/debug.h>
#include <direct/mem.h>
#include <direct/memcpy.h>
#include <fusion/object.h>
#include <fusion/shmalloc.h>
#include <fusion/vector.h>
static __inline__ bool ensure_capacity( FusionVector *vector )
{
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( vector->capacity > 0 );
if (!vector->elements) {
if (vector->pool)
vector->elements = SHMALLOC( vector->pool, vector->capacity * sizeof(void*) );
else
vector->elements = D_MALLOC( vector->capacity * sizeof(void*) );
if (!vector->elements)
return false;
}
else if (vector->count == vector->capacity) {
void *elements;
void *oldelements = vector->elements;
int capacity = vector->capacity << 1;
if (vector->pool)
elements = SHMALLOC( vector->pool, capacity * sizeof(void*) );
else
elements = D_MALLOC( capacity * sizeof(void*) );
if (!elements)
return false;
direct_memcpy( elements, vector->elements,
vector->count * sizeof(void*) );
vector->elements = elements;
vector->capacity = capacity;
if (vector->pool)
SHFREE( vector->pool, oldelements );
else
D_FREE( oldelements );
}
return true;
}
void
fusion_vector_init( FusionVector *vector,
int capacity,
FusionSHMPoolShared *pool )
{
D_ASSERT( vector != NULL );
D_ASSERT( capacity > 0 );
vector->elements = NULL;
vector->count = 0;
vector->capacity = capacity;
vector->pool = pool;
D_MAGIC_SET( vector, FusionVector );
}
void
fusion_vector_destroy( FusionVector *vector )
{
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( vector->count == 0 || vector->elements != NULL );
if (vector->elements) {
if (vector->pool)
SHFREE( vector->pool, vector->elements );
else
D_FREE( vector->elements );
vector->elements = NULL;
}
D_MAGIC_CLEAR( vector );
}
DirectResult
fusion_vector_add( FusionVector *vector,
void *element )
{
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( element != NULL );
/* Make sure there's a free entry left. */
if (!ensure_capacity( vector ))
return D_OOSHM();
/* Add the element to the vector. */
vector->elements[vector->count++] = element;
return DR_OK;
}
DirectResult
fusion_vector_insert( FusionVector *vector,
void *element,
int index )
{
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( element != NULL );
D_ASSERT( index >= 0 );
D_ASSERT( index <= vector->count );
/* Make sure there's a free entry left. */
if (!ensure_capacity( vector ))
return D_OOSHM();
/* Move elements from insertion point one up. */
memmove( &vector->elements[ index + 1 ],
&vector->elements[ index ],
(vector->count - index) * sizeof(void*) );
/* Insert the element into the vector. */
vector->elements[index] = element;
/* Increase the element counter. */
vector->count++;
return DR_OK;
}
DirectResult
fusion_vector_move( FusionVector *vector,
int from,
int to )
{
void *element;
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( from >= 0 );
D_ASSERT( from < vector->count );
D_ASSERT( to >= 0 );
D_ASSERT( to < vector->count );
if (to == from)
return DR_OK;
/* Save the element. */
element = vector->elements[from];
/* Move elements that lie on the way to the new position. */
if (to > from) {
/* Element is moving up -> move other elements down. */
memmove( &vector->elements[ from ],
&vector->elements[ from + 1 ],
(to - from) * sizeof(void*) );
}
else {
/* Element is moving down -> move other elements up. */
memmove( &vector->elements[ to + 1 ],
&vector->elements[ to ],
(from - to) * sizeof(void*) );
}
/* Restore the element at the new position. */
vector->elements[to] = element;
return DR_OK;
}
DirectResult
fusion_vector_remove( FusionVector *vector,
int index )
{
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( index >= 0 );
D_ASSERT( index < vector->count );
/* Move elements after this element one down. */
memmove( &vector->elements[ index ],
&vector->elements[ index + 1 ],
(vector->count - index - 1) * sizeof(void*) );
/* Decrease the element counter. */
vector->count--;
return DR_OK;
}
DirectResult
fusion_vector_remove_last( FusionVector *vector )
{
D_MAGIC_ASSERT( vector, FusionVector );
D_ASSERT( vector->count > 0 );
/* Decrease the element counter. */
vector->count--;
return DR_OK;
}
|