KDiskDeviceUtils.h revision 4419d699
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
19namespace BPrivate {
20namespace DiskDevice {
21
22// helper functions
23
24// min
25template<typename T>
26static inline
27const T&
28min(const T &a, const T &b)
29{
30	if (a < b)
31		return a;
32	return b;
33}
34
35// max
36template<typename T>
37static inline
38const T&
39max(const T &a, const T &b)
40{
41	if (a < b)
42		return b;
43	return a;
44}
45
46// set_string
47static inline
48status_t
49set_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*/
77static inline
78int
79compare_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
94class KDiskDevice;
95class KDiskDeviceManager;
96class KDiskSystem;
97class KPartition;
98//typedef struct disk_device_data disk_device_data;
99
100typedef AutoLocker<KDiskDevice, AutoLockerReadLocking<KDiskDevice> >
101		DeviceReadLocker;
102typedef AutoLocker<KDiskDevice, AutoLockerWriteLocking<KDiskDevice> >
103		DeviceWriteLocker;
104typedef AutoLocker<KDiskDeviceManager > ManagerLocker;
105
106// AutoLockerPartitionRegistration
107template<const int dummy = 0>
108class AutoLockerPartitionRegistration {
109public:
110	inline bool Lock(KPartition *partition)
111	{
112		if (partition == NULL)
113			return true;
114		partition->Register();
115		return true;
116	}
117
118	inline void Unlock(KPartition *partition)
119	{
120		if (partition != NULL)
121			partition->Unregister();
122	}
123};
124
125typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
126	PartitionRegistrar;
127
128// AutoLockerDiskSystemLoading
129template<const int dummy = 0>
130class AutoLockerDiskSystemLoading {
131public:
132	inline bool Lock(KDiskSystem *diskSystem)
133	{
134		return (diskSystem->Load() == B_OK);
135	}
136
137	inline void Unlock(KDiskSystem *diskSystem)
138	{
139		diskSystem->Unload();
140	}
141};
142
143typedef AutoLocker<KDiskSystem, AutoLockerDiskSystemLoading<> >
144	DiskSystemLoader;
145
146/*
147// AutoLockerDeviceStructReadLocker
148template<const int dummy = 0>
149class AutoLockerDeviceStructReadLocker {
150public:
151	inline bool Lock(disk_device_data *device)
152	{
153		return read_lock_disk_device(device->id);
154	}
155
156	inline void Unlock(disk_device_data *device)
157	{
158		read_unlock_disk_device(device->id);
159	}
160};
161
162typedef AutoLocker<disk_device_data, AutoLockerDeviceStructReadLocker<> >
163	DeviceStructReadLocker;
164
165// AutoLockerDeviceStructWriteLocker
166template<const int dummy = 0>
167class AutoLockerDeviceStructWriteLocker {
168public:
169	inline bool Lock(disk_device_data *device)
170	{
171		return write_lock_disk_device(device->id);
172	}
173
174	inline void Unlock(disk_device_data *device)
175	{
176		write_unlock_disk_device(device->id);
177	}
178};
179
180typedef AutoLocker<disk_device_data, AutoLockerDeviceStructWriteLocker<> >
181	DeviceStructWriteLocker;
182*/
183
184} // namespace DiskDevice
185} // namespace BPrivate
186
187using BPrivate::DiskDevice::set_string;
188using BPrivate::DiskDevice::DeviceReadLocker;
189using BPrivate::DiskDevice::DeviceWriteLocker;
190using BPrivate::DiskDevice::ManagerLocker;
191using BPrivate::DiskDevice::PartitionRegistrar;
192using BPrivate::DiskDevice::DiskSystemLoader;
193//using BPrivate::DiskDevice::DeviceStructReadLocker;
194//using BPrivate::DiskDevice::DeviceStructReadLocker;
195
196#endif	// _K_DISK_DEVICE_H
197