citrus_mapper.c (252464) | citrus_mapper.c (252584) |
---|---|
1/* $FreeBSD: head/lib/libc/iconv/citrus_mapper.c 252464 2013-07-01 08:38:31Z peter $ */ | 1/* $FreeBSD: head/lib/libc/iconv/citrus_mapper.c 252584 2013-07-03 18:35:21Z peter $ */ |
2/* $NetBSD: citrus_mapper.c,v 1.7 2008/07/25 14:05:25 christos Exp $ */ 3 4/*- 5 * Copyright (c)2003 Citrus Project, 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions --- 40 unchanged lines hidden (view full) --- 50#include "citrus_hash.h" 51#include "citrus_mapper.h" 52 53#define _CITRUS_MAPPER_DIR "mapper.dir" 54 55#define CM_HASH_SIZE 101 56#define REFCOUNT_PERSISTENT -1 57 | 2/* $NetBSD: citrus_mapper.c,v 1.7 2008/07/25 14:05:25 christos Exp $ */ 3 4/*- 5 * Copyright (c)2003 Citrus Project, 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions --- 40 unchanged lines hidden (view full) --- 50#include "citrus_hash.h" 51#include "citrus_mapper.h" 52 53#define _CITRUS_MAPPER_DIR "mapper.dir" 54 55#define CM_HASH_SIZE 101 56#define REFCOUNT_PERSISTENT -1 57 |
58static pthread_rwlock_t cm_lock = PTHREAD_RWLOCK_INITIALIZER; 59 |
|
58struct _citrus_mapper_area { 59 _CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE) ma_cache; 60 char *ma_dir; 61}; 62 63/* 64 * _citrus_mapper_create_area: 65 * create mapper area --- 4 unchanged lines hidden (view full) --- 70 struct _citrus_mapper_area *__restrict *__restrict rma, 71 const char *__restrict area) 72{ 73 struct _citrus_mapper_area *ma; 74 struct stat st; 75 char path[PATH_MAX]; 76 int ret; 77 | 60struct _citrus_mapper_area { 61 _CITRUS_HASH_HEAD(, _citrus_mapper, CM_HASH_SIZE) ma_cache; 62 char *ma_dir; 63}; 64 65/* 66 * _citrus_mapper_create_area: 67 * create mapper area --- 4 unchanged lines hidden (view full) --- 72 struct _citrus_mapper_area *__restrict *__restrict rma, 73 const char *__restrict area) 74{ 75 struct _citrus_mapper_area *ma; 76 struct stat st; 77 char path[PATH_MAX]; 78 int ret; 79 |
78 WLOCK; | 80 WLOCK(&cm_lock); |
79 80 if (*rma != NULL) { 81 ret = 0; 82 goto quit; 83 } 84 85 snprintf(path, (size_t)PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR); 86 --- 12 unchanged lines hidden (view full) --- 99 free(ma); 100 goto quit; 101 } 102 _CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE); 103 104 *rma = ma; 105 ret = 0; 106quit: | 81 82 if (*rma != NULL) { 83 ret = 0; 84 goto quit; 85 } 86 87 snprintf(path, (size_t)PATH_MAX, "%s/%s", area, _CITRUS_MAPPER_DIR); 88 --- 12 unchanged lines hidden (view full) --- 101 free(ma); 102 goto quit; 103 } 104 _CITRUS_HASH_INIT(&ma->ma_cache, CM_HASH_SIZE); 105 106 *rma = ma; 107 ret = 0; 108quit: |
107 UNLOCK; | 109 UNLOCK(&cm_lock); |
108 109 return (ret); 110} 111 112 113/* 114 * lookup_mapper_entry: 115 * lookup mapper.dir entry in the specified directory. --- 195 unchanged lines hidden (view full) --- 311{ 312 struct _citrus_mapper *cm; 313 char linebuf[PATH_MAX]; 314 const char *module, *variable; 315 int hashval, ret; 316 317 variable = NULL; 318 | 110 111 return (ret); 112} 113 114 115/* 116 * lookup_mapper_entry: 117 * lookup mapper.dir entry in the specified directory. --- 195 unchanged lines hidden (view full) --- 313{ 314 struct _citrus_mapper *cm; 315 char linebuf[PATH_MAX]; 316 const char *module, *variable; 317 int hashval, ret; 318 319 variable = NULL; 320 |
319 WLOCK; | 321 WLOCK(&cm_lock); |
320 321 /* search in the cache */ 322 hashval = hash_func(mapname); 323 _CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname, 324 hashval); 325 if (cm) { 326 /* found */ 327 cm->cm_refcount++; --- 4 unchanged lines hidden (view full) --- 332 333 /* search mapper entry */ 334 ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, 335 (size_t)PATH_MAX, &module, &variable); 336 if (ret) 337 goto quit; 338 339 /* open mapper */ | 322 323 /* search in the cache */ 324 hashval = hash_func(mapname); 325 _CITRUS_HASH_SEARCH(&ma->ma_cache, cm, cm_entry, match_func, mapname, 326 hashval); 327 if (cm) { 328 /* found */ 329 cm->cm_refcount++; --- 4 unchanged lines hidden (view full) --- 334 335 /* search mapper entry */ 336 ret = lookup_mapper_entry(ma->ma_dir, mapname, linebuf, 337 (size_t)PATH_MAX, &module, &variable); 338 if (ret) 339 goto quit; 340 341 /* open mapper */ |
340 UNLOCK; | 342 UNLOCK(&cm_lock); |
341 ret = mapper_open(ma, &cm, module, variable); | 343 ret = mapper_open(ma, &cm, module, variable); |
342 WLOCK; | 344 WLOCK(&cm_lock); |
343 if (ret) 344 goto quit; 345 cm->cm_key = strdup(mapname); 346 if (cm->cm_key == NULL) { 347 ret = errno; 348 _mapper_close(cm); 349 goto quit; 350 } 351 352 /* insert to the cache */ 353 cm->cm_refcount = 1; 354 _CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval); 355 356 *rcm = cm; 357 ret = 0; 358quit: | 345 if (ret) 346 goto quit; 347 cm->cm_key = strdup(mapname); 348 if (cm->cm_key == NULL) { 349 ret = errno; 350 _mapper_close(cm); 351 goto quit; 352 } 353 354 /* insert to the cache */ 355 cm->cm_refcount = 1; 356 _CITRUS_HASH_INSERT(&ma->ma_cache, cm, cm_entry, hashval); 357 358 *rcm = cm; 359 ret = 0; 360quit: |
359 UNLOCK; | 361 UNLOCK(&cm_lock); |
360 361 return (ret); 362} 363 364/* 365 * _citrus_mapper_close: 366 * close the specified mapper. 367 */ 368void 369_citrus_mapper_close(struct _citrus_mapper *cm) 370{ 371 372 if (cm) { | 362 363 return (ret); 364} 365 366/* 367 * _citrus_mapper_close: 368 * close the specified mapper. 369 */ 370void 371_citrus_mapper_close(struct _citrus_mapper *cm) 372{ 373 374 if (cm) { |
373 WLOCK; | 375 WLOCK(&cm_lock); |
374 if (cm->cm_refcount == REFCOUNT_PERSISTENT) 375 goto quit; 376 if (cm->cm_refcount > 0) { 377 if (--cm->cm_refcount > 0) 378 goto quit; 379 _CITRUS_HASH_REMOVE(cm, cm_entry); 380 free(cm->cm_key); 381 } 382 mapper_close(cm); 383quit: | 376 if (cm->cm_refcount == REFCOUNT_PERSISTENT) 377 goto quit; 378 if (cm->cm_refcount > 0) { 379 if (--cm->cm_refcount > 0) 380 goto quit; 381 _CITRUS_HASH_REMOVE(cm, cm_entry); 382 free(cm->cm_key); 383 } 384 mapper_close(cm); 385quit: |
384 UNLOCK; | 386 UNLOCK(&cm_lock); |
385 } 386} 387 388/* 389 * _citrus_mapper_set_persistent: 390 * set persistent count. 391 */ 392void 393_citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm) 394{ 395 | 387 } 388} 389 390/* 391 * _citrus_mapper_set_persistent: 392 * set persistent count. 393 */ 394void 395_citrus_mapper_set_persistent(struct _citrus_mapper * __restrict cm) 396{ 397 |
396 WLOCK; | 398 WLOCK(&cm_lock); |
397 cm->cm_refcount = REFCOUNT_PERSISTENT; | 399 cm->cm_refcount = REFCOUNT_PERSISTENT; |
398 UNLOCK; | 400 UNLOCK(&cm_lock); |
399} | 401} |