mirror of
https://github.com/git/git
synced 2024-10-30 04:01:21 +00:00
b4ff12c8ee
Similar to the preceding commit, let's carry over macros to allocate arrays with `REFTABLE_ALLOC_ARRAY()` and `REFTABLE_CALLOC_ARRAY()`. This requires us to change the signature of `reftable_calloc()`, which only takes a single argument right now and thus puts the burden on the caller to calculate the final array's size. This is a net improvement though as it means that we can now provide proper overflow checks when multiplying the array size with the member size. Convert callsites of `reftable_calloc()` to the new signature and start using the new macros where possible. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
66 lines
1.3 KiB
C
66 lines
1.3 KiB
C
/*
|
|
Copyright 2020 Google LLC
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
license that can be found in the LICENSE file or at
|
|
https://developers.google.com/open-source/licenses/bsd
|
|
*/
|
|
|
|
#include "system.h"
|
|
#include "reftable-malloc.h"
|
|
|
|
#include "basics.h"
|
|
|
|
static void *(*reftable_malloc_ptr)(size_t sz);
|
|
static void *(*reftable_realloc_ptr)(void *, size_t);
|
|
static void (*reftable_free_ptr)(void *);
|
|
|
|
void *reftable_malloc(size_t sz)
|
|
{
|
|
if (reftable_malloc_ptr)
|
|
return (*reftable_malloc_ptr)(sz);
|
|
return malloc(sz);
|
|
}
|
|
|
|
void *reftable_realloc(void *p, size_t sz)
|
|
{
|
|
if (reftable_realloc_ptr)
|
|
return (*reftable_realloc_ptr)(p, sz);
|
|
return realloc(p, sz);
|
|
}
|
|
|
|
void reftable_free(void *p)
|
|
{
|
|
if (reftable_free_ptr)
|
|
reftable_free_ptr(p);
|
|
else
|
|
free(p);
|
|
}
|
|
|
|
void *reftable_calloc(size_t nelem, size_t elsize)
|
|
{
|
|
size_t sz = st_mult(nelem, elsize);
|
|
void *p = reftable_malloc(sz);
|
|
memset(p, 0, sz);
|
|
return p;
|
|
}
|
|
|
|
void reftable_set_alloc(void *(*malloc)(size_t),
|
|
void *(*realloc)(void *, size_t), void (*free)(void *))
|
|
{
|
|
reftable_malloc_ptr = malloc;
|
|
reftable_realloc_ptr = realloc;
|
|
reftable_free_ptr = free;
|
|
}
|
|
|
|
int hash_size(uint32_t id)
|
|
{
|
|
switch (id) {
|
|
case 0:
|
|
case GIT_SHA1_FORMAT_ID:
|
|
return GIT_SHA1_RAWSZ;
|
|
case GIT_SHA256_FORMAT_ID:
|
|
return GIT_SHA256_RAWSZ;
|
|
}
|
|
abort();
|
|
}
|