1 /*
2 * Copyright 2001-2009, Axel Dörfler, axeld@pinc-software.de.
3 * This file may be used under the terms of the MIT License.
4 */
5 #ifndef UTILITY_H
6 #define UTILITY_H
7
8
9 #include "system_dependencies.h"
10
11 #include <file_systems/fs_ops_support.h>
12
13 #include "bfs.h"
14
15
16 enum inode_type {
17 S_DIRECTORY = S_IFDIR,
18 S_FILE = S_IFREG,
19 S_SYMLINK = S_IFLNK,
20
21 S_INDEX_TYPES = (S_STR_INDEX | S_INT_INDEX | S_UINT_INDEX
22 | S_LONG_LONG_INDEX | S_ULONG_LONG_INDEX
23 | S_FLOAT_INDEX | S_DOUBLE_INDEX),
24
25 S_EXTENDED_TYPES = (S_ATTR_DIR | S_ATTR | S_INDEX_DIR)
26 };
27
28
29 /*! \a to must be a power of 2.
30 */
31 template<typename IntType, typename RoundType>
32 inline IntType
round_up(const IntType & value,const RoundType & to)33 round_up(const IntType& value, const RoundType& to)
34 {
35 return (value + (to - 1)) & ~((IntType)to - 1);
36 }
37
38
39 /*! \a to must be a power of 2.
40 */
41 template<typename IntType, typename RoundType>
42 inline IntType
round_down(const IntType & value,const RoundType & to)43 round_down(const IntType& value, const RoundType& to)
44 {
45 return value & ~((IntType)to - 1);
46 }
47
48
49 inline int32
divide_roundup(int32 num,int32 divisor)50 divide_roundup(int32 num, int32 divisor)
51 {
52 return (num + divisor - 1) / divisor;
53 }
54
55
56 inline int64
divide_roundup(int64 num,int32 divisor)57 divide_roundup(int64 num, int32 divisor)
58 {
59 return (num + divisor - 1) / divisor;
60 }
61
62
63 inline int
get_shift(uint64 i)64 get_shift(uint64 i)
65 {
66 int c;
67 c = 0;
68 while (i > 1) {
69 i >>= 1;
70 c++;
71 }
72 return c;
73 }
74
75
76 inline int32
runs_per_block(uint32 blockSize)77 runs_per_block(uint32 blockSize)
78 {
79 #ifdef _BOOT_MODE
80 using namespace BFS;
81 #endif
82
83 return blockSize / sizeof(struct block_run);
84 }
85
86
87 inline int32
double_indirect_max_direct_size(uint32 baseLength,uint32 blockSize)88 double_indirect_max_direct_size(uint32 baseLength, uint32 blockSize)
89 {
90 return baseLength * blockSize;
91 }
92
93
94 inline int32
double_indirect_max_indirect_size(uint32 baseLength,uint32 blockSize)95 double_indirect_max_indirect_size(uint32 baseLength, uint32 blockSize)
96 {
97 return baseLength * double_indirect_max_direct_size(baseLength, blockSize)
98 * runs_per_block(blockSize);
99 }
100
101
102 inline void
get_double_indirect_sizes(uint32 baseLength,uint32 blockSize,int32 & runsPerBlock,int32 & directSize,int32 & indirectSize)103 get_double_indirect_sizes(uint32 baseLength, uint32 blockSize,
104 int32& runsPerBlock, int32& directSize, int32& indirectSize)
105 {
106 runsPerBlock = runs_per_block(blockSize);
107 directSize = double_indirect_max_direct_size(baseLength, blockSize);
108 indirectSize = baseLength * directSize * runsPerBlock;
109 }
110
111
112 inline uint32
key_align(uint32 data)113 key_align(uint32 data)
114 {
115 // rounds up to the next off_t boundary
116 return (data + sizeof(off_t) - 1) & ~(sizeof(off_t) - 1);
117 }
118
119
120 inline bool
is_index(int mode)121 is_index(int mode)
122 {
123 return (mode & (S_EXTENDED_TYPES | 0777)) == S_INDEX_DIR;
124 // That's a stupid check, but the only method to differentiate the
125 // index root from an index.
126 }
127
128
129 inline bool
is_directory(int mode)130 is_directory(int mode)
131 {
132 return (mode & (S_EXTENDED_TYPES | S_IFDIR)) == S_IFDIR;
133 }
134
135
136 #endif // UTILITY_H
137