2008-11-07 15:36:18 +01:00
|
|
|
/*
|
|
|
|
|
mmapings.c - manage ranges of mmaped areas on a filedescriptor
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Christian Thaeter <ct@pipapo.org>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-01-25 00:24:42 +01:00
|
|
|
#include "include/logging.h"
|
2008-11-07 15:36:18 +01:00
|
|
|
#include "lib/mutex.h"
|
|
|
|
|
#include "lib/safeclib.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "backend/mmapings.h"
|
|
|
|
|
#include "backend/mmapcache.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LumieraMMapings
|
|
|
|
|
lumiera_mmapings_init (LumieraMMapings self, LumieraFile file, size_t chunksize)
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapings_dbg);
|
2008-11-07 15:36:18 +01:00
|
|
|
REQUIRE (!file->descriptor->mmapings);
|
|
|
|
|
|
|
|
|
|
llist_init (&self->mmaps);
|
|
|
|
|
self->descriptor = file->descriptor;
|
|
|
|
|
self->chunksize = chunksize;
|
|
|
|
|
|
2009-01-24 03:12:32 +01:00
|
|
|
lumiera_mutex_init (&self->lock, "mmapings", &NOBUG_FLAG(mutex_dbg));
|
2008-11-07 15:36:18 +01:00
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LumieraMMapings
|
|
|
|
|
lumiera_mmapings_destroy (LumieraMMapings self)
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapings_dbg);
|
2008-11-07 15:36:18 +01:00
|
|
|
if (!self)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
LLIST_WHILE_TAIL (&self->mmaps, node)
|
|
|
|
|
{
|
|
|
|
|
LumieraMMap mmap = LLIST_TO_STRUCTP (node, lumiera_mmap, searchnode);
|
|
|
|
|
lumiera_mmap_delete (mmap);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-24 03:12:32 +01:00
|
|
|
lumiera_mutex_destroy (&self->lock, &NOBUG_FLAG(mutex_dbg));
|
2008-11-07 15:36:18 +01:00
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LumieraMMapings
|
|
|
|
|
lumiera_mmapings_new (LumieraFile file, size_t chunksize)
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapings_dbg);
|
2008-11-07 15:36:18 +01:00
|
|
|
LumieraMMapings self = lumiera_malloc (sizeof (*self));
|
|
|
|
|
return lumiera_mmapings_init (self, file, chunksize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
lumiera_mmapings_delete (LumieraMMapings self)
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapings_dbg);
|
2008-11-07 15:36:18 +01:00
|
|
|
free (lumiera_mmapings_destroy (self));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LumieraMMap
|
|
|
|
|
lumiera_mmapings_mmap_acquire (LumieraMMapings self, LumieraFile file, LList acquirer, off_t start, size_t size)
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapings_dbg);
|
2008-11-07 15:36:18 +01:00
|
|
|
|
|
|
|
|
LumieraMMap ret = NULL;
|
|
|
|
|
|
2009-01-24 03:12:32 +01:00
|
|
|
LUMIERA_MUTEX_SECTION (mutex_sync, &self->lock)
|
2008-11-07 15:36:18 +01:00
|
|
|
{
|
|
|
|
|
REQUIRE (llist_is_empty (acquirer));
|
|
|
|
|
|
2008-12-08 20:57:57 +01:00
|
|
|
/* find first matching mmap, crude way */
|
2008-11-07 15:36:18 +01:00
|
|
|
LLIST_FOREACH (&self->mmaps, node)
|
|
|
|
|
{
|
2008-12-08 20:57:57 +01:00
|
|
|
TODO ("improve this selection algorithm, choose mmaps by size, move mfu to head etc");
|
|
|
|
|
|
2008-11-07 15:36:18 +01:00
|
|
|
LumieraMMap mmap = LLIST_TO_STRUCTP (node, lumiera_mmap, searchnode);
|
|
|
|
|
|
|
|
|
|
if (mmap->size >= size && mmap->start <= start && mmap->start+mmap->size >= start+size)
|
|
|
|
|
{
|
|
|
|
|
ret = mmap;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-08 20:57:57 +01:00
|
|
|
/* found? */
|
|
|
|
|
if (ret)
|
|
|
|
|
{
|
|
|
|
|
if (!ret->refcnt)
|
|
|
|
|
/* in cache, needs to me checked out */
|
|
|
|
|
lumiera_mmapcache_checkout (lumiera_mcache, ret);
|
|
|
|
|
}
|
|
|
|
|
else
|
2008-11-07 15:36:18 +01:00
|
|
|
{
|
|
|
|
|
/* create new mmap */
|
2009-09-04 08:53:03 +02:00
|
|
|
TRACE (mmapings_dbg, "mmap not found, creating");
|
2008-12-08 20:57:57 +01:00
|
|
|
ret = lumiera_mmap_new (file, start, size);
|
2008-11-07 15:36:18 +01:00
|
|
|
|
|
|
|
|
llist_insert_head (&self->mmaps, &ret->searchnode);
|
|
|
|
|
|
2008-12-08 20:57:57 +01:00
|
|
|
TODO ("sort search list?");
|
2008-11-07 15:36:18 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-08 20:57:57 +01:00
|
|
|
llist_insert_head (&ret->cachenode, acquirer);
|
2008-11-07 15:36:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
lumiera_mmapings_release_mmap (LumieraMMapings self, LList acquirer, LumieraMMap map)
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapings_dbg);
|
|
|
|
|
|
|
|
|
|
LUMIERA_MUTEX_SECTION (mutex_sync, &self->lock)
|
2008-11-07 15:36:18 +01:00
|
|
|
{
|
|
|
|
|
llist_unlink (acquirer);
|
|
|
|
|
if (llist_is_empty (&map->cachenode))
|
|
|
|
|
{
|
2009-01-24 03:12:32 +01:00
|
|
|
TRACE (mmapcache_dbg, "checkin");
|
2008-11-07 15:36:18 +01:00
|
|
|
lumiera_mmapcache_checkin (lumiera_mcache, map);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
// Local Variables:
|
|
|
|
|
// mode: C
|
|
|
|
|
// c-file-style: "gnu"
|
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
|
// End:
|
|
|
|
|
*/
|