scenefile

view src/dynarr.c @ 2:c15992cedec9

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