1
0
mirror of https://github.com/git/git.git synced 2024-11-18 22:04:14 +01:00
git/commit-slab.h
SZEDER Gábor 1df15f8dee commit-slab: add a function to deep free entries on the slab
clear_##slabname() frees only the memory allocated for a commit slab
itself, but entries in the commit slab might own additional memory
outside the slab that should be freed as well.  We already have (at
least) one such commit slab, and this patch series is about to add one
more.

To free all additional memory owned by entries on the commit slab the
user of such a slab could iterate over all commits it knows about,
peek whether there is a valid entry associated with each commit, and
free the additional memory, if any.  Or it could rely on intimate
knowledge about the internals of the commit slab implementation, and
could itself iterate directly through all entries in the slab, and
free the additional memory.  Or it could just leak the additional
memory...

Introduce deep_clear_##slabname() to allow releasing memory owned by
commit slab entries by invoking the 'void free_fn(elemtype *ptr)'
function specified as parameter for each entry in the slab.

Use it in get_shallow_commits() in 'shallow.c' to replace an
open-coded iteration over a commit slab's entries.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-06-08 12:28:49 -07:00

67 lines
2.5 KiB
C

#ifndef COMMIT_SLAB_H
#define COMMIT_SLAB_H
#include "commit-slab-decl.h"
#include "commit-slab-impl.h"
/*
* define_commit_slab(slabname, elemtype) creates boilerplate code to define
* a new struct (struct slabname) that is used to associate a piece of data
* of elemtype to commits, and a few functions to use that struct.
*
* After including this header file, using:
*
* define_commit_slab(indegree, int);
*
* will let you call the following functions:
*
* - int *indegree_at(struct indegree *, struct commit *);
*
* This function locates the data associated with the given commit in
* the indegree slab, and returns the pointer to it. The location to
* store the data is allocated as necessary.
*
* - int *indegree_peek(struct indegree *, struct commit *);
*
* This function is similar to indegree_at(), but it will return NULL
* if the location to store the data associated with the given commit
* has not been allocated yet.
* Note that the location to store the data might have already been
* allocated even if no indegree_at() call has been made for that commit
* yet; in this case this function returns a pointer to a
* zero-initialized location.
*
* - void init_indegree(struct indegree *);
* void init_indegree_with_stride(struct indegree *, int);
*
* Initializes the indegree slab that associates an array of integers
* to each commit. 'stride' specifies how big each array is. The slab
* that is initialized by the variant without "_with_stride" associates
* each commit with an array of one integer.
*
* - void clear_indegree(struct indegree *);
*
* Empties the slab. The slab can be reused with the same stride
* without calling init_indegree() again or can be reconfigured to a
* different stride by calling init_indegree_with_stride().
*
* Call this function before the slab falls out of scope to avoid
* leaking memory.
*
* - void deep_clear_indegree(struct indegree *, void (*free_fn)(int*))
*
* Empties the slab, similar to clear_indegree(), but in addition it
* calls the given 'free_fn' for each slab entry to release any
* additional memory that might be owned by the entry (but not the
* entry itself!).
* Note that 'free_fn' might be called even for entries for which no
* indegree_at() call has been made; in this case 'free_fn' is invoked
* with a pointer to a zero-initialized location.
*/
#define define_commit_slab(slabname, elemtype) \
declare_commit_slab(slabname, elemtype); \
implement_static_commit_slab(slabname, elemtype)
#endif /* COMMIT_SLAB_H */