Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filedescriptorregistry.c
Go to the documentation of this file.
1 /*
2  FiledescriptorRegistry - registry for tracking all files in use
3 
4  Copyright (C) Lumiera.org
5  2008, 2010, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
30 #include "include/logging.h"
31 #include "lib/safeclib.h"
32 #include "lib/mutex.h"
33 #include "lib/psplay.h"
34 
35 #include "backend/file.h"
36 #include "backend/filedescriptor.h"
38 
39 
44 static lumiera_mutex filedescriptorregistry_mutex;
45 
46 
47 static int
48 cmp_fn (const void* keya, const void* keyb)
49 {
50  const LumieraFiledescriptor a = (const LumieraFiledescriptor)keya;
51  const LumieraFiledescriptor b = (const LumieraFiledescriptor)keyb;
52 
53  if (a->stat.st_dev < b->stat.st_dev)
54  return -1;
55  else if (a->stat.st_dev > b->stat.st_dev)
56  return 1;
57 
58  if (a->stat.st_ino < b->stat.st_ino)
59  return -1;
60  else if (a->stat.st_ino > b->stat.st_ino)
61  return 1;
62 
63  if ((a->flags&LUMIERA_FILE_MASK) < (b->flags&LUMIERA_FILE_MASK))
64  return -1;
65  else if ((a->flags&LUMIERA_FILE_MASK) > (b->flags&LUMIERA_FILE_MASK))
66  return 1;
67 
68  return 0;
69 }
70 
71 
72 static void
74 {
75  TODO ("figure name out? or is the handle here always closed");
77 }
78 
79 
80 static const void*
81 key_fn (const PSplaynode node)
82 {
83  return node;
84 }
85 
86 
87 
88 void
90 {
91  TRACE (filedescriptor_dbg);
92  REQUIRE (!filedescriptorregistry);
93 
96  LUMIERA_DIE (NO_MEMORY);
97 
98  lumiera_mutex_init (&filedescriptorregistry_mutex, "filedescriptorregistry", &NOBUG_FLAG (mutex_dbg), NOBUG_CONTEXT);
99 }
100 
101 void
103 {
104  TRACE (filedescriptor_dbg);
106 
107  lumiera_mutex_destroy (&filedescriptorregistry_mutex, &NOBUG_FLAG (mutex_dbg), NOBUG_CONTEXT);
108 
111 
113 }
114 
115 
118 {
119  REQUIRE (filedescriptorregistry, "not initialised");
120 
122 
124  {
125  /* lookup/create descriptor */
127 
128  if (!ret)
129  {
130  ret = lumiera_filedescriptor_new (template);
131  if (!ret)
132  goto error;
133 
134  psplay_insert (filedescriptorregistry, &ret->node, 100);
135  }
136 
137  error:
138  ;
139  }
140 
141  return ret;
142 }
143 
144 
145 void
147 {
149  psplay_remove (filedescriptorregistry, &self->node);
150 }
151 
152 
void lumiera_filedescriptorregistry_remove(LumieraFiledescriptor self)
Removes a file descriptor from the registry.
Portable and safe wrappers around some C-Lib functions.
LumieraFiledescriptor lumiera_filedescriptorregistry_ensure(LumieraFiledescriptor template)
Ensures that a filedescriptor is in the registry.
static const void * key_fn(const PSplaynode node)
static void delete_fn(PSplaynode node)
File descriptors are the underlying working horse in accessing files.
void lumiera_filedescriptorregistry_init(void)
Initialise the global file descriptor registry.
static PSplay filedescriptorregistry
the global registry for file descriptors.
Registry for used file descriptors.
#define LUMIERA_MUTEX_SECTION(nobugflag, mtx)
Mutual exclusive section.
Definition: mutex.h:41
File management.
This header is for including and configuring NoBug.
static int cmp_fn(const void *keya, const void *keyb)
PSplaynode psplay_find(PSplay self, const void *key, int splayfactor)
Find a element in a splay tree.
Definition: psplay.c:309
PSplaynode psplay_insert(PSplay self, PSplaynode node, int splayfactor)
Insert a element into a splay tree.
Definition: psplay.c:255
TRACE(test,"inserted %d", data)
static lumiera_mutex filedescriptorregistry_mutex
void psplay_delete(PSplay self)
Delete a splay tree Frees all elements and associated resources of a splay tree and then itseld...
Definition: psplay.c:124
return NULL
Definition: llist.h:596
static size_t psplay_nelements(PSplay self)
Number of elements in tree.
Definition: psplay.h:112
#define LUMIERA_DIE(err)
Abort unconditionally with a 'Fatal Error!' message.
Definition: error.h:63
PSplay psplay_new(psplay_cmp_fn cmp, psplay_key_fn key, psplay_delete_fn del)
Allocate a splay tree.
Definition: psplay.c:97
Probabilistic splay tree.
void lumiera_filedescriptor_delete(LumieraFiledescriptor self, const char *name)
Delete a filedescriptor Called whenever its reference count drops to zero.
void lumiera_filedescriptorregistry_destroy(void)
Destroy and free the global file descriptor registry.
Mutual exclusion locking, header.
lumiera_filedescriptor * LumieraFiledescriptor
PSplaynode psplay_remove(PSplay self, PSplaynode node)
Remove a node from a splay tree.
Definition: psplay.c:349
psplaynode * PSplaynode
Definition: psplay.h:49
psplay * PSplay
Definition: psplay.h:91
LumieraFiledescriptor lumiera_filedescriptor_new(LumieraFiledescriptor template)
Allocate a new filedescriptor cloned from a template.
LumieraMutex lumiera_mutex_init(LumieraMutex self, const char *purpose, struct nobug_flag *flag, const struct nobug_context ctx)
Initialise a mutex variable This initialises a 'fast' default mutex which must not be locked recursiv...
Definition: mutex.c:30
#define LUMIERA_FILE_MASK
Definition: file.h:70
LumieraMutex lumiera_mutex_destroy(LumieraMutex self, struct nobug_flag *flag, const struct nobug_context ctx)
Destroy a mutex variable.
Definition: mutex.c:48