From: Stefan Beller Date: Sat, 16 Apr 2016 00:04:11 GMT Subject: Re: [PATCH v4 03/16] index-helper: new daemon for caching index and related stuff Message-ID: In-Reply-To: <1460507589-25525-4-git-send-email-dturner@twopensource.com> > +static int try_shm(struct index_state *istate) > +{ > + void *new_mmap = NULL; > + size_t old_size = istate->mmap_size; > + ssize_t new_size; > + const unsigned char *sha1; > + struct stat st; > + int fd; > + > + if (!is_main_index(istate) || > + old_size <= 20 || > + stat(git_path("index-helper.path"), &st)) > + return -1; > + if (poke_daemon(istate, &st, 0)) > + return -1; > + sha1 = (unsigned char *)istate->mmap + old_size - 20; > + > + fd = open(index_helper_path("git-index-%s", sha1_to_hex(sha1)), > + O_RDONLY); > + if (fd < 0) > + goto fail; > + > + if (fstat(fd, &st)) > + goto fail; > + > + new_size = st.st_size; > + new_mmap = mmap(NULL, new_size, PROT_READ, MAP_SHARED, fd, 0); > + if (new_size <= 20 || > + hashcmp((unsigned char *)istate->mmap + old_size - 20, > + (unsigned char *)new_mmap + new_size - 20)) { > + if (new_mmap) > + munmap(new_mmap, new_size); > + goto fail; coming from here > + } > + > + /* The memory barrier here matches index-helper.c:share_index. */ > + __sync_synchronize(); > + > + munmap(istate->mmap, istate->mmap_size); > + istate->mmap = new_mmap; > + istate->mmap_size = new_size; > + istate->from_shm = 1; > + return 0; > + > +fail: fd may be leaking here? > + poke_daemon(istate, &st, 1); > + return -1; > +} > +