Skip to content

Commit 790aae7

Browse files
committed
odb: rename git_odb_backend_malloc for consistency
The `git_odb_backend_malloc` name is a system function that is provided for custom ODB backends and allows them to allocate memory for an ODB object in the read callback. This is important so that libgit2 can later free the memory used by an ODB object that was read from the custom backend. However, the name _suggests_ that it actually allocates a `git_odb_backend`. It does not; rename it to make it clear that it actually allocates backend _data_.
1 parent 5a6a3c0 commit 790aae7

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

include/git2/sys/odb_backend.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ struct git_odb_backend {
3030

3131
/* read and read_prefix each return to libgit2 a buffer which
3232
* will be freed later. The buffer should be allocated using
33-
* the function git_odb_backend_malloc to ensure that it can
34-
* be safely freed later. */
33+
* the function git_odb_backend_data_alloc to ensure that libgit2
34+
* can safely free it later. */
3535
int GIT_CALLBACK(read)(
3636
void **, size_t *, git_object_t *, git_odb_backend *, const git_oid *);
3737

@@ -117,8 +117,41 @@ GIT_EXTERN(int) git_odb_init_backend(
117117
git_odb_backend *backend,
118118
unsigned int version);
119119

120+
/**
121+
* Allocate data for an ODB object. Custom ODB backends may use this
122+
* to provide data back to the ODB from their read function. This
123+
* memory should not be freed once it is returned to libgit2. If a
124+
* custom ODB uses this function but encounters an error and does not
125+
* return this data to libgit2, then they should use the corresponding
126+
* git_odb_backend_data_free function.
127+
*
128+
* @param backend the ODB backend that is allocating this memory
129+
* @param len the number of bytes to allocate
130+
* @return the allocated buffer on success or NULL if out of memory
131+
*/
132+
GIT_EXTERN(void *) git_odb_backend_data_alloc(git_odb_backend *backend, size_t len);
133+
134+
135+
/*
136+
* Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`.
137+
*/
138+
#ifndef GIT_DEPRECATE_HARD
139+
140+
/**
141+
* Allocate memory for an ODB object from a custom backend. This is
142+
* an alias of `git_odb_backend_data_alloc` and is preserved for
143+
* backward compatibility.
144+
*
145+
* This function is deprecated, but there is no plan to remove this
146+
* function at this time.
147+
*
148+
* @deprecated git_odb_backend_data_alloc
149+
* @see git_odb_backend_data_alloc
150+
*/
120151
GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len);
121152

153+
#endif
154+
122155
GIT_END_DECL
123156

124157
#endif

src/odb.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,12 +1497,17 @@ int git_odb_write_pack(struct git_odb_writepack **out, git_odb *db, git_indexer_
14971497
return error;
14981498
}
14991499

1500-
void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
1500+
void *git_odb_backend_data_alloc(git_odb_backend *backend, size_t len)
15011501
{
15021502
GIT_UNUSED(backend);
15031503
return git__malloc(len);
15041504
}
15051505

1506+
void *git_odb_backend_malloc(git_odb_backend *backend, size_t len)
1507+
{
1508+
return git_odb_backend_data_alloc(backend, len);
1509+
}
1510+
15061511
int git_odb_refresh(struct git_odb *db)
15071512
{
15081513
size_t i;

0 commit comments

Comments
 (0)