Skip to content

Commit 7b083d3

Browse files
authored
Merge pull request libgit2#5005 from libgit2/ethomson/odb_backend_allocations
odb: provide a free function for custom backends
2 parents c7e5eca + 459ac85 commit 7b083d3

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

include/git2/sys/odb_backend.h

Lines changed: 46 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,52 @@ 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+
* Frees custom allocated ODB data. This should only be called when
136+
* memory allocated using git_odb_backend_data_alloc is not returned
137+
* to libgit2 because the backend encountered an error in the read
138+
* function after allocation and did not return this data to libgit2.
139+
*
140+
* @param backend the ODB backend that is freeing this memory
141+
* @param data the buffer to free
142+
*/
143+
GIT_EXTERN(void) git_odb_backend_data_free(git_odb_backend *backend, void *data);
144+
145+
146+
/*
147+
* Users can avoid deprecated functions by defining `GIT_DEPRECATE_HARD`.
148+
*/
149+
#ifndef GIT_DEPRECATE_HARD
150+
151+
/**
152+
* Allocate memory for an ODB object from a custom backend. This is
153+
* an alias of `git_odb_backend_data_alloc` and is preserved for
154+
* backward compatibility.
155+
*
156+
* This function is deprecated, but there is no plan to remove this
157+
* function at this time.
158+
*
159+
* @deprecated git_odb_backend_data_alloc
160+
* @see git_odb_backend_data_alloc
161+
*/
120162
GIT_EXTERN(void *) git_odb_backend_malloc(git_odb_backend *backend, size_t len);
121163

164+
#endif
165+
122166
GIT_END_DECL
123167

124168
#endif

src/odb.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,12 +1497,23 @@ 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+
1511+
void git_odb_backend_data_free(git_odb_backend *backend, void *data)
1512+
{
1513+
GIT_UNUSED(backend);
1514+
git__free(data);
1515+
}
1516+
15061517
int git_odb_refresh(struct git_odb *db)
15071518
{
15081519
size_t i;

0 commit comments

Comments
 (0)