xref: /haiku/headers/private/kernel/disk_device_manager/KDiskDeviceUtils.h (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 // KDiskDeviceUtils.h
2 
3 #ifndef _K_DISK_DEVICE_UTILS_H
4 #define _K_DISK_DEVICE_UTILS_H
5 
6 #include <stdlib.h>
7 #include <string.h>
8 
9 #include <SupportDefs.h>
10 
11 #include <util/AutoLock.h>
12 
13 // instantiations
14 #include <KDiskDevice.h>
15 #include <KDiskDeviceManager.h>
16 #include <KDiskSystem.h>
17 #include <KPartition.h>
18 
19 namespace BPrivate {
20 namespace DiskDevice {
21 
22 // helper functions
23 
24 // min
25 template<typename T>
26 static inline
27 const T&
28 min(const T &a, const T &b)
29 {
30 	if (a < b)
31 		return a;
32 	return b;
33 }
34 
35 // max
36 template<typename T>
37 static inline
38 const T&
39 max(const T &a, const T &b)
40 {
41 	if (a < b)
42 		return b;
43 	return a;
44 }
45 
46 // set_string
47 static inline
48 status_t
49 set_string(char *&location, const char *newValue)
50 {
51 	// unset old value
52 	if (location) {
53 		free(location);
54 		location = NULL;
55 	}
56 	// set new value
57 	status_t error = B_OK;
58 	if (newValue) {
59 		location = strdup(newValue);
60 		if (!location)
61 			error = B_NO_MEMORY;
62 	}
63 	return error;
64 }
65 
66 // compare_string
67 /*!	\brief \c NULL aware strcmp().
68 
69 	\c NULL is considered the least of all strings. \c NULL equals \c NULL.
70 
71 	\param str1 First string.
72 	\param str2 Second string.
73 	\return A value less than 0, if \a str1 is less than \a str2,
74 			0, if they are equal, or a value greater than 0, if
75 			\a str1 is greater \a str2.
76 */
77 static inline
78 int
79 compare_string(const char *str1, const char *str2)
80 {
81 	if (str1 == NULL) {
82 		if (str2 == NULL)
83 			return 0;
84 		return 1;
85 	} else if (str2 == NULL)
86 		return -1;
87 	return strcmp(str1, str2);
88 }
89 
90 
91 // locking
92 
93 // instantiations
94 class KDiskDevice;
95 class KDiskDeviceManager;
96 class KDiskSystem;
97 class KPartition;
98 //typedef struct disk_device_data disk_device_data;
99 
100 typedef AutoLocker<KDiskDevice, AutoLockerReadLocking<KDiskDevice> >
101 		DeviceReadLocker;
102 typedef AutoLocker<KDiskDevice, AutoLockerWriteLocking<KDiskDevice> >
103 		DeviceWriteLocker;
104 typedef AutoLocker<KDiskDeviceManager > ManagerLocker;
105 
106 // AutoLockerPartitionRegistration
107 template<const int dummy = 0>
108 class AutoLockerPartitionRegistration {
109 public:
110 	inline bool Lock(KPartition *partition)
111 	{
112 		partition->Register();
113 		return true;
114 	}
115 
116 	inline void Unlock(KPartition *partition)
117 	{
118 		partition->Unregister();
119 	}
120 };
121 
122 typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
123 	PartitionRegistrar;
124 
125 // AutoLockerDiskSystemLoading
126 template<const int dummy = 0>
127 class AutoLockerDiskSystemLoading {
128 public:
129 	inline bool Lock(KDiskSystem *diskSystem)
130 	{
131 		return (diskSystem->Load() == B_OK);
132 	}
133 
134 	inline void Unlock(KDiskSystem *diskSystem)
135 	{
136 		diskSystem->Unload();
137 	}
138 };
139 
140 typedef AutoLocker<KDiskSystem, AutoLockerDiskSystemLoading<> >
141 	DiskSystemLoader;
142 
143 /*
144 // AutoLockerDeviceStructReadLocker
145 template<const int dummy = 0>
146 class AutoLockerDeviceStructReadLocker {
147 public:
148 	inline bool Lock(disk_device_data *device)
149 	{
150 		return read_lock_disk_device(device->id);
151 	}
152 
153 	inline void Unlock(disk_device_data *device)
154 	{
155 		read_unlock_disk_device(device->id);
156 	}
157 };
158 
159 typedef AutoLocker<disk_device_data, AutoLockerDeviceStructReadLocker<> >
160 	DeviceStructReadLocker;
161 
162 // AutoLockerDeviceStructWriteLocker
163 template<const int dummy = 0>
164 class AutoLockerDeviceStructWriteLocker {
165 public:
166 	inline bool Lock(disk_device_data *device)
167 	{
168 		return write_lock_disk_device(device->id);
169 	}
170 
171 	inline void Unlock(disk_device_data *device)
172 	{
173 		write_unlock_disk_device(device->id);
174 	}
175 };
176 
177 typedef AutoLocker<disk_device_data, AutoLockerDeviceStructWriteLocker<> >
178 	DeviceStructWriteLocker;
179 */
180 
181 } // namespace DiskDevice
182 } // namespace BPrivate
183 
184 using BPrivate::DiskDevice::set_string;
185 using BPrivate::DiskDevice::DeviceReadLocker;
186 using BPrivate::DiskDevice::DeviceWriteLocker;
187 using BPrivate::DiskDevice::ManagerLocker;
188 using BPrivate::DiskDevice::PartitionRegistrar;
189 using BPrivate::DiskDevice::DiskSystemLoader;
190 //using BPrivate::DiskDevice::DeviceStructReadLocker;
191 //using BPrivate::DiskDevice::DeviceStructReadLocker;
192 
193 #endif	// _K_DISK_DEVICE_H
194