|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | +#ifndef INCLUDE_array_h__ |
| 8 | +#define INCLUDE_array_h__ |
| 9 | + |
| 10 | +#include "util.h" |
| 11 | + |
| 12 | +#define git_array_t(type) struct { type *ptr; size_t size, asize; } |
| 13 | + |
| 14 | +#define git_array_init(a) \ |
| 15 | + do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0) |
| 16 | + |
| 17 | +#define git_array_clear(a) \ |
| 18 | + do { git__free((a).ptr); git_array_init(a); } while (0) |
| 19 | + |
| 20 | +#define git_array_grow(a) do { \ |
| 21 | + void *new_array; size_t new_size = \ |
| 22 | + ((a).asize >= 256) ? (a).asize + 256 : ((a).asize >= 8) ? (a).asize * 2 : 8; \ |
| 23 | + new_array = git__realloc((a).ptr, new_size * sizeof(*(a).ptr)); \ |
| 24 | + if (!new_array) { git_array_clear(a); } \ |
| 25 | + else { (a).ptr = new_array; (a).asize = new_size; } \ |
| 26 | + } while (0) |
| 27 | + |
| 28 | +#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr) |
| 29 | + |
| 30 | +#define git_array_alloc(a, el) do { \ |
| 31 | + if ((a).size >= (a).asize) git_array_grow(a); \ |
| 32 | + (el) = (a).ptr ? &(a).ptr[(a).size++] : NULL; \ |
| 33 | + } while (0) |
| 34 | + |
| 35 | +#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL) |
| 36 | + |
| 37 | +#define git_array_get(a, i) (((i) < (a).size) ? &(a).ptr[(i)] : NULL) |
| 38 | + |
| 39 | +#define git_array_size(a) (a).size |
| 40 | + |
| 41 | +#endif |
0 commit comments