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
16enum 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*/
31template<typename IntType, typename RoundType>
32inline IntType
33round_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*/
41template<typename IntType, typename RoundType>
42inline IntType
43round_down(const IntType& value, const RoundType& to)
44{
45	return value & ~((IntType)to - 1);
46}
47
48
49inline int32
50divide_roundup(int32 num, int32 divisor)
51{
52	return (num + divisor - 1) / divisor;
53}
54
55
56inline int64
57divide_roundup(int64 num, int32 divisor)
58{
59	return (num + divisor - 1) / divisor;
60}
61
62
63inline int
64get_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
76inline int32
77runs_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
87inline int32
88double_indirect_max_direct_size(uint32 baseLength, uint32 blockSize)
89{
90	return baseLength * blockSize;
91}
92
93
94inline int32
95double_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
102inline void
103get_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
112inline uint32
113key_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
120inline bool
121is_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
129inline bool
130is_directory(int mode)
131{
132	return (mode & (S_EXTENDED_TYPES | S_IFDIR)) == S_IFDIR;
133}
134
135
136#endif	// UTILITY_H
137