pretty: clear signature check
[git/gitster.git] / reftable / basics.h
blob4c9ef0fe6c5f18e6ec371c2263e03d4af171e7d5
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"
17 #include "reftable-basics.h"
19 /* Bigendian en/decoding of integers */
21 void put_be24(uint8_t *out, uint32_t i);
22 uint32_t get_be24(uint8_t *in);
23 void put_be16(uint8_t *out, uint16_t i);
26 * find smallest index i in [0, sz) at which `f(i) > 0`, assuming that f is
27 * ascending. Return sz if `f(i) == 0` for all indices. The search is aborted
28 * and `sz` is returned in case `f(i) < 0`.
30 * Contrary to bsearch(3), this returns something useful if the argument is not
31 * found.
33 size_t binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
36 * Frees a NULL terminated array of malloced strings. The array itself is also
37 * freed.
39 void free_names(char **a);
42 * Parse a newline separated list of names. `size` is the length of the buffer,
43 * without terminating '\0'. Empty names are discarded. Returns a `NULL`
44 * pointer when allocations fail.
46 char **parse_names(char *buf, int size);
48 /* compares two NULL-terminated arrays of strings. */
49 int names_equal(const char **a, const char **b);
51 /* returns the array size of a NULL-terminated array of strings. */
52 size_t names_length(const char **names);
54 /* Allocation routines; they invoke the functions set through
55 * reftable_set_alloc() */
56 void *reftable_malloc(size_t sz);
57 void *reftable_realloc(void *p, size_t sz);
58 void reftable_free(void *p);
59 void *reftable_calloc(size_t nelem, size_t elsize);
60 char *reftable_strdup(const char *str);
62 #define REFTABLE_ALLOC_ARRAY(x, alloc) (x) = reftable_malloc(st_mult(sizeof(*(x)), (alloc)))
63 #define REFTABLE_CALLOC_ARRAY(x, alloc) (x) = reftable_calloc((alloc), sizeof(*(x)))
64 #define REFTABLE_REALLOC_ARRAY(x, alloc) (x) = reftable_realloc((x), st_mult(sizeof(*(x)), (alloc)))
65 #define REFTABLE_ALLOC_GROW(x, nr, alloc) \
66 do { \
67 if ((nr) > alloc) { \
68 alloc = 2 * (alloc) + 1; \
69 if (alloc < (nr)) \
70 alloc = (nr); \
71 REFTABLE_REALLOC_ARRAY(x, alloc); \
72 } \
73 } while (0)
74 #define REFTABLE_FREE_AND_NULL(p) do { reftable_free(p); (p) = NULL; } while (0)
76 #ifndef REFTABLE_ALLOW_BANNED_ALLOCATORS
77 # define REFTABLE_BANNED(func) use_reftable_##func##_instead
78 # undef malloc
79 # define malloc(sz) REFTABLE_BANNED(malloc)
80 # undef realloc
81 # define realloc(ptr, sz) REFTABLE_BANNED(realloc)
82 # undef free
83 # define free(ptr) REFTABLE_BANNED(free)
84 # undef calloc
85 # define calloc(nelem, elsize) REFTABLE_BANNED(calloc)
86 # undef strdup
87 # define strdup(str) REFTABLE_BANNED(strdup)
88 #endif
90 /* Find the longest shared prefix size of `a` and `b` */
91 struct strbuf;
92 int common_prefix_size(struct strbuf *a, struct strbuf *b);
94 int hash_size(uint32_t id);
96 #endif