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 / description / pint-dist-utils.c < prev    next >
C/C++ Source or Header  |  2010-12-21  |  7KB  |  244 lines

  1. /*
  2.  * (C) 2002 Clemson University and The University of Chicago.
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "pvfs2-dist-simple-stripe.h"
  11. #include "pvfs2-dist-varstrip.h"
  12. #include "pvfs2-dist-twod-stripe.h"
  13. #include "pint-dist-utils.h"
  14.  
  15. /* Default distributions */
  16. extern PINT_dist basic_dist;
  17. extern PINT_dist simple_stripe_dist;
  18. extern PINT_dist varstrip_dist;
  19. extern PINT_dist twod_stripe_dist;
  20.  
  21. /* Struct for determining how to set a distribution parameter by name */
  22. typedef struct PINT_dist_param_offset_s
  23. {
  24.     char* dist_name;
  25.     char* param_name;
  26.     size_t offset;
  27.     size_t size;
  28. } PINT_dist_param_offset;
  29.  
  30. /* Dist param table */
  31. static PINT_dist_param_offset* PINT_dist_param_table = 0;
  32. static size_t PINT_dist_param_table_entries = 0;
  33. static size_t PINT_dist_param_table_size = 0;
  34. static int PINT_dist_param_table_alloc_inc = 10;
  35.  
  36. /* Return a pointer to the dist param offset for this parameter, or null
  37.  *  if none exists
  38.  */
  39. static PINT_dist_param_offset* PINT_get_param_offset(const char* dist_name,
  40.                                                      const char* param_name)
  41. {
  42.     PINT_dist_param_offset* dpo = 0;
  43.     if (0 != PINT_dist_param_table)
  44.     {
  45.         int i;
  46.         for (i = 0; i < PINT_dist_param_table_entries; i++)
  47.         {
  48.             dpo = PINT_dist_param_table + i;
  49.             if (0 == strcmp(dpo->dist_name, dist_name) &&
  50.                 0 == strcmp(dpo->param_name, param_name))
  51.             {
  52.                 return dpo;
  53.             }
  54.         }
  55.     }
  56.     return NULL;
  57. }
  58.  
  59. /* PINT_dist_initialize implementation */
  60. int PINT_dist_initialize(server_configuration_s* server_config)
  61. {
  62.     int ret = 0;
  63.     
  64.     /* Register the basic distribution */
  65.     PINT_register_distribution(&basic_dist);    
  66.     
  67.     /* Register the varstrip distribution */
  68.     PINT_register_distribution(&varstrip_dist);
  69.     
  70.     /* Register the simple stripe distribution */
  71.     PINT_register_distribution(&simple_stripe_dist);    
  72.  
  73.     /* Register the twod stripe distribution */
  74.     PINT_register_distribution(&twod_stripe_dist);
  75.  
  76.     /* add an associated unregister to any new distributions */
  77.     return ret;
  78. }
  79.  
  80. /* PINT_dist_finalize implementation */
  81. void PINT_dist_finalize(void)
  82. {
  83.     PINT_unregister_distribution(basic_dist.dist_name);
  84.     PINT_unregister_distribution(varstrip_dist.dist_name);
  85.     PINT_unregister_distribution(simple_stripe_dist.dist_name);
  86.     PINT_unregister_distribution(twod_stripe_dist.dist_name);
  87.  
  88.     free(PINT_dist_param_table);
  89.     PINT_dist_param_table = 0;
  90.     PINT_dist_param_table_size = 0;
  91. }
  92.  
  93. /*  PINT_dist_default_get_num_dfiles implementation */
  94. int PINT_dist_default_get_num_dfiles(void* params,
  95.                                      uint32_t num_servers_requested,
  96.                                      uint32_t num_dfiles_requested)
  97. {
  98.     int dfiles;
  99.     if (0 < num_dfiles_requested)
  100.     {
  101.         dfiles = num_dfiles_requested;
  102.     }
  103.     else
  104.     {
  105.         dfiles = num_servers_requested;
  106.     }
  107.     return dfiles;
  108. }
  109.  
  110. /*  PINT_dist_default_set_param implementation */
  111. int PINT_dist_default_set_param(const char* dist_name, void* params,
  112.                                 const char* param_name, void* value)
  113. {
  114.     int rc = 0;
  115.     PINT_dist_param_offset* offset_data;
  116.     offset_data = PINT_get_param_offset(dist_name, param_name);
  117.     if (0 != offset_data)
  118.     {
  119.         memcpy(((char *)params) + offset_data->offset, 
  120.                value, offset_data->size);
  121.     }
  122.     else
  123.     {
  124.         rc = -1;
  125.     }
  126.     return rc;
  127. }
  128.  
  129. int PINT_dist_register_param_offset(const char* dist_name,
  130.                                     const char* param_name,
  131.                                     size_t offset,
  132.                                     size_t field_size)
  133. {
  134.     int dist_len, param_len;
  135.     
  136.     /* Increase the size of the param table if its full */
  137.     if (PINT_dist_param_table_entries >= PINT_dist_param_table_size)
  138.     {
  139.         PINT_dist_param_offset* buf;
  140.         int new_table_size = PINT_dist_param_table_size +
  141.             PINT_dist_param_table_alloc_inc;
  142.  
  143.         buf = malloc(new_table_size * sizeof(PINT_dist_param_offset));
  144.         if (0 != buf)
  145.         {
  146.             if(PINT_dist_param_table_size)
  147.             {
  148.                 memcpy(buf, PINT_dist_param_table,
  149.                        PINT_dist_param_table_size * sizeof(PINT_dist_param_offset));
  150.             }
  151.             memset(buf + PINT_dist_param_table_size, 0,
  152.                    PINT_dist_param_table_alloc_inc * sizeof(PINT_dist_param_offset));
  153.  
  154.             if(PINT_dist_param_table_size)
  155.             {
  156.                 free(PINT_dist_param_table);
  157.             }
  158.             PINT_dist_param_table_size = new_table_size;
  159.             PINT_dist_param_table = buf;
  160.         }
  161.         else
  162.         {
  163.             return -1;
  164.         }
  165.     }
  166.  
  167.     /* Allocate memory for the dist and param name */
  168.     dist_len = strlen(dist_name) + 1;
  169.     param_len = strlen(param_name) + 1;
  170.     PINT_dist_param_table[PINT_dist_param_table_entries].dist_name =
  171.         malloc(dist_len);
  172.     if (0 == PINT_dist_param_table[PINT_dist_param_table_entries].dist_name)
  173.     {
  174.         return -1;
  175.     }
  176.     
  177.     PINT_dist_param_table[PINT_dist_param_table_entries].param_name =
  178.         malloc(param_len);
  179.     if (0 == PINT_dist_param_table[PINT_dist_param_table_entries].param_name)
  180.     {
  181.         return -1;
  182.     }    
  183.     
  184.     /* Register the parameter information for later lookup */
  185.     strncpy(PINT_dist_param_table[PINT_dist_param_table_entries].dist_name,
  186.            dist_name, dist_len);
  187.     strncpy(PINT_dist_param_table[PINT_dist_param_table_entries].param_name,
  188.            param_name, param_len);
  189.     PINT_dist_param_table[PINT_dist_param_table_entries].offset = offset;
  190.     PINT_dist_param_table[PINT_dist_param_table_entries].size = field_size;
  191.     PINT_dist_param_table_entries += 1;
  192.     return 0;
  193. }
  194.  
  195. int PINT_dist_unregister_param_offset(const char *dist_name, 
  196.                                       const char *param_name)
  197. {
  198.     int i = 0;
  199.  
  200.     if( !dist_name || !param_name )
  201.     {
  202.         return -EINVAL;
  203.     }
  204.  
  205.     int dlen = strlen(dist_name) + 1;
  206.     int plen = strlen(param_name) + 1;
  207.  
  208.     for( i = 0; i < PINT_dist_param_table_entries; i++ )
  209.     {
  210.         if((strncmp(PINT_dist_param_table[i].dist_name, dist_name, dlen)==0) && 
  211.            (strncmp(PINT_dist_param_table[i].param_name, param_name, plen)==0))
  212.         {
  213.             /* found dist and param to unregister */
  214.             if( PINT_dist_param_table[i].dist_name )
  215.             {
  216.                 free(PINT_dist_param_table[i].dist_name);
  217.             }
  218.             if( PINT_dist_param_table[i].param_name )
  219.             {
  220.                 free(PINT_dist_param_table[i].param_name);
  221.             }
  222.             
  223.             /* bubble up, not sure I like this but it is just an array */
  224.             --PINT_dist_param_table_entries;
  225.             for( ; i < PINT_dist_param_table_entries; i++ )
  226.             {
  227.                 PINT_dist_param_table[i] = PINT_dist_param_table[i+1];
  228.             }
  229.         }
  230.     }
  231.  
  232.     return 0;
  233. }
  234.  
  235. /*
  236.  * Local variables:
  237.  *  mode: c
  238.  *  c-indent-level: 4
  239.  *  c-basic-offset: 4
  240.  * End:
  241.  *
  242.  * vim: ft=c ts=8 sts=4 sw=4 expandtab
  243.  */
  244.