git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] commit-slab: clarify slabname##_peek()'s return value

From
Jeff King <peff@peff.net>
Date
Mar 10, 2020, 17:54 UTC
Message-ID
<20200310175446.GB549010@coredump.intra.peff.net>
In-Reply-To
<20200310153049.3482-1-szeder.dev@gmail.com>
On Tue, Mar 10, 2020 at 04:30:49PM +0100, SZEDER Gábor wrote:
Show 15 quoted lines
> 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.

Yeah, I agree the existing documentation is misleading. Your update looks good to me.

I thought at first we might simply be able to say:
  This function is similar to indegree_at(), but it will avoid
  allocating new slab memory (so its result is suitable only for
  reading, not writing).

But I think it's worth mentioning that the caller needs to handle both NULL or a possible zero-initialized value, as your patch does.

I also wondered if we could make life easier for the caller by collapsing these cases. I.e., always returning a zero-initialized value, and never NULL. All of the callers do something like:

  struct blame_origin *get_blame_suspects(struct commit *commit)
  {                               
          struct blame_origin **result;
                  
          result = blame_suspects_peek(&blame_suspects, commit);
  
          return result ? *result : NULL;
  }

all of which could be turned into a single blame_suspects_peek() call if it just consistently returned a zero-initialized value (it's a little confusing in this example because we're storing pointers, so the zero-initialized value is _also_ NULL, but it's a different type).

But that would get a bit awkward, because peek() returns a pointer, not a value (as it should, because the type we're storing may be a compound type, which we generally avoid passing or returning by value). So we'd actually need to return a pointer to a zero-initialized dummy value. Not impossible, but getting a bit odd.

-Peff
Previous: SZEDER GáborNext: Jeff King
Message 2 of 6 in “commit-slab: clarify slabname##_peek()'s return value”
  1. commit-slab: clarify slabname##_peek()'s return valueSZEDER Gábor, Mar 10, 2020
  2. Jeff KingMar 10, 2020
  3. Jeff KingMar 10, 2020
  4. Jeff KingMar 11, 2020
  5. Junio C HamanoMar 10, 2020
  6. Junio C HamanoMar 10, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.