Refactored filehandle/filehandlecache to use a normal init function

This commit is contained in:
Christian Thaeter 2008-11-11 10:06:03 +01:00
parent e9cca69fb1
commit e953c3003b
3 changed files with 31 additions and 15 deletions

View file

@ -29,20 +29,28 @@
#include <unistd.h>
LumieraFilehandle
lumiera_filehandle_new ()
lumiera_filehandle_init (LumieraFilehandle self, LumieraFiledescriptor desc)
{
LumieraFilehandle self = lumiera_malloc (sizeof (lumiera_filehandle));
TRACE (filehandle, "%p", self);
llist_init (&self->cachenode);
self->fd = -1;
self->use_cnt = 1;
self->descriptor = NULL;
if (self)
{
llist_init (&self->cachenode);
self->fd = -1;
self->use_cnt = 1;
self->descriptor = desc;
}
return self;
}
LumieraFilehandle
lumiera_filehandle_new (LumieraFiledescriptor desc)
{
LumieraFilehandle self = lumiera_malloc (sizeof (*self));
return lumiera_filehandle_init (self, desc);
}
void*
lumiera_filehandle_destroy_node (LList node)
{
@ -53,8 +61,6 @@ lumiera_filehandle_destroy_node (LList node)
if (self->fd >= 0)
close (self->fd);
self->fd = -1;
self->descriptor = NULL;
return self;
}

View file

@ -52,11 +52,21 @@ struct lumiera_filehandle_struct
};
/**
* Allocate a new filehandle structure.
* Initialize filehandle structure.
* @param self filehandle sttructure to be initialized
* @param descriptor on which this filehandle will be attached
* @return new filehandle structure
*/
LumieraFilehandle
lumiera_filehandle_new ();
lumiera_filehandle_init (LumieraFilehandle self, LumieraFiledescriptor descriptor);
/**
* Allocate a new filehandle structure.
* @param descriptor on which this filehandle will be attached
* @return new filehandle structure
*/
LumieraFilehandle
lumiera_filehandle_new (LumieraFiledescriptor descriptor);
/**

View file

@ -74,6 +74,7 @@ lumiera_filehandlecache_handle_acquire (LumieraFilehandlecache self, LumieraFile
{
/* pop a filehandle from cache */
ret = lumiera_mrucache_pop (&self->cache);
ret = lumiera_filehandle_init (lumiera_mrucache_pop (&self->cache), desc);
if (self->available < 0)
/* try to free overallocated filehandles */
self->available -= self->available + lumiera_mrucache_age (&self->cache, -self->available);
@ -81,14 +82,13 @@ lumiera_filehandlecache_handle_acquire (LumieraFilehandlecache self, LumieraFile
else
{
/* allocate new filehandle if we are below the limit or no cached handles are available (overallocating) */
ret = lumiera_filehandle_new ();
NOTICE (filehandlecache, "overallocating filehandle");
ret = lumiera_filehandle_new (desc);
if (!ret)
LUMIERA_ERROR_SET (filehandlecache, FILEHANDLECACHE_NOHANDLE);
else
--self->available;
}
ret->use_cnt = 1;
ret->descriptor = desc;
desc->handle = ret;
++self->checked_out;
}