git/commit-slab.h
SZEDER Gábor 7422b2a0a1 commit-slab: clarify slabname##_peek()'s return value
Ever since 862e730ec1 (commit-slab: introduce slabname##_peek()
function, 2015-05-14) the slabname##_peek() function is documented as:

  This function is similar to indegree_at(), but it will return NULL
  until a call to indegree_at() was made for the commit.

This, however, is usually not the case.  If indegree_at() allocates
memory, then it will do so not only for the single commit it got as
parameter, but it will allocate a whole new, ~512kB slab.  Later on,
if any other commit's 'index' field happens to point into an already
allocated slab, then indegree_peek() for such a commit will return a
valid non-NULL pointer, pointing to a zero-initialized location in the
slab, even if no indegree_at() call has been made for that commit yet.

Update slabname##_peek()'s documentation to clarify this.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-03-10 11:44:24 -07:00

57 lines
2.1 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.
*/
#define define_commit_slab(slabname, elemtype) \
declare_commit_slab(slabname, elemtype); \
implement_static_commit_slab(slabname, elemtype)
#endif /* COMMIT_SLAB_H */