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 / bmi-method-support.c < prev    next >
C/C++ Source or Header  |  2007-11-06  |  5KB  |  216 lines

  1. /*
  2.  * (C) 2001 Clemson University and The University of Chicago
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7. /* These are some support functions shared across the
  8.  * various BMI methods.
  9.  */
  10.  
  11. #include <string.h>
  12.  
  13. #include "bmi-types.h"
  14. #include "bmi-method-support.h"
  15. #include "id-generator.h"
  16. #include "reference-list.h"
  17.  
  18.  
  19. /*
  20.  * alloc_method_op()
  21.  *
  22.  * allocates storage for an operation info struct.
  23.  *
  24.  * returns a pointer to the new structure on success, NULL on failure.
  25.  */
  26. method_op_p bmi_alloc_method_op(bmi_size_t payload_size)
  27. {
  28.  
  29.     /* we are going to allocate the full operation structure as a
  30.      * contiguous region.  The method_data pointer is going to point to
  31.      * the memory address immediately following the method_addr
  32.      * structure.
  33.      */
  34.     method_op_p my_method_op = NULL;
  35.     int ssize = sizeof(method_op_st);
  36.     /* generic component */
  37.     my_method_op = (method_op_p) malloc(ssize + payload_size);
  38.     if (!my_method_op)
  39.     {
  40.     return (NULL);
  41.     }
  42.     memset(my_method_op, 0, (ssize + payload_size));
  43.  
  44.     id_gen_fast_register(&(my_method_op->op_id), my_method_op);
  45.  
  46.     my_method_op->error_code = 0;
  47.     if (payload_size == 0)
  48.     {
  49.     my_method_op->method_data = NULL;
  50.     }
  51.     else
  52.     {
  53.     my_method_op->method_data = (char *) my_method_op + ssize;
  54.     }
  55.  
  56.     return (my_method_op);
  57. }
  58.  
  59. /* 
  60.  * dealloc_method_op()
  61.  *
  62.  * frees the memory allocated to an method_op structure 
  63.  * 
  64.  * no return value
  65.  */
  66. void bmi_dealloc_method_op(method_op_p op_p)
  67. {
  68.     id_gen_fast_unregister(op_p->op_id);
  69.     free(op_p);
  70.     op_p = NULL;
  71.     return;
  72. }
  73.  
  74.  
  75. /*
  76.  * alloc_method_addr()
  77.  * 
  78.  * alloc_method_addr is used to generate a basic method_addr struct and
  79.  * initialize it correctly.
  80.  *
  81.  * Returns a pointer to an allocated method_addr struct on success,
  82.  * NULL on failure.
  83.  */
  84. struct bmi_method_addr *bmi_alloc_method_addr(int method_type,
  85.                       bmi_size_t payload_size)
  86. {
  87.  
  88.     /* we are going to allocate the full address structure as a
  89.      * contiguous region.  The method_data pointer is going to point to
  90.      * the memory address immediately following the method_addr
  91.      * structure.
  92.      */
  93.     struct bmi_method_addr *my_method_addr = NULL;
  94.     int ssize = sizeof(struct bmi_method_addr);
  95.     /* generic component */
  96.     my_method_addr = (struct bmi_method_addr *) malloc(ssize + payload_size);
  97.     if (!my_method_addr)
  98.     {
  99.     return (NULL);
  100.     }
  101.     memset(my_method_addr, 0, (ssize + payload_size));
  102.     my_method_addr->method_type = method_type;
  103.  
  104.     my_method_addr->method_data = (char *) my_method_addr + ssize;
  105.  
  106.     return (my_method_addr);
  107. }
  108.  
  109. /*
  110.  * dealloc_method_addr()
  111.  *
  112.  * used to deallocate a method_addr structure safely.  mainly used by
  113.  * list management functions.  MAKE SURE that any method specific
  114.  * freeing has been handled before calling this function.  It is
  115.  * oblivious to this.
  116.  * 
  117.  * no return value
  118.  */
  119. void bmi_dealloc_method_addr(bmi_method_addr_p my_method_addr)
  120. {
  121.     free(my_method_addr);
  122.     my_method_addr = NULL;
  123.     return;
  124. }
  125.  
  126.  
  127. /*
  128.  * string_key()
  129.  *
  130.  * string_key is used to create a new string which is a subset of the
  131.  * given id string.  The substring is a complete method address field
  132.  * that begins right after <key>:// and ends with a null terminator.
  133.  * The function strips all whitespace, commas, and address fields which do
  134.  * not match the key.  
  135.  *
  136.  * Boy, I sure do hate writing code to parse strings...
  137.  * 
  138.  * returns a pointer to the new string on success, NULL on failure.
  139.  */
  140. char *string_key(const char *key,
  141.          const char *id_string)
  142. {
  143.  
  144.     const char *holder = NULL;
  145.     const char *end = NULL;
  146.     char *newkey = NULL;
  147.     char *retstring = NULL;
  148.     int keysize = 0;
  149.     int strsize = 0;
  150.     int retsize = 0;
  151.  
  152.     if ((!id_string) || (!key))
  153.     {
  154.     return (NULL);
  155.     }
  156.     keysize = strlen(key);
  157.     strsize = strlen(id_string);
  158.  
  159.     /* create a new key of the form <key>:// */
  160.     if ((newkey = (char *) malloc(keysize + 4)) == NULL)
  161.     {
  162.     return (NULL);
  163.     }
  164.     strcpy(newkey, key);
  165.     strcat(newkey, "://");
  166.  
  167.     holder = id_string;
  168.  
  169.     holder = strstr(holder, newkey);
  170.     /* first match */
  171.     if (holder)
  172.     {
  173.     end = strpbrk(holder, ", \t\n");
  174.     if (end)
  175.     {
  176.         end = end;    /* stop on terminator */
  177.     }
  178.     else
  179.     {
  180.         end = id_string + strsize;    /* go to the end of the id string (\0) */
  181.     }
  182.     /* move holder so it doesn't include the opening key and deliminator */
  183.     holder = holder + keysize + 3;
  184.     }
  185.     else
  186.     {
  187.     /* no match */
  188.     free(newkey);
  189.     return (NULL);
  190.     }
  191.  
  192.     /* figure out how long our substring is */
  193.     retsize = (end - holder);
  194.     if ((retstring = (char *) malloc(retsize + 1)) == NULL)
  195.     {
  196.     free(newkey);
  197.     return (NULL);
  198.     }
  199.  
  200.     /* copy it out */
  201.     strncpy(retstring, holder, retsize);
  202.     retstring[retsize] = '\0';
  203.  
  204.     free(newkey);
  205.     return (retstring);
  206. }
  207.  
  208. /*
  209.  * Local variables:
  210.  *  c-indent-level: 4
  211.  *  c-basic-offset: 4
  212.  * End:
  213.  *
  214.  * vim: ts=8 sts=4 sw=4 expandtab
  215.  */
  216.