-static int mmap_cache_get_fd_index(MMapCache *m, int fd, unsigned *fd_index) {
- FileDescriptor *j;
- int r;
-
- assert(m);
- assert(fd >= 0);
- assert(fd_index);
-
- r = mmap_cache_peek_fd_index(m, fd, fd_index);
- if (r != 0)
- return r;
-
- if (m->n_fds >= m->fds_max) {
- unsigned k;
- FileDescriptor *n;
-
- k = m->n_fds * 2;
- n = realloc(m->by_fd, sizeof(FileDescriptor) * k);
- if (!n)
- return -ENOMEM;
-
- m->fds_max = k;
- m->by_fd = n;
- }
-
- j = m->by_fd + m->n_fds ++;
- j->fd = fd;
- j->windows = (unsigned) -1;
-
- qsort(m->by_fd, m->n_fds, sizeof(FileDescriptor), fd_cmp);
-
- return mmap_cache_peek_fd_index(m, fd, fd_index);
-}
-
-static bool mmap_cache_test_window(
- MMapCache *m,
- unsigned w,
- uint64_t offset,
- uint64_t size) {
- Window *v;
-
- assert(m);
- assert(w < m->n_windows);
- assert(size > 0);
-
- v = m->windows + w;
-
- return offset >= v->offset &&
- offset + size <= v->offset + v->size;
-}
-
-static int mmap_cache_current(
- MMapCache *m,
- int fd,
- unsigned context,
- uint64_t offset,
- uint64_t size,
- void **ret) {
-
- Window *v;
- unsigned w;
-
- assert(m);
- assert(fd >= 0);
- assert(context < m->contexts_max);
- assert(size > 0);
- assert(ret);
-
- if (m->by_context[context] == (unsigned) -1)
- return 0;
-
- w = m->by_context[context];
- v = m->windows + w;
-
- if (v->fd != fd)
- return 0;
-
- if (!mmap_cache_test_window(m, w, offset, size))
- return 0;
-
- *ret = (uint8_t*) v->ptr + (offset - v->offset);
- return 1;
-}
-
-static int mmap_cache_find(