File: ptrarray.c

package info (click to toggle)
cyrus-imapd 3.12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,544 kB
  • sloc: ansic: 280,382; perl: 146,834; javascript: 9,624; sh: 5,730; yacc: 2,660; cpp: 2,263; makefile: 2,103; lex: 675; xml: 621; awk: 303; python: 273; asm: 262
file content (228 lines) | stat: -rw-r--r-- 6,017 bytes parent folder | download | duplicates (6)
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
/* ptrarray.c -- an expanding array of pointers
 *
 * Copyright (c) 1994-2011 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.
 *
 * Author: Greg Banks
 * Start Date: 2011/01/11
 */

#include "ptrarray.h"
#include <memory.h>
#include "util.h"
#include "xmalloc.h"

EXPORTED ptrarray_t *ptrarray_new(void)
{
    return xzmalloc(sizeof(ptrarray_t));
}

EXPORTED void ptrarray_fini(ptrarray_t *pa)
{
    if (!pa)
        return;

    xzfree(pa->data);
    pa->count = 0;
    pa->alloc = 0;
}

EXPORTED void ptrarray_free(ptrarray_t *pa)
{
    if (!pa)
        return;
    ptrarray_fini(pa);
    free(pa);
}

#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(ptrarray_t *pa, int newalloc)
{
    if (newalloc < pa->alloc)
        return;
    newalloc = grow(pa->alloc, newalloc + 1);
    pa->data = xzrealloc(pa->data,
                         sizeof(void *) * pa->alloc,
                         sizeof(void *) * newalloc);
    pa->alloc = newalloc;
}

static inline int adjust_index_ro(const ptrarray_t *pa, int idx)
{
    if (idx >= pa->count)
        return -1;
    else if (idx < 0)
        idx += pa->count;
    return idx;
}

static inline int adjust_index_rw(ptrarray_t *pa, int idx, int len)
{
    if (idx >= pa->count) {
        ensure_alloc(pa, idx+len);
    } else if (idx < 0) {
        idx += pa->count;
        if (idx >= 0 && len)
            ensure_alloc(pa, pa->count+len);
    } else if (len) {
        ensure_alloc(pa, pa->count+len);
    }
    return idx;
}

EXPORTED void ptrarray_add(ptrarray_t *pa, void *p)
{
    if (ptrarray_find(pa, p, 0) < 0)
        ptrarray_append(pa, p);
}

EXPORTED void ptrarray_append(ptrarray_t *pa, void *p)
{
    ensure_alloc(pa, pa->count+1);
    pa->data[pa->count++] = p;
}

EXPORTED void ptrarray_set(ptrarray_t *pa, int idx, void *p)
{
    if ((idx = adjust_index_rw(pa, idx, 0)) < 0)
        return;
    pa->data[idx] = p;
}

static inline void _ptrarray_insert(ptrarray_t *pa, int idx, void *p)
{
    if (idx < pa->count)
        memmove(pa->data+idx+1, pa->data+idx,
                sizeof(void *) * (pa->count-idx));
    pa->data[idx] = p;
    pa->count++;
}

EXPORTED void ptrarray_insert(ptrarray_t *pa, int idx, void *p)
{
    if ((idx = adjust_index_rw(pa, idx, 1)) < 0)
        return;
    _ptrarray_insert(pa, idx, p);
}

EXPORTED void *ptrarray_remove(ptrarray_t *pa, int idx)
{
    void *p;
    if ((idx = adjust_index_ro(pa, idx)) < 0)
        return NULL;
    p = pa->data[idx];
    pa->count--;
    if (idx < pa->count)
        memmove(pa->data+idx, pa->data+idx+1,
                sizeof(void *) * (pa->count-idx));
    return p;
}

EXPORTED void ptrarray_truncate(ptrarray_t *pa, int newlen)
{
    int i;

    if (newlen == pa->count)
        return;

    if (newlen > pa->count) {
        ensure_alloc(pa, newlen);
    } else {
        for (i = newlen ; i < pa->count ; i++) {
            pa->data[i] = NULL;
        }
    }
    pa->count = newlen;
}

EXPORTED void *ptrarray_nth(const ptrarray_t *pa, int idx)
{
    if ((idx = adjust_index_ro(pa, idx)) < 0)
        return NULL;
    return pa->data[idx];
}

EXPORTED void **ptrarray_takevf(ptrarray_t *pa)
{
    void **d = pa->data;
    pa->data = NULL;
    pa->count = pa->alloc = 0;
    ptrarray_free(pa);
    return d;
}

EXPORTED int ptrarray_find(const ptrarray_t *pa, void *match, int starting)
{
    if (!pa) return -1;
    int i;

    for (i = starting ; i < pa->count ; i++)
        if (match == pa->data[i])
            return i;
    return -1;
}

EXPORTED void ptrarray_sort(ptrarray_t *pa,
                            int (*compare)(const void **, const void **))
{
    if (!pa) return;
    qsort(pa->data, pa->count, sizeof(void*),
            (int (*)(const void *, const void *))compare);
}

EXPORTED int ptrarray_size(const ptrarray_t *pa)
{
    if (!pa) return 0;
    return pa->count;
}