home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / src / io / bmi / reference-list.c < prev    next >
C/C++ Source or Header  |  2008-11-19  |  5KB  |  279 lines

  1. /*
  2.  * (C) 2001 Clemson University and The University of Chicago
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7.  
  8. /*
  9.  * reference_list.c - functions to handle the creation and modification of 
  10.  * reference structures for the BMI layer
  11.  *
  12.  */
  13.  
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <assert.h>
  18.  
  19. #include "reference-list.h"
  20. #include "gossip.h"
  21. #include "id-generator.h"
  22. #include "quickhash.h"
  23.  
  24. static struct qhash_table* str_table = NULL;
  25. #define STR_TABLE_SIZE 137
  26.  
  27. /***************************************************************
  28.  * Visible functions
  29.  */
  30.  
  31. static int ref_list_compare_key_entry(void* key, struct qhash_head* link);
  32.  
  33. /*
  34.  * ref_list_new()
  35.  *
  36.  * creates a new reference list.
  37.  *
  38.  * returns pointer to an empty list or NULL on failure.
  39.  */
  40. ref_list_p ref_list_new(void)
  41. {
  42.  
  43.     ref_list_p tmp_list = NULL;
  44.  
  45.     /* There is currently never more than one reference list in BMI.  If we
  46.      * ever have a need for more, then this hash table should be moved from
  47.      * a static global to actually be part of the ref_list_p.
  48.      */
  49.     assert(str_table == NULL);
  50.  
  51.     str_table = qhash_init(
  52.         ref_list_compare_key_entry,
  53.         quickhash_string_hash, 
  54.         STR_TABLE_SIZE);
  55.  
  56.     if(!str_table)
  57.     {
  58.         return(NULL);
  59.     }
  60.  
  61.     tmp_list = (ref_list_p) malloc(sizeof(struct qlist_head));
  62.     if(!tmp_list)
  63.     {
  64.         qhash_finalize(str_table);
  65.         str_table = NULL;
  66.         return(NULL);
  67.     }
  68.  
  69.     INIT_QLIST_HEAD(tmp_list);
  70.     return (tmp_list);
  71. }
  72.  
  73. /*
  74.  * ref_list_add()
  75.  *
  76.  * adds a reference to the list.  
  77.  *
  78.  * no return value
  79.  */
  80. void ref_list_add(ref_list_p rlp,
  81.           ref_st_p rsp)
  82. {
  83.     if(rsp->id_string)
  84.     {
  85.         qhash_add(str_table, rsp->id_string, &rsp->hash_link);
  86.     }
  87.  
  88.     qlist_add(&(rsp->list_link), rlp);
  89. }
  90.  
  91. /*
  92.  * ref_list_search_addr()
  93.  *
  94.  * looks for a reference structure in the list that matches the given
  95.  * BMI_addr_t.
  96.  *
  97.  * returns a pointer to the structure on success, a NULL on failure.
  98.  */
  99. ref_st_p ref_list_search_addr(ref_list_p rlp,
  100.                   BMI_addr_t my_addr)
  101. {
  102.     return(id_gen_safe_lookup(my_addr));
  103. }
  104.  
  105.  
  106. /*
  107.  * ref_list_search_method_addr()
  108.  *
  109.  * looks for a reference structure in the list that matches the given
  110.  * method_addr_p.
  111.  *
  112.  * returns a pointer to the structure on success, NULL on failure.
  113.  */
  114. ref_st_p ref_list_search_method_addr(ref_list_p rlp,
  115.                      bmi_method_addr_p map)
  116. {
  117.     return(map->parent);
  118. }
  119.  
  120. /*
  121.  * ref_list_search_str()
  122.  *
  123.  * looks for a reference structure in the list that matches the given
  124.  * id string.
  125.  *
  126.  * returns a pointer to the structure on success, a NULL on failure.
  127.  */
  128. ref_st_p ref_list_search_str(ref_list_p rlp,
  129.                  const char *idstring)
  130. {
  131.  
  132.     struct qhash_head* tmp_link;
  133.  
  134.     tmp_link = qhash_search(str_table, (char*)idstring);
  135.     if(!tmp_link)
  136.     {
  137.         return(NULL);
  138.     }
  139.  
  140.     return(qlist_entry(tmp_link, ref_st, hash_link));
  141. }
  142.  
  143. /*
  144.  * ref_list_rem()
  145.  *
  146.  * removes the first match from the list - does not destroy it 
  147.  *
  148.  * returns a pointer to the structure on success, a NULL on failure.
  149.  */
  150. ref_st_p ref_list_rem(ref_list_p rlp,
  151.               BMI_addr_t my_addr)
  152. {
  153.     ref_st_p tmp_entry;
  154.     
  155.     tmp_entry = id_gen_safe_lookup(my_addr);
  156.  
  157.     if(tmp_entry)
  158.     {
  159.         qlist_del(&tmp_entry->list_link);
  160.  
  161.         if(tmp_entry->id_string)
  162.         {
  163.             qhash_del(&tmp_entry->hash_link);
  164.         }
  165.     }
  166.     return (tmp_entry);
  167. }
  168.  
  169.  
  170.  
  171. /*
  172.  * ref_list_cleanup()
  173.  *
  174.  * frees up the list and all data associated with it.
  175.  *
  176.  * no return values
  177.  */
  178. void ref_list_cleanup(ref_list_p rlp)
  179. {
  180.     ref_list_p tmp_link = NULL;
  181.     ref_list_p scratch = NULL;
  182.     ref_st_p tmp_entry = NULL;
  183.  
  184.     qlist_for_each_safe(tmp_link, scratch, rlp)
  185.     {
  186.     tmp_entry = qlist_entry(tmp_link, struct ref_st,
  187.                 list_link);
  188.         dealloc_ref_st(tmp_entry);
  189.     }
  190.  
  191.     qhash_finalize(str_table);
  192.     str_table = NULL;
  193.  
  194.     free(rlp);
  195.     return;
  196. }
  197.  
  198. /*
  199.  * alloc_ref_st()
  200.  *
  201.  * allocates storage for a reference struct.
  202.  *
  203.  * returns a pointer to the new structure on success, NULL on failure.
  204.  */
  205. ref_st_p alloc_ref_st(void)
  206. {
  207.  
  208.     int ssize = sizeof(struct ref_st);
  209.     ref_st_p new_ref = NULL;
  210.  
  211.     new_ref = (ref_st_p) malloc(ssize);
  212.     if (!new_ref)
  213.     {
  214.     return (NULL);
  215.     }
  216.  
  217.     memset(new_ref, 0, ssize);
  218.  
  219.     /* we can go ahead and set the bmi_addr here */
  220.     id_gen_safe_register(&(new_ref->bmi_addr), new_ref);
  221.  
  222.     return (new_ref);
  223. }
  224.  
  225. /*
  226.  * dealloc_ref_st()
  227.  *
  228.  * frees all memory associated with a reference structure
  229.  * NOTE: it *does not*, however, destroy the associated method address.
  230.  *
  231.  * returns 0 on success, -1 on failure
  232.  */
  233. void dealloc_ref_st(ref_st_p deadref)
  234. {
  235.  
  236.     if (!deadref)
  237.     {
  238.     return;
  239.     }
  240.  
  241.     if (deadref->id_string)
  242.     {
  243.     free(deadref->id_string);
  244.     }
  245.  
  246.     if (deadref->method_addr)
  247.     {
  248.     deadref->interface->set_info(BMI_DROP_ADDR, deadref->method_addr);
  249.     }
  250.  
  251.     id_gen_safe_unregister(deadref->bmi_addr);
  252.  
  253.     free(deadref);
  254. }
  255.  
  256. static int ref_list_compare_key_entry(void* key, struct qhash_head* link)
  257. {
  258.     char* key_string = (char*)key;
  259.     ref_st_p tmp_entry = NULL;
  260.  
  261.     tmp_entry = qhash_entry(link, ref_st, hash_link);
  262.     assert(tmp_entry);
  263.  
  264.     if(strcmp(tmp_entry->id_string, key_string) == 0)
  265.     {
  266.         return(1);
  267.     }
  268.     return(0);
  269. }
  270.  
  271. /*
  272.  * Local variables:
  273.  *  c-indent-level: 4
  274.  *  c-basic-offset: 4
  275.  * End:
  276.  *
  277.  * vim: ts=8 sts=4 sw=4 expandtab
  278.  */
  279.