mmap-cache.c revision fcde238921b857679363a95488a5a5af1dc1f243
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "mmap-cache.h"
#define DEFAULT_WINDOWS_MAX 64
#define DEFAULT_FDS_MAX 32
#define DEFAULT_CONTEXTS_MAX 32
typedef struct Window {
int fd;
void *ptr;
unsigned n_ref;
unsigned lru_prev;
unsigned lru_next;
unsigned by_fd_prev;
unsigned by_fd_next;
} Window;
typedef struct FileDescriptor {
int fd;
unsigned windows;
struct MMapCache {
unsigned n_ref;
unsigned contexts_max;
unsigned windows_max;
unsigned fds_max;
unsigned n_windows;
unsigned n_fds;
unsigned *by_context;
};
static void mmap_cache_window_unmap(MMapCache *m, unsigned w) {
Window *v;
assert(m);
v = m->windows + w;
if (!v->ptr)
return;
}
static void mmap_cache_window_add_lru(MMapCache *m, unsigned w) {
Window *v;
assert(m);
v = m->windows + w;
if (m->lru_last != (unsigned) -1) {
}
v->lru_next = (unsigned) -1;
m->lru_last = w;
if (m->lru_first == (unsigned) -1)
m->lru_first = w;
}
static void mmap_cache_window_remove_lru(MMapCache *m, unsigned w) {
Window *v;
assert(m);
v = m->windows + w;
if (v->lru_prev == (unsigned) -1) {
} else {
}
if (v->lru_next == (unsigned) -1) {
} else {
}
}
Window *v;
assert(m);
v = m->windows + w;
}
v->by_fd_prev = (unsigned) -1;
}
Window *v;
assert(m);
v = m->windows + w;
if (v->by_fd_prev == (unsigned) -1) {
} else {
}
if (v->by_fd_next != (unsigned) -1) {
}
}
static void mmap_cache_context_unset(MMapCache *m, unsigned c) {
Window *v;
unsigned w;
assert(m);
assert(c < m->contexts_max);
if (m->by_context[c] == (unsigned) -1)
return;
w = m->by_context[c];
m->by_context[c] = (unsigned) -1;
v = m->windows + w;
v->n_ref --;
if (v->n_ref == 0)
mmap_cache_window_add_lru(m, w);
}
static void mmap_cache_context_set(MMapCache *m, unsigned c, unsigned w) {
Window *v;
assert(m);
assert(c < m->contexts_max);
if (m->by_context[c] == w)
return;
mmap_cache_context_unset(m, c);
m->by_context[c] = w;
v = m->windows + w;
v->n_ref ++;
if (v->n_ref == 1)
mmap_cache_window_remove_lru(m, w);
}
static void mmap_cache_free(MMapCache *m) {
assert(m);
if (m->windows) {
unsigned w;
for (w = 0; w < m->n_windows; w++)
mmap_cache_window_unmap(m, w);
}
free(m->by_context);
free(m);
}
MMapCache* mmap_cache_new(void) {
MMapCache *m;
if (!m)
return NULL;
m->fds_max = DEFAULT_FDS_MAX;
m->n_ref = 1;
m->lru_first = (unsigned) -1;
m->lru_last = (unsigned) -1;
if (!m->windows) {
mmap_cache_free(m);
return NULL;
}
if (!m->by_context) {
mmap_cache_free(m);
return NULL;
}
if (!m->by_fd) {
mmap_cache_free(m);
return NULL;
}
return m;
}
assert(m);
m->n_ref++;
return m;
}
assert(m);
if (m->n_ref == 1)
mmap_cache_free(m);
else
m->n_ref--;
return NULL;
}
static int mmap_cache_allocate_window(MMapCache *m, unsigned *w) {
Window *v;
unsigned fd_index;
assert(m);
assert(w);
if (m->n_windows < m->windows_max) {
*w = m->n_windows ++;
return 0;
}
if (m->lru_first == (unsigned) -1)
return -E2BIG;
*w = m->lru_first;
v = m->windows + *w;
mmap_cache_window_unmap(m, *w);
if (v->fd >= 0) {
mmap_cache_fd_remove(m, fd_index, *w);
}
mmap_cache_window_remove_lru(m, *w);
return 0;
}
static int mmap_cache_make_room(MMapCache *m) {
unsigned w;
assert(m);
w = m->lru_first;
while (w != (unsigned) -1) {
Window *v;
v = m->windows + w;
if (v->ptr) {
mmap_cache_window_unmap(m, w);
return 1;
}
w = v->lru_next;
}
return 0;
}
static int mmap_cache_put(
MMapCache *m,
int fd,
unsigned fd_index,
int prot,
unsigned context,
bool keep_always,
void **ret) {
unsigned w;
Window *v;
void *d;
int r;
assert(m);
if (wsize < WINDOW_SIZE) {
woffset = 0;
else
wsize = WINDOW_SIZE;
}
if (st) {
/* Memory maps that are larger then the files
underneath have undefined behaviour. Hence, clamp
things to the file size if we know it */
return -EADDRNOTAVAIL;
}
for (;;) {
if (d != MAP_FAILED)
break;
return -errno;
r = mmap_cache_make_room(m);
if (r < 0)
return r;
if (r == 0)
return -ENOMEM;
}
r = mmap_cache_allocate_window(m, &w);
if (r < 0) {
return r;
}
v = m->windows + w;
v->ptr = d;
if (keep_always)
v->n_ref = 1;
else {
v->n_ref = 0;
mmap_cache_window_add_lru(m, w);
}
mmap_cache_fd_add(m, fd_index, w);
mmap_cache_context_set(m, context, w);
return 1;
}
return -1;
return 1;
return 0;
}
FileDescriptor *j;
unsigned r;
assert(m);
for (r = 0; r < m->n_fds; r++)
if (!j)
return 0;
return 1;
}
FileDescriptor *j;
int r;
assert(m);
if (r != 0)
return r;
unsigned k;
FileDescriptor *n;
k = m->n_fds * 2;
if (!n)
return -ENOMEM;
m->fds_max = k;
m->by_fd = n;
}
j->windows = (unsigned) -1;
}
static bool mmap_cache_test_window(
MMapCache *m,
unsigned w,
Window *v;
assert(m);
v = m->windows + w;
}
static int mmap_cache_current(
MMapCache *m,
int fd,
unsigned context,
void **ret) {
Window *v;
unsigned w;
assert(m);
return 0;
w = m->by_context[context];
v = m->windows + w;
return 0;
return 0;
return 1;
}
static int mmap_cache_find(
MMapCache *m,
int fd,
unsigned fd_index,
unsigned context,
void **ret) {
unsigned w;
assert(m);
while (w != (unsigned) -1) {
v = m->windows + w;
break;
w = v->by_fd_next;
}
if (w == (unsigned) -1)
return 0;
mmap_cache_context_set(m, context, w);
return 1;
}
int mmap_cache_get(
MMapCache *m,
int fd,
int prot,
unsigned context,
bool keep_always,
void **ret) {
unsigned fd_index;
int r;
assert(m);
if (context >= m->contexts_max) {
unsigned k, *n;
Window *w;
/* Increase the number of contexts if necessary, and
* make sure we have twice the number of windows */
k = context * 2;
n = realloc(m->by_context, sizeof(unsigned) * k);
if (!n)
return -ENOMEM;
m->contexts_max = k;
m->by_context = n;
if (!w)
return -ENOMEM;
m->windows_max = k;
m->windows = w;
}
/* Maybe the current pointer for this context is already the
* right one? */
if (r != 0)
return r;
/* Hmm, drop the reference to the current one, since it wasn't
* good enough */
/* OK, let's find the chain for this FD */
if (r < 0)
return r;
/* And let's look through the available mmaps */
if (r != 0)
return r;
/* Not found? Then, let's add it */
}
unsigned fd_index, c, w;
int r;
assert(m);
if (r <= 0)
return;
for (c = 0; c < m->contexts_max; c++) {
w = m->by_context[c];
if (w == (unsigned) -1)
continue;
mmap_cache_context_unset(m, c);
}
while (w != (unsigned) -1) {
Window *v;
v = m->windows + w;
mmap_cache_window_unmap(m, w);
mmap_cache_fd_remove(m, fd_index, w);
v->fd = -1;
}
memmove(m->by_fd + fd_index, m->by_fd + fd_index + 1, (m->n_fds - (fd_index + 1)) * sizeof(FileDescriptor));
m->n_fds --;
}
}