]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re_vfs/include/impl/simple_store.h
update
[l4.git] / l4 / pkg / l4re_vfs / include / impl / simple_store.h
1 /*
2  * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  *
9  * As a special exception, you may use this file as part of a free software
10  * library without restriction.  Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License.  This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18 #pragma once
19
20 template< unsigned O_SIZE, unsigned N = 10 >
21 struct Simple_store_sz
22 {
23   enum
24   {
25     Item_size = (O_SIZE + sizeof(unsigned long) - 1) / sizeof(unsigned long)
26   };
27
28   struct Item
29   {
30     unsigned long _str[Item_size];
31     Item *next;
32   };
33
34   enum { MAX = N };
35
36   Item *first;
37   Item _o[MAX];
38
39   Simple_store_sz() throw();
40
41   void *alloc() throw();
42   void free(void *b) throw();
43
44 };
45
46 template< unsigned O, unsigned N >
47 Simple_store_sz<O,N>::Simple_store_sz() throw() : first(_o)
48 {
49   for (unsigned i = 0; i < MAX - 1; ++i)
50     _o[i].next = _o + i + 1;
51
52   _o[MAX - 1].next = 0;
53 }
54
55 template< unsigned O, unsigned N >
56 inline
57 void *
58 Simple_store_sz<O,N>::alloc() throw()
59 {
60   Item *i = first;
61
62   if (i)
63     {
64       first = i->next;
65       return (void*)(i->_str);
66     }
67
68   return 0;
69 }
70
71 template< unsigned O, unsigned N >
72 inline
73 void
74 Simple_store_sz<O,N>::free(void *b) throw()
75 {
76   Item *i = reinterpret_cast<Item*>(b);
77   i->next = first;
78   first = i;
79 }
80
81
82 template< typename O, unsigned N = 10 >
83 struct Simple_store : public Simple_store_sz<sizeof(O), N> {};
84