BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / pegasus / devlist.c
blob87953b3b54cf5c490159edae9dd11fd2dc5163d7
1 /*
2 * Copyright 2004-2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Jérôme Duval
8 * Some portions of code are copyrighted by
9 * USB Joystick driver for BeOS R5
10 * Copyright 2000 (C) ITO, Takayuki. All rights reserved
14 #include "driver.h"
16 #include <stdlib.h>
17 #include <string.h>
20 sem_id gDeviceListLock = -1;
21 bool gDeviceListChanged = true; /* added or removed */
22 /* dynamically generated */
23 char **gDeviceNames = NULL;
26 static pegasus_dev *sDeviceList = NULL;
27 static int sDeviceCount = 0;
30 void
31 add_device_info(pegasus_dev *device)
33 ASSERT(device != NULL);
35 acquire_sem(gDeviceListLock);
36 device->next = sDeviceList;
37 sDeviceList = device;
38 sDeviceCount++;
39 gDeviceListChanged = true;
40 release_sem(gDeviceListLock);
44 void
45 remove_device_info(pegasus_dev *device)
47 ASSERT(device != NULL);
49 acquire_sem(gDeviceListLock);
51 if (sDeviceList == device) {
52 sDeviceList = device->next;
53 --sDeviceCount;
54 gDeviceListChanged = true;
55 } else {
56 pegasus_dev *previous;
57 for (previous = sDeviceList; previous != NULL; previous = previous->next) {
58 if (previous->next == device) {
59 previous->next = device->next;
60 --sDeviceCount;
61 gDeviceListChanged = true;
62 break;
65 ASSERT(previous != NULL);
67 release_sem(gDeviceListLock);
71 pegasus_dev *
72 search_device_info(const char* name)
74 pegasus_dev *device;
76 acquire_sem(gDeviceListLock);
77 for (device = sDeviceList; device != NULL; device = device->next) {
78 if (strcmp(device->name, name) == 0)
79 break;
82 release_sem(gDeviceListLock);
83 return device;
87 // #pragma mark - device names
90 void
91 alloc_device_names(void)
93 ASSERT(gDeviceNames == NULL);
94 gDeviceNames = malloc(sizeof(char *) * (sDeviceCount + 1));
98 void
99 free_device_names(void)
101 if (gDeviceNames != NULL) {
102 int i;
103 for (i = 0; gDeviceNames [i] != NULL; i++) {
104 free(gDeviceNames[i]);
107 free(gDeviceNames);
108 gDeviceNames = NULL;
113 void
114 rebuild_device_names(void)
116 int i;
117 pegasus_dev *device;
119 ASSERT(gDeviceNames != NULL);
120 acquire_sem(gDeviceListLock);
121 for (i = 0, device = sDeviceList; device != NULL; device = device->next) {
122 gDeviceNames[i++] = strdup(device->name);
123 DPRINTF_INFO(MY_ID "publishing %s\n", device->name);
125 gDeviceNames[i] = NULL;
126 release_sem(gDeviceListLock);