1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
*
* This Software is licensed under one of the following licenses:
*
* 1) under the terms of the "Common Public License 1.0" a copy of which is
* in the file LICENSE.txt in the root directory. The license is also
* available from the Open Source Initiative, see
* http://www.opensource.org/licenses/cpl.php.
*
* 2) under the terms of the "The BSD License" a copy of which is in the file
* LICENSE2.txt in the root directory. The license is also available from
* the Open Source Initiative, see
* http://www.opensource.org/licenses/bsd-license.php.
*
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a
* copy of which is in the file LICENSE3.txt in the root directory. The
* license is also available from the Open Source Initiative, see
* http://www.opensource.org/licenses/gpl-license.php.
*
* Licensee has the right to choose one of the above licenses.
*
* Redistributions of source code must retain the above copyright
* notice and one of the license notices.
*
* Redistributions in binary form must reproduce both the above copyright
* notice, one of the license notices in the documentation
* and/or other materials provided with the distribution.
*/
/**********************************************************************
*
* MODULE: dat_dr.c
*
* PURPOSE: dynamic registry implementation
*
* $Id: dat_dr.c,v 1.17 2005/03/24 05:58:27 jlentini Exp $
**********************************************************************/
#include <dat2/dat_platform_specific.h>
#include "dat_dr.h"
#include "dat_dictionary.h"
/*********************************************************************
* *
* Global Variables *
* *
*********************************************************************/
static DAT_OS_LOCK g_dr_lock;
static DAT_DICTIONARY *g_dr_dictionary = NULL;
/*********************************************************************
* *
* External Functions *
* *
*********************************************************************/
//***********************************************************************
// Function: dat_dr_init
//***********************************************************************
DAT_RETURN dat_dr_init(void)
{
DAT_RETURN status;
status = dat_os_lock_init(&g_dr_lock);
if (DAT_SUCCESS != status) {
return status;
}
status = dat_dictionary_create(&g_dr_dictionary);
if (DAT_SUCCESS != status) {
return status;
}
return DAT_SUCCESS;
}
//***********************************************************************
// Function: dat_dr_fini
//***********************************************************************
DAT_RETURN dat_dr_fini(void)
{
DAT_RETURN status;
status = dat_os_lock_destroy(&g_dr_lock);
if (DAT_SUCCESS != status) {
return status;
}
status = dat_dictionary_destroy(g_dr_dictionary);
if (DAT_SUCCESS != status) {
return status;
}
return DAT_SUCCESS;
}
//***********************************************************************
// Function: dat_dr_insert
//***********************************************************************
DAT_RETURN
dat_dr_insert(IN const DAT_PROVIDER_INFO * info, IN DAT_DR_ENTRY * entry)
{
DAT_RETURN status;
DAT_DICTIONARY_ENTRY dict_entry = NULL;
DAT_DR_ENTRY *data;
data = dat_os_alloc(sizeof(DAT_DR_ENTRY));
if (NULL == data) {
status =
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY);
goto bail;
}
*data = *entry;
status = dat_dictionary_entry_create(&dict_entry);
if (DAT_SUCCESS != status) {
goto bail;
}
dat_os_lock(&g_dr_lock);
status = dat_dictionary_insert(g_dr_dictionary,
dict_entry,
info, (DAT_DICTIONARY_DATA *) data);
dat_os_unlock(&g_dr_lock);
bail:
if (DAT_SUCCESS != status) {
if (NULL != data) {
dat_os_free(data, sizeof(DAT_DR_ENTRY));
}
if (NULL != dict_entry) {
(void)dat_dictionary_entry_destroy(dict_entry);
}
}
return status;
}
//***********************************************************************
// Function: dat_dr_remove
//***********************************************************************
DAT_RETURN dat_dr_remove(IN const DAT_PROVIDER_INFO * info)
{
DAT_DICTIONARY_ENTRY dict_entry;
DAT_RETURN status;
DAT_DICTIONARY_DATA data;
dict_entry = NULL;
dat_os_lock(&g_dr_lock);
status = dat_dictionary_search(g_dr_dictionary, info, &data);
if (DAT_SUCCESS != status) {
/* return status from dat_dictionary_search() */
goto bail;
}
if (0 != ((DAT_DR_ENTRY *) data)->ref_count) {
status = DAT_ERROR(DAT_PROVIDER_IN_USE, 0);
goto bail;
}
status = dat_dictionary_remove(g_dr_dictionary,
&dict_entry, info, &data);
if (DAT_SUCCESS != status) {
/* return status from dat_dictionary_remove() */
goto bail;
}
dat_os_free(data, sizeof(DAT_DR_ENTRY));
bail:
dat_os_unlock(&g_dr_lock);
if (NULL != dict_entry) {
(void)dat_dictionary_entry_destroy(dict_entry);
}
return status;
}
//***********************************************************************
// Function: dat_dr_provider_open
//***********************************************************************
DAT_RETURN
dat_dr_provider_open(IN const DAT_PROVIDER_INFO * info,
OUT DAT_IA_OPEN_FUNC * p_ia_open_func)
{
DAT_RETURN status;
DAT_DICTIONARY_DATA data;
dat_os_lock(&g_dr_lock);
status = dat_dictionary_search(g_dr_dictionary, info, &data);
dat_os_unlock(&g_dr_lock);
if (DAT_SUCCESS == status) {
((DAT_DR_ENTRY *) data)->ref_count++;
*p_ia_open_func = ((DAT_DR_ENTRY *) data)->ia_open_func;
}
return status;
}
//***********************************************************************
// Function: dat_dr_provider_close
//***********************************************************************
DAT_RETURN dat_dr_provider_close(IN const DAT_PROVIDER_INFO * info)
{
DAT_RETURN status;
DAT_DICTIONARY_DATA data;
dat_os_lock(&g_dr_lock);
status = dat_dictionary_search(g_dr_dictionary, info, &data);
dat_os_unlock(&g_dr_lock);
if (DAT_SUCCESS == status) {
((DAT_DR_ENTRY *) data)->ref_count--;
}
return status;
}
//***********************************************************************
// Function: dat_dr_size
//***********************************************************************
DAT_RETURN dat_dr_size(OUT DAT_COUNT * size)
{
return dat_dictionary_size(g_dr_dictionary, size);
}
//***********************************************************************
// Function: dat_dr_list
//***********************************************************************
DAT_RETURN
dat_dr_list(IN DAT_COUNT max_to_return,
OUT DAT_COUNT * entries_returned,
OUT DAT_PROVIDER_INFO * (dat_provider_list[]))
{
DAT_DR_ENTRY **array;
DAT_COUNT array_size;
DAT_COUNT i;
DAT_RETURN status;
array = NULL;
status = DAT_SUCCESS;
/* The dictionary size may increase between the call to */
/* dat_dictionary_size() and dat_dictionary_enumerate(). */
/* Therefore we loop until a successful enumeration is made. */
*entries_returned = 0;
for (;;) {
status = dat_dictionary_size(g_dr_dictionary, &array_size);
if (status != DAT_SUCCESS) {
goto bail;
}
if (array_size == 0) {
status = DAT_SUCCESS;
goto bail;
}
array = dat_os_alloc(array_size * sizeof(DAT_DR_ENTRY *));
if (array == NULL) {
status =
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
DAT_RESOURCE_MEMORY);
goto bail;
}
dat_os_lock(&g_dr_lock);
status = dat_dictionary_enumerate(g_dr_dictionary,
(DAT_DICTIONARY_DATA *) array,
array_size);
dat_os_unlock(&g_dr_lock);
if (DAT_SUCCESS == status) {
break;
} else {
dat_os_free(array, array_size * sizeof(DAT_DR_ENTRY *));
array = NULL;
continue;
}
}
for (i = 0; (i < max_to_return) && (i < array_size); i++) {
if (NULL == dat_provider_list[i]) {
status =
DAT_ERROR(DAT_INVALID_PARAMETER, DAT_INVALID_ARG3);
goto bail;
}
*dat_provider_list[i] = array[i]->info;
}
*entries_returned = i;
bail:
if (NULL != array) {
dat_os_free(array, array_size * sizeof(DAT_DR_ENTRY *));
}
return status;
}
/*
* Local variables:
* c-indent-level: 4
* c-basic-offset: 4
* tab-width: 8
* End:
*/
|