win32: fix building with NO_UNIX_SOCKETS
[alt-git.git] / reftable / basics.h
blob91f3533efee501faf3e461f7206dbc7a964da98e
1 /*
2 Copyright 2020 Google LLC
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 */
9 #ifndef BASICS_H
10 #define BASICS_H
13 * miscellaneous utilities that are not provided by Git.
16 #include "system.h"
18 /* Bigendian en/decoding of integers */
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);
25 * find smallest index i in [0, sz) at which f(i) is true, assuming
26 * that f is ascending. Return sz if f(i) is false for all indices.
28 * Contrary to bsearch(3), this returns something useful if the argument is not
29 * found.
31 int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
34 * Frees a NULL terminated array of malloced strings. The array itself is also
35 * freed.
37 void free_names(char **a);
39 /* parse a newline separated list of names. `size` is the length of the buffer,
40 * without terminating '\0'. Empty names are discarded. */
41 void parse_names(char *buf, int size, char ***namesp);
43 /* compares two NULL-terminated arrays of strings. */
44 int names_equal(char **a, char **b);
46 /* returns the array size of a NULL-terminated array of strings. */
47 size_t names_length(char **names);
49 /* Allocation routines; they invoke the functions set through
50 * reftable_set_alloc() */
51 void *reftable_malloc(size_t sz);
52 void *reftable_realloc(void *p, size_t sz);
53 void reftable_free(void *p);
54 void *reftable_calloc(size_t nelem, size_t elsize);
56 #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))
57 #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))
58 #define REFTABLE_REALLOC_ARRAY(x, alloc) (x) = reftable_realloc((x), st_mult(sizeof(*(x)), (alloc)))
59 #define REFTABLE_ALLOC_GROW(x, nr, alloc) \
60 do { \
61 if ((nr) > alloc) { \
62 alloc = 2 * (alloc) + 1; \
63 if (alloc < (nr)) \
64 alloc = (nr); \
65 REFTABLE_REALLOC_ARRAY(x, alloc); \
66 } \
67 } while (0)
69 /* Find the longest shared prefix size of `a` and `b` */
70 struct strbuf;
71 int common_prefix_size(struct strbuf *a, struct strbuf *b);
73 #endif