btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / kernel / disk_device_manager / KDiskDeviceUtils.h
blobe91f25590644a9ce90036c2899d0427f833a3460
1 // KDiskDeviceUtils.h
3 #ifndef _K_DISK_DEVICE_UTILS_H
4 #define _K_DISK_DEVICE_UTILS_H
6 #include <stdlib.h>
7 #include <string.h>
9 #include <SupportDefs.h>
11 #include <util/AutoLock.h>
13 // instantiations
14 #include <KDiskDevice.h>
15 #include <KDiskDeviceManager.h>
16 #include <KDiskSystem.h>
17 #include <KPartition.h>
19 namespace BPrivate {
20 namespace DiskDevice {
22 // helper functions
24 // min
25 template<typename T>
26 static inline
27 const T&
28 min(const T &a, const T &b)
30 if (a < b)
31 return a;
32 return b;
35 // max
36 template<typename T>
37 static inline
38 const T&
39 max(const T &a, const T &b)
41 if (a < b)
42 return b;
43 return a;
46 // set_string
47 static inline
48 status_t
49 set_string(char *&location, const char *newValue)
51 // unset old value
52 if (location) {
53 free(location);
54 location = NULL;
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;
63 return error;
66 // compare_string
67 /*! \brief \c NULL aware strcmp().
69 \c NULL is considered the least of all strings. \c NULL equals \c NULL.
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.
77 static inline
78 int
79 compare_string(const char *str1, const char *str2)
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);
91 // locking
93 // instantiations
94 class KDiskDevice;
95 class KDiskDeviceManager;
96 class KDiskSystem;
97 class KPartition;
98 //typedef struct disk_device_data disk_device_data;
100 typedef AutoLocker<KDiskDevice, AutoLockerReadLocking<KDiskDevice> >
101 DeviceReadLocker;
102 typedef AutoLocker<KDiskDevice, AutoLockerWriteLocking<KDiskDevice> >
103 DeviceWriteLocker;
104 typedef AutoLocker<KDiskDeviceManager > ManagerLocker;
106 // AutoLockerPartitionRegistration
107 template<const int dummy = 0>
108 class AutoLockerPartitionRegistration {
109 public:
110 inline bool Lock(KPartition *partition)
112 if (partition == NULL)
113 return true;
114 partition->Register();
115 return true;
118 inline void Unlock(KPartition *partition)
120 if (partition != NULL)
121 partition->Unregister();
125 typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
126 PartitionRegistrar;
128 // AutoLockerDiskSystemLoading
129 template<const int dummy = 0>
130 class AutoLockerDiskSystemLoading {
131 public:
132 inline bool Lock(KDiskSystem *diskSystem)
134 return (diskSystem->Load() == B_OK);
137 inline void Unlock(KDiskSystem *diskSystem)
139 diskSystem->Unload();
143 typedef AutoLocker<KDiskSystem, AutoLockerDiskSystemLoading<> >
144 DiskSystemLoader;
147 // AutoLockerDeviceStructReadLocker
148 template<const int dummy = 0>
149 class AutoLockerDeviceStructReadLocker {
150 public:
151 inline bool Lock(disk_device_data *device)
153 return read_lock_disk_device(device->id);
156 inline void Unlock(disk_device_data *device)
158 read_unlock_disk_device(device->id);
162 typedef AutoLocker<disk_device_data, AutoLockerDeviceStructReadLocker<> >
163 DeviceStructReadLocker;
165 // AutoLockerDeviceStructWriteLocker
166 template<const int dummy = 0>
167 class AutoLockerDeviceStructWriteLocker {
168 public:
169 inline bool Lock(disk_device_data *device)
171 return write_lock_disk_device(device->id);
174 inline void Unlock(disk_device_data *device)
176 write_unlock_disk_device(device->id);
180 typedef AutoLocker<disk_device_data, AutoLockerDeviceStructWriteLocker<> >
181 DeviceStructWriteLocker;
184 } // namespace DiskDevice
185 } // namespace BPrivate
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;
196 #endif // _K_DISK_DEVICE_H