2 Generic device list for use in drivers.
3 Copyright (C) 2008 Michael Lotz <mmlr@mlotz.ch>
4 Distributed under the terms of the MIT license.
6 #include "DeviceList.h"
7 #include <util/kernel_cpp.h>
12 struct device_list_entry
{
15 device_list_entry
* next
;
19 DeviceList::DeviceList()
27 DeviceList::~DeviceList()
31 device_list_entry
*current
= fDeviceList
;
33 device_list_entry
*next
= current
->next
;
42 DeviceList::AddDevice(const char *name
, void *device
)
44 device_list_entry
*entry
= new(std::nothrow
) device_list_entry
;
48 entry
->name
= strdup(name
);
49 if (entry
->name
== NULL
) {
54 entry
->device
= device
;
57 if (fDeviceList
== NULL
)
60 device_list_entry
*current
= fDeviceList
;
62 if (current
->next
== NULL
) {
63 current
->next
= entry
;
67 current
= current
->next
;
77 DeviceList::RemoveDevice(const char *name
, void *device
)
79 if (name
== NULL
&& device
== NULL
)
82 device_list_entry
*previous
= NULL
;
83 device_list_entry
*current
= fDeviceList
;
85 if ((name
!= NULL
&& strcmp(current
->name
, name
) == 0)
86 || (device
!= NULL
&& current
->device
== device
)) {
88 fDeviceList
= current
->next
;
90 previous
->next
= current
->next
;
99 current
= current
->next
;
102 return B_ENTRY_NOT_FOUND
;
107 DeviceList::FindDevice(const char *name
, void *device
)
109 if (name
== NULL
&& device
== NULL
)
112 device_list_entry
*current
= fDeviceList
;
114 if ((name
!= NULL
&& strcmp(current
->name
, name
) == 0)
115 || (device
!= NULL
&& current
->device
== device
))
116 return current
->device
;
118 current
= current
->next
;
126 DeviceList::CountDevices(const char *baseName
)
128 if (baseName
== NULL
)
132 int32 baseNameLength
= strlen(baseName
);
133 device_list_entry
*current
= fDeviceList
;
135 if (strncmp(current
->name
, baseName
, baseNameLength
) == 0)
138 current
= current
->next
;
146 DeviceList::DeviceAt(int32 index
)
148 device_list_entry
*current
= fDeviceList
;
151 return current
->device
;
153 current
= current
->next
;
161 DeviceList::PublishDevices()
165 fPublishList
= (char **)malloc((fDeviceCount
+ 1) * sizeof(char *));
166 if (fPublishList
== NULL
)
170 device_list_entry
*current
= fDeviceList
;
172 fPublishList
[index
++] = strdup(current
->name
);
173 current
= current
->next
;
176 fPublishList
[index
] = NULL
;
177 return (const char **)fPublishList
;
182 DeviceList::_FreePublishList()
184 if (fPublishList
== NULL
)
188 while (fPublishList
[index
] != NULL
) {
189 free(fPublishList
[index
]);