- r = mmap_cache_allocate_window(m, &w);
- if (r < 0) {
- munmap(d, wsize);
- return r;
- }
-
- v = m->windows + w;
- v->fd = fd;
- v->ptr = d;
- v->offset = woffset;
- v->size = wsize;
-
- v->n_ref = 0;
- v->lru_prev = v->lru_next = (unsigned) -1;
-
- mmap_cache_fd_add(m, fd_index, w);
- mmap_cache_context_set(m, context, w);
-
- *ret = (uint8_t*) d + (offset - woffset);
- return 1;
-}
-
-static int fd_cmp(const void *_a, const void *_b) {
- const FileDescriptor *a = _a, *b = _b;
-
- if (a->fd < b->fd)
- return -1;
- if (a->fd > b->fd)
- return 1;
-
- return 0;
-}
-
-static int mmap_cache_get_fd_index(MMapCache *m, int fd, unsigned *fd_index) {
- FileDescriptor *j;
-
- assert(m);
- assert(fd >= 0);
- assert(fd_index);
-
- j = bsearch(&fd, m->by_fd, m->n_fds, sizeof(m->by_fd[0]), fd_cmp);
- if (!j) {
- if (m->n_fds >= m->fds_max)
- return -E2BIG;