reftable/basics: merge "publicbasics" into "basics"

The split between "basics" and "publicbasics" is somewhat arbitrary and not in line with how we typically structure code in the reftable library. While we do indeed split up headers into a public and internal part, we don't do that for the compilation unit itself. Furthermore, the declarations for "publicbasics.c" are in "reftable-malloc.h", which isn't in line with our naming schema, either. Fix these inconsistencies by: - Merging "publicbasics.c" into "basics.c". - Renaming "reftable-malloc.h" to "reftable-basics.h" as the public header. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Oct 2, 2024 at 12:55 UTC a5a15a4514f45c4ba8e901675951bfe551d77fae
6 files changed +76 -85
Makefile
-1
@@ -2683,7 +2683,6 @@ REFTABLE_OBJS += reftable/error.o
2683 REFTABLE_OBJS += reftable/block.o
2684 REFTABLE_OBJS += reftable/blocksource.o
2685 REFTABLE_OBJS += reftable/iter.o
2686 -REFTABLE_OBJS += reftable/publicbasics.o
2686 REFTABLE_OBJS += reftable/merged.o
2687 REFTABLE_OBJS += reftable/pq.o
2688 REFTABLE_OBJS += reftable/reader.o
reftable/basics.c
+55
@@ -7,6 +7,49 @@ https://developers.google.com/open-source/licenses/bsd
7 */
8
9 #include "basics.h"
10 +#include "reftable-basics.h"
11 +
12 +static void *(*reftable_malloc_ptr)(size_t sz);
13 +static void *(*reftable_realloc_ptr)(void *, size_t);
14 +static void (*reftable_free_ptr)(void *);
15 +
16 +void *reftable_malloc(size_t sz)
17 +{
18 + if (reftable_malloc_ptr)
19 + return (*reftable_malloc_ptr)(sz);
20 + return malloc(sz);
21 +}
22 +
23 +void *reftable_realloc(void *p, size_t sz)
24 +{
25 + if (reftable_realloc_ptr)
26 + return (*reftable_realloc_ptr)(p, sz);
27 + return realloc(p, sz);
28 +}
29 +
30 +void reftable_free(void *p)
31 +{
32 + if (reftable_free_ptr)
33 + reftable_free_ptr(p);
34 + else
35 + free(p);
36 +}
37 +
38 +void *reftable_calloc(size_t nelem, size_t elsize)
39 +{
40 + size_t sz = st_mult(nelem, elsize);
41 + void *p = reftable_malloc(sz);
42 + memset(p, 0, sz);
43 + return p;
44 +}
45 +
46 +void reftable_set_alloc(void *(*malloc)(size_t),
47 + void *(*realloc)(void *, size_t), void (*free)(void *))
48 +{
49 + reftable_malloc_ptr = malloc;
50 + reftable_realloc_ptr = realloc;
51 + reftable_free_ptr = free;
52 +}
53
54 void put_be24(uint8_t *out, uint32_t i)
55 {
@@ -121,3 +164,15 @@ int common_prefix_size(struct strbuf *a, struct strbuf *b)
164
165 return p;
166 }
167 +
168 +int hash_size(uint32_t id)
169 +{
170 + switch (id) {
171 + case 0:
172 + case GIT_SHA1_FORMAT_ID:
173 + return GIT_SHA1_RAWSZ;
174 + case GIT_SHA256_FORMAT_ID:
175 + return GIT_SHA256_RAWSZ;
176 + }
177 + abort();
178 +}
reftable/basics.h
+3
@@ -14,6 +14,7 @@ https://developers.google.com/open-source/licenses/bsd
14 */
15
16 #include "system.h"
17 +#include "reftable-basics.h"
18
19 /* Bigendian en/decoding of integers */
20
@@ -71,4 +72,6 @@ void *reftable_calloc(size_t nelem, size_t elsize);
72 struct strbuf;
73 int common_prefix_size(struct strbuf *a, struct strbuf *b);
74
75 +int hash_size(uint32_t id);
76 +
77 #endif
reftable/publicbasics.c deleted
-66
@@ -1,66 +0,0 @@
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 -{
20 - if (reftable_malloc_ptr)
21 - return (*reftable_malloc_ptr)(sz);
22 - return malloc(sz);
23 -}
24 -
25 -void *reftable_realloc(void *p, size_t sz)
26 -{
27 - if (reftable_realloc_ptr)
28 - return (*reftable_realloc_ptr)(p, sz);
29 - return realloc(p, sz);
30 -}
31 -
32 -void reftable_free(void *p)
33 -{
34 - if (reftable_free_ptr)
35 - reftable_free_ptr(p);
36 - else
37 - free(p);
38 -}
39 -
40 -void *reftable_calloc(size_t nelem, size_t elsize)
41 -{
42 - size_t sz = st_mult(nelem, elsize);
43 - void *p = reftable_malloc(sz);
44 - memset(p, 0, sz);
45 - return p;
46 -}
47 -
48 -void reftable_set_alloc(void *(*malloc)(size_t),
49 - void *(*realloc)(void *, size_t), void (*free)(void *))
50 -{
51 - reftable_malloc_ptr = malloc;
52 - reftable_realloc_ptr = realloc;
53 - reftable_free_ptr = free;
54 -}
55 -
56 -int hash_size(uint32_t id)
57 -{
58 - switch (id) {
59 - case 0:
60 - case GIT_SHA1_FORMAT_ID:
61 - return GIT_SHA1_RAWSZ;
62 - case GIT_SHA256_FORMAT_ID:
63 - return GIT_SHA256_RAWSZ;
64 - }
65 - abort();
66 -}
reftable/reftable-basics.h new
+18
@@ -0,0 +1,18 @@
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 REFTABLE_BASICS_H
10 +#define REFTABLE_BASICS_H
11 +
12 +#include <stddef.h>
13 +
14 +/* Overrides the functions to use for memory management. */
15 +void reftable_set_alloc(void *(*malloc)(size_t),
16 + void *(*realloc)(void *, size_t), void (*free)(void *));
17 +
18 +#endif
reftable/reftable-malloc.h deleted
-18
@@ -1,18 +0,0 @@
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 REFTABLE_H
10 -#define REFTABLE_H
11 -
12 -#include <stddef.h>
13 -
14 -/* Overrides the functions to use for memory management. */
15 -void reftable_set_alloc(void *(*malloc)(size_t),
16 - void *(*realloc)(void *, size_t), void (*free)(void *));
17 -
18 -#endif