color: add support for 12-bit RGB colors
[alt-git.git] / reftable / basics.h
blob523ecd530762f6e4cde48edfa3761b3139b21b92
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) > 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`.
29 * Contrary to bsearch(3), this returns something useful if the argument is not
30 * found.
32 size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
35 * Frees a NULL terminated array of malloced strings. The array itself is also
36 * freed.
38 void free_names(char **a);
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);
44 /* compares two NULL-terminated arrays of strings. */
45 int names_equal(char **a, char **b);
47 /* returns the array size of a NULL-terminated array of strings. */
48 size_t names_length(char **names);
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);
55 void *reftable_calloc(size_t nelem, size_t elsize);
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)))
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)
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);
74 #endif