/src/git/reftable/publicbasics.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include "system.h" |
10 | | #include "reftable-malloc.h" |
11 | | |
12 | | #include "basics.h" |
13 | | |
14 | | static void *(*reftable_malloc_ptr)(size_t sz); |
15 | | static void *(*reftable_realloc_ptr)(void *, size_t); |
16 | | static void (*reftable_free_ptr)(void *); |
17 | | |
18 | | void *reftable_malloc(size_t sz) |
19 | 0 | { |
20 | 0 | if (reftable_malloc_ptr) |
21 | 0 | return (*reftable_malloc_ptr)(sz); |
22 | 0 | return malloc(sz); |
23 | 0 | } |
24 | | |
25 | | void *reftable_realloc(void *p, size_t sz) |
26 | 0 | { |
27 | 0 | if (reftable_realloc_ptr) |
28 | 0 | return (*reftable_realloc_ptr)(p, sz); |
29 | 0 | return realloc(p, sz); |
30 | 0 | } |
31 | | |
32 | | void reftable_free(void *p) |
33 | 0 | { |
34 | 0 | if (reftable_free_ptr) |
35 | 0 | reftable_free_ptr(p); |
36 | 0 | else |
37 | 0 | free(p); |
38 | 0 | } |
39 | | |
40 | | void *reftable_calloc(size_t nelem, size_t elsize) |
41 | 0 | { |
42 | 0 | size_t sz = st_mult(nelem, elsize); |
43 | 0 | void *p = reftable_malloc(sz); |
44 | 0 | memset(p, 0, sz); |
45 | 0 | return p; |
46 | 0 | } |
47 | | |
48 | | void reftable_set_alloc(void *(*malloc)(size_t), |
49 | | void *(*realloc)(void *, size_t), void (*free)(void *)) |
50 | 0 | { |
51 | 0 | reftable_malloc_ptr = malloc; |
52 | 0 | reftable_realloc_ptr = realloc; |
53 | 0 | reftable_free_ptr = free; |
54 | 0 | } |
55 | | |
56 | | int hash_size(uint32_t id) |
57 | 0 | { |
58 | 0 | switch (id) { |
59 | 0 | case 0: |
60 | 0 | case GIT_SHA1_FORMAT_ID: |
61 | 0 | return GIT_SHA1_RAWSZ; |
62 | 0 | case GIT_SHA256_FORMAT_ID: |
63 | 0 | return GIT_SHA256_RAWSZ; |
64 | 0 | } |
65 | 0 | abort(); |
66 | 0 | } |