]> git.proxmox.com Git - libgit2.git/blame - src/array.h
odb__hashlink: check st.st_size before casting
[libgit2.git] / src / array.h
CommitLineData
596b121a
RB
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
72556cc6 10#include "common.h"
596b121a 11
ef3374a8
RB
12/*
13 * Use this to declare a typesafe resizable array of items, a la:
14 *
15 * git_array_t(int) my_ints = GIT_ARRAY_INIT;
16 * ...
17 * int *i = git_array_alloc(my_ints);
18 * GITERR_CHECK_ALLOC(i);
19 * ...
20 * git_array_clear(my_ints);
21 *
22 * You may also want to do things like:
23 *
24 * typedef git_array_t(my_struct) my_struct_array_t;
25 */
26#define git_array_t(type) struct { type *ptr; uint32_t size, asize; }
27
28#define GIT_ARRAY_INIT { NULL, 0, 0 }
596b121a
RB
29
30#define git_array_init(a) \
31 do { (a).size = (a).asize = 0; (a).ptr = NULL; } while (0)
32
9abc78ae
RB
33#define git_array_init_to_size(a, desired) \
34 do { (a).size = 0; (a).asize = desired; (a).ptr = git__calloc(desired, sizeof(*(a).ptr)); } while (0)
35
596b121a
RB
36#define git_array_clear(a) \
37 do { git__free((a).ptr); git_array_init(a); } while (0)
38
596b121a
RB
39#define GITERR_CHECK_ARRAY(a) GITERR_CHECK_ALLOC((a).ptr)
40
ef3374a8 41
8dd8aa48 42typedef git_array_t(char) git_array_generic_t;
ef3374a8
RB
43
44/* use a generic array for growth so this can return the new item */
8dd8aa48 45GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
ef3374a8 46{
b62a6a13 47 volatile git_array_generic_t *a = _a;
392702ee
ET
48 uint32_t new_size;
49 char *new_array;
50
51 if (a->size < 8) {
52 new_size = 8;
ef3374a8 53 } else {
392702ee
ET
54 if (GIT_ALLOC_OVERFLOW_MULTIPLY(a->size, 3 / 2))
55 goto on_oom;
56
57 new_size = a->size * 3 / 2;
ef3374a8 58 }
392702ee
ET
59
60 if (GIT_ALLOC_OVERFLOW_MULTIPLY(new_size, item_size) ||
61 (new_array = git__realloc(a->ptr, new_size * item_size)) == NULL)
62 goto on_oom;
63
64 a->ptr = new_array; a->asize = new_size; a->size++;
65 return a->ptr + (a->size - 1) * item_size;
66
67on_oom:
68 git_array_clear(*a);
69 return NULL;
ef3374a8
RB
70}
71
72#define git_array_alloc(a) \
73342386 73 (((a).size >= (a).asize) ? \
8dd8aa48 74 git_array_grow(&(a), sizeof(*(a).ptr)) : \
73342386 75 ((a).ptr ? &(a).ptr[(a).size++] : NULL))
596b121a
RB
76
77#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : NULL)
78
fb23d05f
CMN
79#define git_array_pop(a) ((a).size ? &(a).ptr[--(a).size] : NULL)
80
596b121a
RB
81#define git_array_get(a, i) (((i) < (a).size) ? &(a).ptr[(i)] : NULL)
82
83#define git_array_size(a) (a).size
84
2b672d5b
RB
85#define git_array_valid_index(a, i) ((i) < (a).size)
86
596b121a 87#endif