]>
Commit | Line | Data |
---|---|---|
ef8a6c62 HWN |
1 | /* |
2 | Copyright 2020 Google LLC | |
3 | ||
4 | Use of this source code is governed by a BSD-style | |
5 | license that can be found in the LICENSE file or at | |
6 | https://developers.google.com/open-source/licenses/bsd | |
7 | */ | |
8 | ||
9 | #ifndef BASICS_H | |
10 | #define BASICS_H | |
11 | ||
12 | /* | |
13 | * miscellaneous utilities that are not provided by Git. | |
14 | */ | |
15 | ||
16 | #include "system.h" | |
17 | ||
18 | /* Bigendian en/decoding of integers */ | |
19 | ||
20 | void put_be24(uint8_t *out, uint32_t i); | |
21 | uint32_t get_be24(uint8_t *in); | |
22 | void put_be16(uint8_t *out, uint16_t i); | |
23 | ||
24 | /* | |
f9e88544 PS |
25 | * find smallest index i in [0, sz) at which `f(i) > 0`, assuming that f is |
26 | * ascending. Return sz if `f(i) == 0` for all indices. The search is aborted | |
27 | * and `sz` is returned in case `f(i) < 0`. | |
ef8a6c62 HWN |
28 | * |
29 | * Contrary to bsearch(3), this returns something useful if the argument is not | |
30 | * found. | |
31 | */ | |
3e7b36d1 | 32 | size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args); |
ef8a6c62 HWN |
33 | |
34 | /* | |
35 | * Frees a NULL terminated array of malloced strings. The array itself is also | |
36 | * freed. | |
37 | */ | |
38 | void free_names(char **a); | |
39 | ||
40 | /* parse a newline separated list of names. `size` is the length of the buffer, | |
41 | * without terminating '\0'. Empty names are discarded. */ | |
42 | void parse_names(char *buf, int size, char ***namesp); | |
43 | ||
44 | /* compares two NULL-terminated arrays of strings. */ | |
45 | int names_equal(char **a, char **b); | |
46 | ||
47 | /* returns the array size of a NULL-terminated array of strings. */ | |
81879123 | 48 | size_t names_length(char **names); |
ef8a6c62 HWN |
49 | |
50 | /* Allocation routines; they invoke the functions set through | |
51 | * reftable_set_alloc() */ | |
52 | void *reftable_malloc(size_t sz); | |
53 | void *reftable_realloc(void *p, size_t sz); | |
54 | void reftable_free(void *p); | |
b4ff12c8 | 55 | void *reftable_calloc(size_t nelem, size_t elsize); |
ef8a6c62 | 56 | |
b4ff12c8 PS |
57 | #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc))) |
58 | #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x))) | |
f6b58c1b PS |
59 | #define REFTABLE_REALLOC_ARRAY(x, alloc) (x) = reftable_realloc((x), st_mult(sizeof(*(x)), (alloc))) |
60 | #define REFTABLE_ALLOC_GROW(x, nr, alloc) \ | |
61 | do { \ | |
62 | if ((nr) > alloc) { \ | |
63 | alloc = 2 * (alloc) + 1; \ | |
64 | if (alloc < (nr)) \ | |
65 | alloc = (nr); \ | |
66 | REFTABLE_REALLOC_ARRAY(x, alloc); \ | |
67 | } \ | |
68 | } while (0) | |
69 | ||
ef8a6c62 HWN |
70 | /* Find the longest shared prefix size of `a` and `b` */ |
71 | struct strbuf; | |
72 | int common_prefix_size(struct strbuf *a, struct strbuf *b); | |
73 | ||
74 | #endif |