2 libanim - hierarchical keyframe animation library
3 Copyright (C) 2012-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* The array descriptor keeps auxilliary information needed to manipulate
25 * the dynamic array. It's allocated adjacent to the array buffer.
30 int bufsz; /* not including the descriptor */
33 #define DESC(x) ((struct arrdesc*)((char*)(x) - sizeof(struct arrdesc)))
35 void *anm_dynarr_alloc(int elem, int szelem)
39 if(!(desc = malloc(elem * szelem + sizeof *desc))) {
42 desc->nelem = desc->max_elem = elem;
43 desc->szelem = szelem;
44 desc->bufsz = elem * szelem;
45 return (char*)desc + sizeof *desc;
48 void anm_dynarr_free(void *da)
55 void *anm_dynarr_resize(void *da, int elem)
64 newsz = desc->szelem * elem;
66 if(!(tmp = realloc(desc, newsz + sizeof *desc))) {
71 desc->nelem = desc->max_elem = elem;
73 return (char*)desc + sizeof *desc;
76 int anm_dynarr_empty(void *da)
78 return DESC(da)->nelem ? 0 : 1;
81 int anm_dynarr_size(void *da)
83 return DESC(da)->nelem;
88 void *anm_dynarr_push(void *da, void *item)
96 if(nelem >= desc->max_elem) {
99 int newsz = desc->max_elem ? desc->max_elem * 2 : 1;
101 if(!(tmp = anm_dynarr_resize(da, newsz))) {
102 fprintf(stderr, "failed to resize\n");
110 memcpy((char*)da + desc->nelem++ * desc->szelem, item, desc->szelem);
114 void *anm_dynarr_pop(void *da)
116 struct arrdesc *desc;
122 if(!nelem) return da;
124 if(nelem <= desc->max_elem / 3) {
127 int newsz = desc->max_elem / 2;
129 if(!(tmp = anm_dynarr_resize(da, newsz))) {
130 fprintf(stderr, "failed to resize\n");