Mercurial > ~dholland > hg > tradcpp > index.cgi
comparison array.c @ 39:337110e7240a
Pass the size to free; it makes debug checking easier.
author | David A. Holland |
---|---|
date | Sat, 30 Mar 2013 21:17:47 -0400 |
parents | b156910b59b2 |
children |
comparison
equal
deleted
inserted
replaced
38:b156910b59b2 | 39:337110e7240a |
---|---|
45 | 45 |
46 void | 46 void |
47 array_destroy(struct array *a) | 47 array_destroy(struct array *a) |
48 { | 48 { |
49 array_cleanup(a); | 49 array_cleanup(a); |
50 dofree(a); | 50 dofree(a, sizeof(*a)); |
51 } | 51 } |
52 | 52 |
53 void | 53 void |
54 array_init(struct array *a) | 54 array_init(struct array *a) |
55 { | 55 { |
59 | 59 |
60 void | 60 void |
61 array_cleanup(struct array *a) | 61 array_cleanup(struct array *a) |
62 { | 62 { |
63 arrayassert(a->num == 0); | 63 arrayassert(a->num == 0); |
64 dofree(a->v); | 64 dofree(a->v, a->max * sizeof(a->v[0])); |
65 #ifdef ARRAYS_CHECKED | 65 #ifdef ARRAYS_CHECKED |
66 a->v = NULL; | 66 a->v = NULL; |
67 #endif | 67 #endif |
68 } | 68 } |
69 | 69 |
76 if (num > a->max) { | 76 if (num > a->max) { |
77 newmax = a->max; | 77 newmax = a->max; |
78 while (num > newmax) { | 78 while (num > newmax) { |
79 newmax = newmax ? newmax*2 : 4; | 79 newmax = newmax ? newmax*2 : 4; |
80 } | 80 } |
81 newptr = dorealloc(a->v, newmax*sizeof(*a->v)); | 81 newptr = dorealloc(a->v, a->max * sizeof(a->v[0]), |
82 newmax * sizeof(a->v[0])); | |
82 a->v = newptr; | 83 a->v = newptr; |
83 a->max = newmax; | 84 a->max = newmax; |
84 } | 85 } |
85 a->num = num; | 86 a->num = num; |
86 } | 87 } |