1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
NrrdIO: stand-alone code for basic nrrd functionality
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "NrrdIO.h"
void
_airLenSet(airArray *a, unsigned int len) {
a->len = len;
/* printf(" HEY: len = %d\n", a->len); */
if (a->lenP) {
*(a->lenP) = len;
/* printf(" HEY: *(a->lenP) = *(%lu) = %d\n",
(unsigned long)a->lenP, *(a->lenP)); */
}
}
void
_airSetData(airArray *a, void *data) {
a->data = data;
if (a->dataP) {
*(a->dataP) = data;
}
}
/*
******** airArrayNew()
**
** creates a new airArray struct and returns a pointer to it.
** dataP is a pointer to the user's data pointer
** lenP is a pointer to the user's array length variable (optional)
** unit is the size (in bytes) of one element in the array
** incr is the number of units by which the array will grow or shrink
**
** returns NULL on error, or the new airArray pointer if okay
** errors: bogus arguments, or couldn't alloc airArray struct
**
** --> The user CAN NOT change the pointer variable (of which *dataP
** is the address) after this is called, or else everything will
** get all bolloxed up. The same goes for the array length
** variable, if its address is passed- though in that case the
** correct value will over-write any other.
*/
airArray *
airArrayNew(void **dataP, unsigned int *lenP, size_t unit, unsigned int incr) {
airArray *a;
if (unit<=0 || incr<=0) {
return NULL;
}
a = AIR_CALLOC(1, airArray);
if (!a) {
return NULL;
}
a->dataP = dataP;
_airSetData(a, NULL);
a->lenP = lenP;
_airLenSet(a, 0);
a->incr = incr;
a->unit = unit;
a->noReallocWhenSmaller = AIR_FALSE;
a->allocCB = NULL;
a->freeCB = NULL;
a->initCB = NULL;
a->doneCB = NULL;
return a;
}
/*
******** airArrayStructCB()
**
** set callbacks to maintain array of structs
*/
void
airArrayStructCB(airArray *a,
void (*initCB)(void *), void (*doneCB)(void *)) {
if (a) {
a->initCB = initCB;
a->doneCB = doneCB;
a->allocCB = NULL;
a->freeCB = NULL;
}
}
/*
******** airArrayPointerCB()
**
** set callbacks to maintain array of pointers
*/
void
airArrayPointerCB(airArray *a,
void *(*allocCB)(void), void *(*freeCB)(void *)) {
if (a) {
a->initCB = NULL;
a->doneCB = NULL;
a->allocCB = allocCB;
a->freeCB = freeCB;
}
}
/*
******** airArrayLenPreSet()
**
** allocates the array to hold up to given length, without
** actually changing the length. In order for this to be
** useful, this also turns on noReallocWhenSmaller
**
** NB: this used to have a "boolean" return to indicate allocation
** error, but nothing in Teem actually did the error checking. Now
** conscientious users can look at NULL-ity of a->data to detect such
** an error.
*/
void
airArrayLenPreSet(airArray *a, unsigned int newlen) {
/* char me[]="airArrayLenPreSet"; */
unsigned int newsize;
void *newdata;
if (!a) {
return;
}
if (newlen == 0) {
/* there is no pre-set length, turn off noReallocWhenSmaller */
a->noReallocWhenSmaller = AIR_FALSE;
} else {
newsize = (newlen-1)/a->incr + 1;
/*
fprintf(stderr, "!%s: newlen = %u, incr = %u -> newsize = %u\n", me,
newlen, a->incr, newsize);
fprintf(stderr, "!%s: a->size = %u, a->len = %u, a->unit = %u\n", me,
a->size, a->len, a->unit);
*/
if (newsize > a->size) {
newdata = calloc(newsize*a->incr, a->unit);
/*
fprintf(stderr, "!%s: a->data = %p, newdata = %p\n", me,
a->data, newdata);
*/
if (!newdata) {
free(a->data);
_airSetData(a, NULL);
return;
}
if (a->data) {
memcpy(newdata, a->data, AIR_MIN(a->len*a->unit,
newsize*a->incr*a->unit));
free(a->data);
}
_airSetData(a, newdata);
a->size = newsize;
}
a->noReallocWhenSmaller = AIR_TRUE;
}
/* fprintf(stderr, "!%s: returning data %p\n", me, a->data); */
return;
}
/*
******** airArrayLenSet()
**
** Set the length of the array, allocating or freeing as needed
**
** returns 1 on error, otherwise 0 if okay
** possible errors: bogus arguments, or couldn't allocate new memory segment
**
** In case we can't allocate the new space, the old space is left untouched,
** however if the new length is smaller, the free/done callbacks will
** have been called on invalidated elements
**
** NB: this used to have a "boolean" return to indicate allocation
** error, but almost nothing in Teem actually did the error checking.
** Now conscientious users can look at NULL-ity of a->data to detect
** such an error.
*/
void
airArrayLenSet(airArray *a, unsigned int newlen) {
/* char me[]="airArrayLenSet"; */
unsigned int ii, newsize;
void *addr, *newdata;
if (!a) {
/* user is a moron, what can you do */
return;
}
if (newlen == a->len) {
/* nothing to do */
return;
}
/* call freeCB/doneCB on all the elements which are going bye-bye */
/* Wed Sep 12 14:40:45 EDT 2007: the order in which these called is
now ascending, instead of descending (as was the way before) */
if (newlen < a->len && (a->freeCB || a->doneCB)) {
for (ii=newlen; ii<a->len; ii++) {
addr = (char*)(a->data) + ii*a->unit;
if (a->freeCB) {
(a->freeCB)(*((void**)addr));
} else {
(a->doneCB)(addr);
}
}
}
newsize = newlen ? (newlen-1)/a->incr + 1 : 0;
if (newsize != a->size) {
/* we have to change the size of the array */
if (newsize) {
/* array should be bigger or smaller, but not zero-length */
if (newsize > a->size
|| (newsize < a->size && !(a->noReallocWhenSmaller)) ) {
newdata = calloc(newsize*a->incr, a->unit);
if (!newdata) {
free(a->data);
_airSetData(a, NULL);
return;
}
memcpy(newdata, a->data, AIR_MIN(a->len*a->unit,
newsize*a->incr*a->unit));
free(a->data);
_airSetData(a, newdata);
a->size = newsize;
}
} else {
/* array should be zero-length */
free(a->data);
_airSetData(a, NULL);
a->size = newsize;
}
}
/* else new size is still within current allocated length,
and neither "size" nor "data" need to change */
/* call allocCB/initCB on newly created elements */
if (newlen > a->len && (a->allocCB || a->initCB)) {
for (ii=a->len; ii<newlen; ii++) {
addr = (char*)(a->data) + ii*a->unit;
if (a->allocCB) {
*((void**)addr) = (a->allocCB)();
} else {
(a->initCB)(addr);
}
}
}
_airLenSet(a, newlen);
return;
}
/*
******** airArrayLenIncr()
**
** Like airArrayLenSet, but works with an increment instead of an
** absolute length. Return value is different:
** got NULL: return 0
** allocation error: return 0, and a->data set to NULL
** no error, delta > 0: return index of 1st element in newly allocated
** segment (a->len before length was increased)
** no error, delta <= 0: return 0, and a->data unchanged
**
** HEY: it is apparently not clear how to do error checking (aside from
** looking at a->data) when there was NO data previously allocated, and the
** first index of the newly allocated data is zero...
*/
unsigned int
airArrayLenIncr(airArray *a, int delta) {
/* char me[]="airArrayLenIncr"; */
unsigned int oldlen, ret;
if (!a) {
return 0;
}
if (delta < 0 && (unsigned int)(-delta) > a->len) {
/* error: asked for newlength to be negative */
airArrayLenSet(a, 0);
return 0;
}
oldlen = a->len;
airArrayLenSet(a, oldlen + delta);
if (!a->data) {
/* allocation error */
ret = 0;
} else {
ret = (delta <= 0 ? 0 : oldlen);
}
return ret;
}
/*
******** airArrayNuke()
**
** free both the memory pointed to by the struct and the struct itself
*/
airArray *
airArrayNuke(airArray *a) {
if (a) {
airArrayLenSet(a, 0);
free(a);
}
return NULL;
}
/*
******** airArrayNix()
**
** frees just the struct, leaving the memory it points to untouched
*/
airArray *
airArrayNix(airArray *a) {
if (a) {
free(a);
}
return NULL;
}
|