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
|
/* dynarray.c -- an expanding array of same-size members
*
* Copyright (c) 1994-2020 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <assert.h>
#include <memory.h>
#include "dynarray.h"
#include "util.h"
#include "xmalloc.h"
EXPORTED void dynarray_init(struct dynarray *da, size_t membsize)
{
assert(membsize);
memset(da, 0, sizeof(struct dynarray));
da->membsize = membsize;
}
EXPORTED void dynarray_fini(struct dynarray *da)
{
free(da->data);
memset(da, 0, sizeof(struct dynarray));
}
EXPORTED struct dynarray *dynarray_new(size_t membsize)
{
struct dynarray *da = xmalloc(sizeof(struct dynarray));
dynarray_init(da, membsize);
return da;
}
EXPORTED void dynarray_free(struct dynarray **dap)
{
if (!dap || *dap == NULL) return;
free((*dap)->data);
free(*dap);
*dap = NULL;
}
#define QUANTUM 16
static inline int grow(int have, int want)
{
int x = MAX(QUANTUM, have);
while (x < want)
x *= 2;
return x;
}
/*
* Ensure the index @newalloc exists in the array, if necessary expanding the
* array, and if necessary NULL-filling all the intervening elements.
* Note that we always ensure an empty slot past the last reported
* index, so that we can pass data[] to execve() or other routines that
* assume a NULL terminator.
*/
static void ensure_alloc(struct dynarray *da, int newalloc)
{
assert(newalloc >= 0);
if (newalloc < da->alloc)
return;
newalloc = grow(da->alloc, newalloc + 1);
da->data = xzrealloc(da->data,
da->membsize * da->alloc,
da->membsize * newalloc);
da->alloc = newalloc;
}
static inline int adjust_index_ro(const struct dynarray *da, int idx)
{
if (idx >= da->count)
return -1;
else if (idx < 0)
idx += da->count;
return idx;
}
static inline int adjust_index_rw(struct dynarray *da, int idx, int len)
{
if (idx >= da->count) {
ensure_alloc(da, idx+len);
} else if (idx < 0) {
idx += da->count;
if (idx >= 0 && len)
ensure_alloc(da, da->count+len);
} else if (len) {
ensure_alloc(da, da->count+len);
}
return idx;
}
EXPORTED int dynarray_size(struct dynarray *da)
{
return da->count;
}
__attribute__((unused))
static char *dump(struct dynarray *da)
{
struct buf buf = BUF_INITIALIZER;
buf_printf(&buf, "{membsize=%zu count=%d alloc=%d data=%p: ", da->membsize, da->count, da->alloc, da->data);
int i;
for (i = 0; i < da->alloc; i++) {
void *memb = da->data + i * da->membsize;
buf_putc(&buf, '[');
size_t j;
for (j = 0; j < da->membsize; j++) {
buf_printf(&buf, "%02x", *((unsigned char*) memb + j) & 0xff);
if (j < da->membsize - 1) buf_putc(&buf, ' ');
}
buf_putc(&buf, ']');
}
buf_cstring(&buf);
return buf_release(&buf);
}
EXPORTED void dynarray_append(struct dynarray *da, void *memb)
{
ensure_alloc(da, da->count+1);
memcpy(da->data + da->count * da->membsize, memb, da->membsize);
da->count++;
}
EXPORTED void dynarray_set(struct dynarray *da, int idx, void *memb)
{
if ((idx = adjust_index_rw(da, idx, 0)) < 0)
return;
memcpy(da->data + idx * da->membsize, memb, da->membsize);
if (idx >= da->count)
da->count = idx + 1;
}
EXPORTED void *dynarray_nth(const struct dynarray *da, int idx)
{
if ((idx = adjust_index_ro(da, idx)) < 0)
return NULL;
return da->data + idx * da->membsize;
}
EXPORTED void dynarray_truncate(struct dynarray *da, int newlen)
{
if (newlen == da->count)
return;
if (newlen > da->count) {
ensure_alloc(da, newlen);
} else {
int i;
for (i = newlen ; i < da->count ; i++) {
memset(da->data + i * da->membsize, 0, da->membsize);
}
}
da->count = newlen;
}
EXPORTED void dynarray_sort(struct dynarray *da,
int (*compare)(const void *, const void *))
{
if (!da || !da->count) return;
qsort(da->data, da->count, da->membsize,
(int (*)(const void *, const void *))compare);
}
|