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

static variables

From
SZStefan Zager <szager@google.com>
Date
Dec 11, 2013, 01:25 UTC
Message-ID
<CAHOQ7J-rO-KjHyYk1Gw6Wv+iH_M7DPr76t3G7YN_sUv3YqcJcg@mail.gmail.com>

This is probably a naive question, but: there are quite a lot of static variables in the git code where it's really unnecessary. Is that just a historical artifact, or is there some reason to prefer them? I'm working on a patch that will introduce threading, so naturally I'm on the lookout for static variables. In general, can I get rid of static variables where it seems straightforward to do so?

As an example, here's an excerpt from symlnks.c. In addition to being static, if I'm reading this right, it appears that the 'removal' variable is used before it's initialized:

static struct removal_def {
  char path[PATH_MAX];
  int len;
} removal;
static void do_remove_scheduled_dirs(int new_len)
{
  while (removal.len > new_len) {
    removal.path[removal.len] = '\0';
    if (rmdir(removal.path))
      break;
    do {
      removal.len--;
    } while (removal.len > new_len &&
       removal.path[removal.len] != '/');
  }
  removal.len = new_len;
}
void schedule_dir_for_removal(const char *name, int len)
{
  int match_len, last_slash, i, previous_slash;
  match_len = last_slash = i =
    longest_path_match(name, len, removal.path, removal.len,
           &previous_slash);
  /* Find last slash inside 'name' */
  while (i < len) {
    if (name[i] == '/')
      last_slash = i;
    i++;
  }
  /*
   * If we are about to go down the directory tree, we check if
   * we must first go upwards the tree, such that we then can
   * remove possible empty directories as we go upwards.
   */
  if (match_len < last_slash && match_len < removal.len)
    do_remove_scheduled_dirs(match_len);
  /*
   * If we go deeper down the directory tree, we only need to
   * save the new path components as we go down.
   */
  if (match_len < last_slash) {
    memcpy(&removal.path[match_len], &name[match_len],
           last_slash - match_len);
    removal.len = last_slash;
  }
}
void remove_scheduled_dirs(void)
{
  do_remove_scheduled_dirs(0);
}
EOF
Thanks,
Stefan
Next: Jonathan Nieder
Message 1 of 3 in “static variables”
  1. Stefan ZagerDec 11, 2013
  2. Jonathan NiederDec 11, 2013
  3. Jonathan NiederDec 11, 2013

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.