2 * miscellaneous utilities
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
22 * The interface is documented in the header file.
24 * Note that we never end up needing to explicitly set errno, not even
25 * for malloc failure, because Unix98 guarantees errno=ENOMEM for us
26 * automatically in that case.
30 voidacc_ctor(struct voidacc *va)
32 va->n_used = 1; /* We begin with 1 entry already for zero termination. */
33 va->n_avail = 4; /* arbitrary initial value */
34 va->result = (void**)calloc(sizeof(void*), va->n_avail);
35 return va->result ? 0 : (-1);
39 voidacc_acc(struct voidacc *va, void* x)
41 /* Ensure that we have enough space, or die. */
42 if (va->n_used >= va->n_avail) { /* if we've run out of space */
43 /* We need to allocate more space. */
44 int new_n_avail = 1 + 2 * va->n_avail;
45 void** new_result = (void**)calloc(sizeof(void*), new_n_avail);
50 /* Copy old result into new space. */
51 for (i = va->n_used; --i >= 0; ) {
52 new_result[i] = va->result[i];
55 va->result = new_result;
56 va->n_avail = new_n_avail;
59 /* If we get to this point, we have enough space to store x.
61 * Note that since we cleverly counted the 0 as part of the space
62 * used, now we need to subtract one to get the correct offset to
64 va->result[va->n_used++ - 1] = x;
69 voidacc_give_away_result(struct voidacc *va)
71 /* (We could do realloc(3) here to try to shrink the result down
72 * to minimum size, but that's not really needed for the
73 * directory-iteration application this was originally written
74 * for, so for now we just do the simplest thing which could
76 void **result_tmp = va->result;
82 voidacc_dtor(struct voidacc *va)
84 /* if result has been allocated and hasn't been given away */
86 free(voidacc_give_away_result(va));