goat3d

annotate libs/anim/dynarr.c @ 29:3d669155709d

- switched the unix build to use the internal vmath/anim as well - fixed a memory corruption issue which was caused by including the system-wide installed version of the anim.h header file - started the ass2goat assimp->goat3d converter
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 21:53:03 +0300
parents
children
rev   line source
nuclear@27 1 #include <stdio.h>
nuclear@27 2 #include <stdlib.h>
nuclear@27 3 #include <string.h>
nuclear@27 4 #include "dynarr.h"
nuclear@27 5
nuclear@27 6 /* The array descriptor keeps auxilliary information needed to manipulate
nuclear@27 7 * the dynamic array. It's allocated adjacent to the array buffer.
nuclear@27 8 */
nuclear@27 9 struct arrdesc {
nuclear@27 10 int nelem, szelem;
nuclear@27 11 int max_elem;
nuclear@27 12 int bufsz; /* not including the descriptor */
nuclear@27 13 };
nuclear@27 14
nuclear@27 15 #define DESC(x) ((struct arrdesc*)((char*)(x) - sizeof(struct arrdesc)))
nuclear@27 16
nuclear@27 17 void *dynarr_alloc(int elem, int szelem)
nuclear@27 18 {
nuclear@27 19 struct arrdesc *desc;
nuclear@27 20
nuclear@27 21 if(!(desc = malloc(elem * szelem + sizeof *desc))) {
nuclear@27 22 return 0;
nuclear@27 23 }
nuclear@27 24 desc->nelem = desc->max_elem = elem;
nuclear@27 25 desc->szelem = szelem;
nuclear@27 26 desc->bufsz = elem * szelem;
nuclear@27 27 return (char*)desc + sizeof *desc;
nuclear@27 28 }
nuclear@27 29
nuclear@27 30 void dynarr_free(void *da)
nuclear@27 31 {
nuclear@27 32 if(da) {
nuclear@27 33 free(DESC(da));
nuclear@27 34 }
nuclear@27 35 }
nuclear@27 36
nuclear@27 37 void *dynarr_resize(void *da, int elem)
nuclear@27 38 {
nuclear@27 39 int newsz;
nuclear@27 40 void *tmp;
nuclear@27 41 struct arrdesc *desc;
nuclear@27 42
nuclear@27 43 if(!da) return 0;
nuclear@27 44 desc = DESC(da);
nuclear@27 45
nuclear@27 46 newsz = desc->szelem * elem;
nuclear@27 47
nuclear@27 48 if(!(tmp = realloc(desc, newsz + sizeof *desc))) {
nuclear@27 49 return 0;
nuclear@27 50 }
nuclear@27 51 desc = tmp;
nuclear@27 52
nuclear@27 53 desc->nelem = desc->max_elem = elem;
nuclear@27 54 desc->bufsz = newsz;
nuclear@27 55 return (char*)desc + sizeof *desc;
nuclear@27 56 }
nuclear@27 57
nuclear@27 58 int dynarr_empty(void *da)
nuclear@27 59 {
nuclear@27 60 return DESC(da)->nelem ? 0 : 1;
nuclear@27 61 }
nuclear@27 62
nuclear@27 63 int dynarr_size(void *da)
nuclear@27 64 {
nuclear@27 65 return DESC(da)->nelem;
nuclear@27 66 }
nuclear@27 67
nuclear@27 68
nuclear@27 69 /* stack semantics */
nuclear@27 70 void *dynarr_push(void *da, void *item)
nuclear@27 71 {
nuclear@27 72 struct arrdesc *desc;
nuclear@27 73 int nelem;
nuclear@27 74
nuclear@27 75 desc = DESC(da);
nuclear@27 76 nelem = desc->nelem;
nuclear@27 77
nuclear@27 78 if(nelem >= desc->max_elem) {
nuclear@27 79 /* need to resize */
nuclear@27 80 struct arrdesc *tmp;
nuclear@27 81 int newsz = desc->max_elem ? desc->max_elem * 2 : 1;
nuclear@27 82
nuclear@27 83 if(!(tmp = dynarr_resize(da, newsz))) {
nuclear@27 84 fprintf(stderr, "failed to resize\n");
nuclear@27 85 return da;
nuclear@27 86 }
nuclear@27 87 da = tmp;
nuclear@27 88 desc = DESC(da);
nuclear@27 89 desc->nelem = nelem;
nuclear@27 90 }
nuclear@27 91
nuclear@27 92 memcpy((char*)da + desc->nelem++ * desc->szelem, item, desc->szelem);
nuclear@27 93 return da;
nuclear@27 94 }
nuclear@27 95
nuclear@27 96 void *dynarr_pop(void *da)
nuclear@27 97 {
nuclear@27 98 struct arrdesc *desc;
nuclear@27 99 int nelem;
nuclear@27 100
nuclear@27 101 desc = DESC(da);
nuclear@27 102 nelem = desc->nelem;
nuclear@27 103
nuclear@27 104 if(!nelem) return da;
nuclear@27 105
nuclear@27 106 if(nelem <= desc->max_elem / 3) {
nuclear@27 107 /* reclaim space */
nuclear@27 108 struct arrdesc *tmp;
nuclear@27 109 int newsz = desc->max_elem / 2;
nuclear@27 110
nuclear@27 111 if(!(tmp = dynarr_resize(da, newsz))) {
nuclear@27 112 fprintf(stderr, "failed to resize\n");
nuclear@27 113 return da;
nuclear@27 114 }
nuclear@27 115 da = tmp;
nuclear@27 116 desc = DESC(da);
nuclear@27 117 desc->nelem = nelem;
nuclear@27 118 }
nuclear@27 119 desc->nelem--;
nuclear@27 120
nuclear@27 121 return da;
nuclear@27 122 }