odb_mempack: use an out param

This commit is contained in:
Edward Thomson
2024-12-09 22:36:10 +00:00
parent b190162f3e
commit 4bb69b0827
3 changed files with 22 additions and 7 deletions

View File

@@ -104,10 +104,11 @@ GIT_EXTERN(int) git_mempack_reset(git_odb_backend *backend);
/** /**
* Get the total number of objects in mempack * Get the total number of objects in mempack
* *
* @param count The count of objects in the mempack
* @param backend The mempack backend * @param backend The mempack backend
* @return the number of objects in the mempack, -1 on error * @return 0 on success, or -1 on error
*/ */
GIT_EXTERN(int) git_mempack_object_count(git_odb_backend *backend); GIT_EXTERN(int) git_mempack_object_count(size_t *count, git_odb_backend *backend);
GIT_END_DECL GIT_END_DECL

View File

@@ -210,9 +210,18 @@ int git_mempack_new(git_odb_backend **out)
return 0; return 0;
} }
int git_mempack_object_count(git_odb_backend *_backend) int git_mempack_object_count(size_t *out, git_odb_backend *_backend)
{ {
struct memory_packer_db *db = (struct memory_packer_db *)_backend; struct memory_packer_db *db = (struct memory_packer_db *)_backend;
uint32_t count;
GIT_ASSERT_ARG(_backend); GIT_ASSERT_ARG(_backend);
return git_odb_mempack_oidmap_size(&db->objects);
count = git_odb_mempack_oidmap_size(&db->objects);
if (count < 0)
return count;
*out = (size_t)count;
return 0;
} }

View File

@@ -61,17 +61,22 @@ void test_odb_backend_mempack__blob_create_from_buffer_succeeds(void)
void test_odb_backend_mempack__empty_object_count_succeeds(void) void test_odb_backend_mempack__empty_object_count_succeeds(void)
{ {
cl_assert_equal_sz(0, git_mempack_object_count(_backend)); size_t count;
cl_git_pass(git_mempack_object_count(&count, _backend));
cl_assert_equal_sz(0, count);
} }
void test_odb_backend_mempack__object_count_succeeds(void) void test_odb_backend_mempack__object_count_succeeds(void)
{ {
const char *data = "data"; const char *data = "data";
size_t count;
cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJECT_BLOB)); cl_git_pass(git_odb_write(&_oid, _odb, data, strlen(data) + 1, GIT_OBJECT_BLOB));
cl_assert_equal_sz(1, git_mempack_object_count(_backend)); cl_git_pass(git_mempack_object_count(&count, _backend));
cl_assert_equal_sz(1, count);
} }
void test_odb_backend_mempack__object_count_fails(void) void test_odb_backend_mempack__object_count_fails(void)
{ {
cl_git_fail_with(-1, git_mempack_object_count(0)); size_t count;
cl_git_fail_with(-1, git_mempack_object_count(&count, 0));
} }