| 1 |
/* |
| 2 |
* Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This Software is licensed under one of the following licenses: |
| 5 |
* |
| 6 |
* 1) under the terms of the "Common Public License 1.0" a copy of which is |
| 7 |
* in the file LICENSE.txt in the root directory. The license is also |
| 8 |
* available from the Open Source Initiative, see |
| 9 |
* http://www.opensource.org/licenses/cpl.php. |
| 10 |
* |
| 11 |
* 2) under the terms of the "The BSD License" a copy of which is in the file |
| 12 |
* LICENSE2.txt in the root directory. The license is also available from |
| 13 |
* the Open Source Initiative, see |
| 14 |
* http://www.opensource.org/licenses/bsd-license.php. |
| 15 |
* |
| 16 |
* 3) under the terms of the "GNU General Public License (GPL) Version 2" a |
| 17 |
* copy of which is in the file LICENSE3.txt in the root directory. The |
| 18 |
* license is also available from the Open Source Initiative, see |
| 19 |
* http://www.opensource.org/licenses/gpl-license.php. |
| 20 |
* |
| 21 |
* Licensee has the right to choose one of the above licenses. |
| 22 |
* |
| 23 |
* Redistributions of source code must retain the above copyright |
| 24 |
* notice and one of the license notices. |
| 25 |
* |
| 26 |
* Redistributions in binary form must reproduce both the above copyright |
| 27 |
* notice, one of the license notices in the documentation |
| 28 |
* and/or other materials provided with the distribution. |
| 29 |
*/ |
| 30 |
|
| 31 |
/********************************************************************** |
| 32 |
* |
| 33 |
* MODULE: dat_dictionary.c |
| 34 |
* |
| 35 |
* PURPOSE: dictionary data structure |
| 36 |
* |
| 37 |
* $Id: dat_dictionary.c,v 1.17 2005/05/20 22:24:01 jlentini Exp $ |
| 38 |
**********************************************************************/ |
| 39 |
|
| 40 |
#include "dat_dictionary.h" |
| 41 |
|
| 42 |
/********************************************************************* |
| 43 |
* * |
| 44 |
* Structures * |
| 45 |
* * |
| 46 |
*********************************************************************/ |
| 47 |
|
| 48 |
typedef struct DAT_DICTIONARY_NODE { |
| 49 |
DAT_PROVIDER_INFO key; |
| 50 |
DAT_DICTIONARY_DATA data; |
| 51 |
struct DAT_DICTIONARY_NODE *prev; |
| 52 |
struct DAT_DICTIONARY_NODE *next; |
| 53 |
} DAT_DICTIONARY_NODE; |
| 54 |
|
| 55 |
struct DAT_DICTIONARY { |
| 56 |
DAT_DICTIONARY_NODE *head; |
| 57 |
DAT_DICTIONARY_NODE *tail; |
| 58 |
DAT_COUNT size; |
| 59 |
}; |
| 60 |
|
| 61 |
/********************************************************************* |
| 62 |
* * |
| 63 |
* Function Declarations * |
| 64 |
* * |
| 65 |
*********************************************************************/ |
| 66 |
|
| 67 |
static DAT_RETURN |
| 68 |
dat_dictionary_key_dup(const DAT_PROVIDER_INFO * old_key, |
| 69 |
DAT_PROVIDER_INFO * new_key); |
| 70 |
|
| 71 |
static DAT_BOOLEAN |
| 72 |
dat_dictionary_key_is_equal(const DAT_PROVIDER_INFO * key_a, |
| 73 |
const DAT_PROVIDER_INFO * key_b); |
| 74 |
|
| 75 |
/********************************************************************* |
| 76 |
* * |
| 77 |
* External Functions * |
| 78 |
* * |
| 79 |
*********************************************************************/ |
| 80 |
|
| 81 |
/*********************************************************************** |
| 82 |
* Function: dat_dictionary_create |
| 83 |
***********************************************************************/ |
| 84 |
|
| 85 |
DAT_RETURN dat_dictionary_create(OUT DAT_DICTIONARY ** pp_dictionary) |
| 86 |
{ |
| 87 |
DAT_DICTIONARY *p_dictionary; |
| 88 |
DAT_RETURN status; |
| 89 |
|
| 90 |
dat_os_assert(NULL != pp_dictionary); |
| 91 |
|
| 92 |
status = DAT_SUCCESS; |
| 93 |
|
| 94 |
/* create the dictionary */ |
| 95 |
p_dictionary = dat_os_alloc(sizeof(DAT_DICTIONARY)); |
| 96 |
if (NULL == p_dictionary) { |
| 97 |
status = |
| 98 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 99 |
goto bail; |
| 100 |
} |
| 101 |
|
| 102 |
dat_os_memset(p_dictionary, '\0', sizeof(DAT_DICTIONARY)); |
| 103 |
|
| 104 |
/* create the head node */ |
| 105 |
p_dictionary->head = dat_os_alloc(sizeof(DAT_DICTIONARY_NODE)); |
| 106 |
if (NULL == p_dictionary->head) { |
| 107 |
status = |
| 108 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 109 |
goto bail; |
| 110 |
} |
| 111 |
|
| 112 |
dat_os_memset(p_dictionary->head, '\0', sizeof(DAT_DICTIONARY_NODE)); |
| 113 |
|
| 114 |
/* create the tail node */ |
| 115 |
p_dictionary->tail = dat_os_alloc(sizeof(DAT_DICTIONARY_NODE)); |
| 116 |
if (NULL == p_dictionary->tail) { |
| 117 |
status = |
| 118 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 119 |
goto bail; |
| 120 |
} |
| 121 |
|
| 122 |
dat_os_memset(p_dictionary->tail, '\0', sizeof(DAT_DICTIONARY_NODE)); |
| 123 |
|
| 124 |
p_dictionary->head->next = p_dictionary->tail; |
| 125 |
p_dictionary->tail->prev = p_dictionary->head; |
| 126 |
|
| 127 |
*pp_dictionary = p_dictionary; |
| 128 |
|
| 129 |
bail: |
| 130 |
if (DAT_SUCCESS != status) { |
| 131 |
if (NULL != p_dictionary) { |
| 132 |
if (NULL != p_dictionary->head) { |
| 133 |
dat_os_free(p_dictionary->head, |
| 134 |
sizeof(DAT_DICTIONARY_NODE)); |
| 135 |
} |
| 136 |
|
| 137 |
if (NULL != p_dictionary->tail) { |
| 138 |
dat_os_free(p_dictionary->tail, |
| 139 |
sizeof(DAT_DICTIONARY_NODE)); |
| 140 |
} |
| 141 |
|
| 142 |
dat_os_free(p_dictionary, sizeof(DAT_DICTIONARY)); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return status; |
| 147 |
} |
| 148 |
|
| 149 |
/*********************************************************************** |
| 150 |
* Function: dat_dictionary_destroy |
| 151 |
***********************************************************************/ |
| 152 |
|
| 153 |
DAT_RETURN dat_dictionary_destroy(IN DAT_DICTIONARY * p_dictionary) |
| 154 |
{ |
| 155 |
DAT_DICTIONARY_NODE *cur_node; |
| 156 |
|
| 157 |
dat_os_assert(NULL != p_dictionary); |
| 158 |
|
| 159 |
while (NULL != p_dictionary->head) { |
| 160 |
cur_node = p_dictionary->head; |
| 161 |
p_dictionary->head = cur_node->next; |
| 162 |
|
| 163 |
dat_os_free(cur_node, sizeof(DAT_DICTIONARY_NODE)); |
| 164 |
} |
| 165 |
|
| 166 |
dat_os_free(p_dictionary, sizeof(DAT_DICTIONARY)); |
| 167 |
|
| 168 |
return DAT_SUCCESS; |
| 169 |
} |
| 170 |
|
| 171 |
/*********************************************************************** |
| 172 |
* Function: dat_dictionary_size |
| 173 |
***********************************************************************/ |
| 174 |
|
| 175 |
DAT_RETURN |
| 176 |
dat_dictionary_size(IN DAT_DICTIONARY * p_dictionary, OUT DAT_COUNT * p_size) |
| 177 |
{ |
| 178 |
dat_os_assert(NULL != p_dictionary); |
| 179 |
dat_os_assert(NULL != p_size); |
| 180 |
|
| 181 |
*p_size = p_dictionary->size; |
| 182 |
|
| 183 |
return DAT_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
/*********************************************************************** |
| 187 |
* Function: dat_dictionary_entry_create |
| 188 |
***********************************************************************/ |
| 189 |
|
| 190 |
DAT_RETURN dat_dictionary_entry_create(OUT DAT_DICTIONARY_ENTRY * p_entry) |
| 191 |
{ |
| 192 |
DAT_DICTIONARY_NODE *node; |
| 193 |
DAT_RETURN dat_status; |
| 194 |
|
| 195 |
dat_os_assert(NULL != p_entry); |
| 196 |
|
| 197 |
dat_status = DAT_SUCCESS; |
| 198 |
|
| 199 |
node = dat_os_alloc(sizeof(DAT_DICTIONARY_NODE)); |
| 200 |
if (NULL == node) { |
| 201 |
dat_status = |
| 202 |
DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, DAT_RESOURCE_MEMORY); |
| 203 |
goto bail; |
| 204 |
} |
| 205 |
|
| 206 |
*p_entry = node; |
| 207 |
|
| 208 |
bail: |
| 209 |
return dat_status; |
| 210 |
} |
| 211 |
|
| 212 |
/*********************************************************************** |
| 213 |
* Function: dat_dictionary_entry_destroy |
| 214 |
***********************************************************************/ |
| 215 |
|
| 216 |
DAT_RETURN dat_dictionary_entry_destroy(OUT DAT_DICTIONARY_ENTRY entry) |
| 217 |
{ |
| 218 |
dat_os_free(entry, sizeof(DAT_DICTIONARY_NODE)); |
| 219 |
return DAT_SUCCESS; |
| 220 |
} |
| 221 |
|
| 222 |
/*********************************************************************** |
| 223 |
* Function: dat_dictionary_insert |
| 224 |
***********************************************************************/ |
| 225 |
|
| 226 |
DAT_RETURN |
| 227 |
dat_dictionary_insert(IN DAT_DICTIONARY * p_dictionary, |
| 228 |
IN DAT_DICTIONARY_ENTRY entry, |
| 229 |
IN const DAT_PROVIDER_INFO * key, |
| 230 |
IN DAT_DICTIONARY_DATA data) |
| 231 |
{ |
| 232 |
DAT_RETURN dat_status; |
| 233 |
DAT_DICTIONARY_NODE *cur_node, *prev_node, *next_node; |
| 234 |
|
| 235 |
dat_os_assert(NULL != p_dictionary); |
| 236 |
dat_os_assert(NULL != entry); |
| 237 |
|
| 238 |
cur_node = entry; |
| 239 |
|
| 240 |
if (DAT_SUCCESS == dat_dictionary_search(p_dictionary, key, NULL)) { |
| 241 |
dat_status = DAT_ERROR(DAT_PROVIDER_ALREADY_REGISTERED, 0); |
| 242 |
goto bail; |
| 243 |
} |
| 244 |
|
| 245 |
dat_status = dat_dictionary_key_dup(key, &cur_node->key); |
| 246 |
if (DAT_SUCCESS != dat_status) { |
| 247 |
goto bail; |
| 248 |
} |
| 249 |
|
| 250 |
/* insert node at end of list to preserve registration order */ |
| 251 |
prev_node = p_dictionary->tail->prev; |
| 252 |
next_node = p_dictionary->tail; |
| 253 |
|
| 254 |
cur_node->data = data; |
| 255 |
cur_node->next = next_node; |
| 256 |
cur_node->prev = prev_node; |
| 257 |
|
| 258 |
prev_node->next = cur_node; |
| 259 |
next_node->prev = cur_node; |
| 260 |
|
| 261 |
p_dictionary->size++; |
| 262 |
|
| 263 |
bail: |
| 264 |
return dat_status; |
| 265 |
} |
| 266 |
|
| 267 |
/*********************************************************************** |
| 268 |
* Function: dat_dictionary_search |
| 269 |
***********************************************************************/ |
| 270 |
|
| 271 |
DAT_RETURN |
| 272 |
dat_dictionary_search(IN DAT_DICTIONARY * p_dictionary, |
| 273 |
IN const DAT_PROVIDER_INFO * key, |
| 274 |
OUT DAT_DICTIONARY_DATA * p_data) |
| 275 |
{ |
| 276 |
DAT_DICTIONARY_NODE *cur_node; |
| 277 |
DAT_RETURN status; |
| 278 |
|
| 279 |
dat_os_assert(NULL != p_dictionary); |
| 280 |
|
| 281 |
status = DAT_ERROR(DAT_PROVIDER_NOT_FOUND, DAT_NAME_NOT_REGISTERED); |
| 282 |
|
| 283 |
for (cur_node = p_dictionary->head->next; |
| 284 |
p_dictionary->tail != cur_node; cur_node = cur_node->next) { |
| 285 |
if (DAT_TRUE == |
| 286 |
dat_dictionary_key_is_equal(&cur_node->key, key)) { |
| 287 |
if (NULL != p_data) { |
| 288 |
*p_data = cur_node->data; |
| 289 |
} |
| 290 |
|
| 291 |
status = DAT_SUCCESS; |
| 292 |
goto bail; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
bail: |
| 297 |
return status; |
| 298 |
} |
| 299 |
|
| 300 |
/*********************************************************************** |
| 301 |
* Function: dat_dictionary_enumerate |
| 302 |
***********************************************************************/ |
| 303 |
|
| 304 |
DAT_RETURN |
| 305 |
dat_dictionary_enumerate(IN DAT_DICTIONARY * p_dictionary, |
| 306 |
IN DAT_DICTIONARY_DATA array[], |
| 307 |
IN DAT_COUNT array_size) |
| 308 |
{ |
| 309 |
DAT_DICTIONARY_NODE *cur_node; |
| 310 |
DAT_COUNT i; |
| 311 |
DAT_RETURN status; |
| 312 |
|
| 313 |
dat_os_assert(NULL != p_dictionary); |
| 314 |
dat_os_assert(NULL != array); |
| 315 |
|
| 316 |
status = DAT_SUCCESS; |
| 317 |
|
| 318 |
if (array_size < p_dictionary->size) { |
| 319 |
status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES, 0); |
| 320 |
goto bail; |
| 321 |
} |
| 322 |
|
| 323 |
for (cur_node = p_dictionary->head->next, i = 0; |
| 324 |
p_dictionary->tail != cur_node; cur_node = cur_node->next, i++) { |
| 325 |
array[i] = cur_node->data; |
| 326 |
} |
| 327 |
|
| 328 |
bail: |
| 329 |
return status; |
| 330 |
} |
| 331 |
|
| 332 |
/*********************************************************************** |
| 333 |
* Function: dat_dictionary_remove |
| 334 |
***********************************************************************/ |
| 335 |
|
| 336 |
DAT_RETURN |
| 337 |
dat_dictionary_remove(IN DAT_DICTIONARY * p_dictionary, |
| 338 |
IN DAT_DICTIONARY_ENTRY * p_entry, |
| 339 |
IN const DAT_PROVIDER_INFO * key, |
| 340 |
OUT DAT_DICTIONARY_DATA * p_data) |
| 341 |
{ |
| 342 |
DAT_DICTIONARY_NODE *cur_node, *prev_node, *next_node; |
| 343 |
DAT_RETURN status; |
| 344 |
|
| 345 |
dat_os_assert(NULL != p_dictionary); |
| 346 |
dat_os_assert(NULL != p_entry); |
| 347 |
|
| 348 |
status = DAT_ERROR(DAT_PROVIDER_NOT_FOUND, DAT_NAME_NOT_REGISTERED); |
| 349 |
|
| 350 |
for (cur_node = p_dictionary->head->next; |
| 351 |
p_dictionary->tail != cur_node; cur_node = cur_node->next) { |
| 352 |
if (DAT_TRUE == |
| 353 |
dat_dictionary_key_is_equal(&cur_node->key, key)) { |
| 354 |
if (NULL != p_data) { |
| 355 |
*p_data = cur_node->data; |
| 356 |
} |
| 357 |
|
| 358 |
prev_node = cur_node->prev; |
| 359 |
next_node = cur_node->next; |
| 360 |
|
| 361 |
prev_node->next = next_node; |
| 362 |
next_node->prev = prev_node; |
| 363 |
|
| 364 |
*p_entry = cur_node; |
| 365 |
|
| 366 |
p_dictionary->size--; |
| 367 |
|
| 368 |
status = DAT_SUCCESS; |
| 369 |
goto bail; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
bail: |
| 374 |
return status; |
| 375 |
} |
| 376 |
|
| 377 |
/********************************************************************* |
| 378 |
* * |
| 379 |
* Internal Function Definitions * |
| 380 |
* * |
| 381 |
*********************************************************************/ |
| 382 |
|
| 383 |
/*********************************************************************** |
| 384 |
* Function: dat_dictionary_key_create |
| 385 |
***********************************************************************/ |
| 386 |
|
| 387 |
DAT_RETURN |
| 388 |
dat_dictionary_key_dup(const DAT_PROVIDER_INFO * old_key, |
| 389 |
DAT_PROVIDER_INFO * new_key) |
| 390 |
{ |
| 391 |
dat_os_assert(NULL != old_key); |
| 392 |
dat_os_assert(NULL != new_key); |
| 393 |
|
| 394 |
dat_os_strncpy(new_key->ia_name, old_key->ia_name, DAT_NAME_MAX_LENGTH); |
| 395 |
new_key->dapl_version_major = old_key->dapl_version_major; |
| 396 |
new_key->dapl_version_minor = old_key->dapl_version_minor; |
| 397 |
new_key->is_thread_safe = old_key->is_thread_safe; |
| 398 |
|
| 399 |
return DAT_SUCCESS; |
| 400 |
} |
| 401 |
|
| 402 |
/*********************************************************************** |
| 403 |
* Function: dat_dictionary_key_is_equal |
| 404 |
***********************************************************************/ |
| 405 |
|
| 406 |
DAT_BOOLEAN |
| 407 |
dat_dictionary_key_is_equal(const DAT_PROVIDER_INFO * key_a, |
| 408 |
const DAT_PROVIDER_INFO * key_b) |
| 409 |
{ |
| 410 |
if ((dat_os_strlen(key_a->ia_name) == dat_os_strlen(key_b->ia_name)) && |
| 411 |
(!dat_os_strncmp |
| 412 |
(key_a->ia_name, key_b->ia_name, dat_os_strlen(key_a->ia_name))) |
| 413 |
&& (key_a->dapl_version_major == key_b->dapl_version_major) |
| 414 |
&& (key_a->dapl_version_minor == key_b->dapl_version_minor) |
| 415 |
&& (key_a->is_thread_safe == key_b->is_thread_safe)) { |
| 416 |
return DAT_TRUE; |
| 417 |
} else { |
| 418 |
return DAT_FALSE; |
| 419 |
} |
| 420 |
} |