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&
min(const T & a,const T & b)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&
max(const T & a,const T & b)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
set_string(char * & location,const char * newValue)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
compare_string(const char * str1,const char * str2)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:
Lock(KPartition * partition)110 inline bool Lock(KPartition *partition)
111 {
112 if (partition == NULL)
113 return true;
114 partition->Register();
115 return true;
116 }
117
Unlock(KPartition * partition)118 inline void Unlock(KPartition *partition)
119 {
120 if (partition != NULL)
121 partition->Unregister();
122 }
123 };
124
125 typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
126 PartitionRegistrar;
127
128 // AutoLockerDiskSystemLoading
129 template<const int dummy = 0>
130 class AutoLockerDiskSystemLoading {
131 public:
Lock(KDiskSystem * diskSystem)132 inline bool Lock(KDiskSystem *diskSystem)
133 {
134 return (diskSystem->Load() == B_OK);
135 }
136
Unlock(KDiskSystem * diskSystem)137 inline void Unlock(KDiskSystem *diskSystem)
138 {
139 diskSystem->Unload();
140 }
141 };
142
143 typedef AutoLocker<KDiskSystem, AutoLockerDiskSystemLoading<> >
144 DiskSystemLoader;
145
146 /*
147 // AutoLockerDeviceStructReadLocker
148 template<const int dummy = 0>
149 class AutoLockerDeviceStructReadLocker {
150 public:
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
162 typedef AutoLocker<disk_device_data, AutoLockerDeviceStructReadLocker<> >
163 DeviceStructReadLocker;
164
165 // AutoLockerDeviceStructWriteLocker
166 template<const int dummy = 0>
167 class AutoLockerDeviceStructWriteLocker {
168 public:
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
180 typedef AutoLocker<disk_device_data, AutoLockerDeviceStructWriteLocker<> >
181 DeviceStructWriteLocker;
182 */
183
184 } // namespace DiskDevice
185 } // namespace BPrivate
186
187 using BPrivate::DiskDevice::set_string;
188 using BPrivate::DiskDevice::DeviceReadLocker;
189 using BPrivate::DiskDevice::DeviceWriteLocker;
190 using BPrivate::DiskDevice::ManagerLocker;
191 using BPrivate::DiskDevice::PartitionRegistrar;
192 using BPrivate::DiskDevice::DiskSystemLoader;
193 //using BPrivate::DiskDevice::DeviceStructReadLocker;
194 //using BPrivate::DiskDevice::DeviceStructReadLocker;
195
196 #endif // _K_DISK_DEVICE_H
197