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