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-distribution.c < prev    next >
C/C++ Source or Header  |  2010-12-21  |  6KB  |  235 lines

  1. /*
  2.  * (C) 2002 Clemson University.
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */       
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <assert.h>
  11.  
  12. #define __PINT_REQPROTO_ENCODE_FUNCS_C
  13. #include "pvfs2-types.h"
  14. #include "pvfs2-debug.h"
  15. #include "gossip.h"
  16. #include "pint-distribution.h"
  17.  
  18.  
  19.  
  20. /* global size of dist table */
  21. #define PINT_DIST_TABLE_SZ 8
  22. static int PINT_Dist_count = 0; 
  23.  
  24. /* compiled-in distributions */
  25. extern PINT_dist basic_dist;
  26. extern PINT_dist simple_stripe_dist;
  27.  
  28. /* initial dist table - default dists */
  29. static PINT_dist *PINT_Dist_table[PINT_DIST_TABLE_SZ] = {0};
  30.  
  31. /*
  32.  * add a dist to dist table
  33.  */
  34. int PINT_register_distribution(PINT_dist *d_p)
  35. {
  36.     if (0 != d_p &&
  37.         PINT_Dist_count < PINT_DIST_TABLE_SZ)
  38.     {
  39.         /* Register dist */
  40.         PINT_Dist_table[PINT_Dist_count++] = d_p;
  41.  
  42.         /* Perform dist specific registration code */
  43.         d_p->methods->registration_init(d_p->params);
  44.         
  45.         return (0);
  46.     }
  47.     return (-1);
  48. }
  49.  
  50. /*
  51.  * remove a dist from dist table
  52.  */
  53. int PINT_unregister_distribution(char *dist_name)
  54. {
  55.     int d;
  56.     if (!dist_name)
  57.         return -1;
  58.     for (d = 0; d < PINT_Dist_count && PINT_Dist_table[d]; d++)
  59.     {
  60.         if (strncmp(dist_name, PINT_Dist_table[d]->dist_name,
  61.                     PINT_DIST_NAME_SZ) == 0)
  62.         {
  63.             PINT_Dist_table[d]->methods->unregister();
  64.             /* bubble up */
  65.             --PINT_Dist_count;
  66.             for (; d<PINT_Dist_count; d++)
  67.                 PINT_Dist_table[d] = PINT_Dist_table[d+1];
  68.  
  69.             /* clean out the last entry we just moved up */
  70.             PINT_Dist_table[d] = NULL;
  71.             return (0);
  72.         }
  73.     }
  74.     return (-1);
  75. }
  76.  
  77. /*
  78.  * Looks up a distribution and copies it into a contiguous memory region.
  79.  * This is similar to PVFS_Dist_copy, but it copies from the static table,
  80.  * not from another contiguous region.
  81.  */
  82. PINT_dist *PINT_dist_create(const char *name)
  83. {
  84.     PINT_dist old_dist;
  85.     PINT_dist *new_dist = 0;
  86.  
  87.     if (!name)
  88.     return 0;
  89.     old_dist.dist_name = (char*)name;
  90.     old_dist.params = 0;
  91.     old_dist.methods = 0;
  92.     if (PINT_dist_lookup(&old_dist) == 0)
  93.     {
  94.     /* distribution was found */
  95.     new_dist = malloc(PINT_DIST_PACK_SIZE(&old_dist));
  96.     if (new_dist)
  97.     {
  98.         *new_dist = old_dist;
  99.         new_dist->dist_name
  100.           = (char *) new_dist + roundup8(sizeof(*new_dist));
  101.         new_dist->params
  102.           = (void *)(new_dist->dist_name + roundup8(new_dist->name_size));
  103.             /* after lookup there must be enough room to hold name */
  104.             assert(old_dist.name_size >= (strlen(old_dist.dist_name) + 1));
  105.             /* copy using length of string passed in by caller
  106.              * rather than rounded up name_size used for distribution packing
  107.              */
  108.         memcpy(new_dist->dist_name, old_dist.dist_name,
  109.           (strlen(old_dist.dist_name) + 1));
  110.         memcpy(new_dist->params, old_dist.params, old_dist.param_size);
  111.         /* leave methods pointing to same static functions */
  112.     }
  113.     }
  114.     return new_dist;
  115. }
  116.  
  117. int PINT_dist_free(PINT_dist *dist)
  118. {
  119.     if (dist)
  120.     {
  121.         /* assumes this is a dist created from above */
  122.         free(dist);
  123.         return 0;
  124.     }
  125.     return -1;
  126. }
  127.  
  128. PINT_dist* PINT_dist_copy(const PINT_dist *dist)
  129. {
  130.     int dist_size;
  131.     PINT_dist *new_dist;
  132.  
  133.     if (!dist)
  134.     {
  135.         return NULL;
  136.     }
  137.     dist_size = PINT_DIST_PACK_SIZE(dist);
  138.     new_dist = (PINT_dist *)malloc(dist_size);
  139.     if (new_dist)
  140.     {
  141.         memcpy(new_dist, dist, dist_size);
  142.         /* fixup pointers to new space */
  143.         new_dist->dist_name
  144.           = (char *) new_dist + roundup8(sizeof(*new_dist));
  145.         new_dist->params
  146.           = (void *)(new_dist->dist_name + roundup8(new_dist->name_size));
  147.                 memcpy(new_dist->dist_name, dist->dist_name,
  148.                        dist->name_size);
  149.                 memcpy(new_dist->params, dist->params, dist->param_size);
  150.     }
  151.     return (new_dist);
  152. }
  153.  
  154. int PINT_dist_getparams(void *buf, const PINT_dist *dist)
  155. {
  156.     if (!dist || !buf)
  157.     {
  158.         return -1;
  159.     }
  160.     memcpy (buf, dist->params, dist->param_size);
  161.     return 0;
  162. }
  163.  
  164. int PINT_dist_setparams(PINT_dist *dist, const void *buf)
  165. {
  166.     if (!dist || !buf)
  167.     {
  168.         return -1;
  169.     }
  170.     memcpy (dist->params, buf, dist->param_size);
  171.     return 0;
  172. }
  173.  
  174. /*
  175.  * pass in a dist with a valid name, looks up in dist table
  176.  * if found, fills in the missing parts of the structure
  177.  */
  178. int PINT_dist_lookup(PINT_dist *dist)
  179. {
  180.     int d;
  181.     if (!dist || !dist->dist_name)
  182.         return -1;
  183.     for (d = 0; d < PINT_Dist_count && PINT_Dist_table[d]; d++)
  184.     {
  185.         if (!strncmp(dist->dist_name, PINT_Dist_table[d]->dist_name,
  186.                     PINT_DIST_NAME_SZ))
  187.         {
  188.             dist->name_size = PINT_Dist_table[d]->name_size;
  189.             dist->param_size = PINT_Dist_table[d]->param_size;
  190.             if (!dist->params)
  191.                 dist->params = PINT_Dist_table[d]->params;
  192.             if (!dist->methods)
  193.                 dist->methods = PINT_Dist_table[d]->methods;
  194.             return 0; /* success */
  195.         }
  196.     }
  197.     return -1;
  198. }
  199.  
  200. /* pack dist struct for storage */
  201. void PINT_dist_encode(void *buffer, PINT_dist *dist)
  202. {
  203.     char * tmpbuf = (char *)buffer;
  204.     encode_PINT_dist(&tmpbuf, &dist);
  205. }
  206.  
  207. /* unpack dist struct after receiving from storage */
  208. void PINT_dist_decode(PINT_dist **dist, void *buffer)
  209. {
  210.     char * tmpbuf = (char *)buffer;
  211.     decode_PINT_dist(&tmpbuf, dist);
  212. }
  213.  
  214. void PINT_dist_dump(PINT_dist *dist)
  215. {
  216.     gossip_debug(GOSSIP_DIST_DEBUG,"******************************\n");
  217.     gossip_debug(GOSSIP_DIST_DEBUG,"address\t\t%p\n", dist);
  218.     gossip_debug(GOSSIP_DIST_DEBUG,"dist_name\t%s\n", dist->dist_name);
  219.     gossip_debug(GOSSIP_DIST_DEBUG,"name_size\t%d\n", dist->name_size);
  220.     gossip_debug(GOSSIP_DIST_DEBUG,"param_size\t%d\n", dist->param_size);
  221.     gossip_debug(GOSSIP_DIST_DEBUG,"params\t\t%p\n", dist->params);
  222.     gossip_debug(GOSSIP_DIST_DEBUG,"methods\t\t%p\n", dist->methods);
  223.     gossip_debug(GOSSIP_DIST_DEBUG,"******************************\n");
  224. }
  225.  
  226. /*
  227.  * Local variables:
  228.  *  mode: c
  229.  *  c-indent-level: 4
  230.  *  c-basic-offset: 4
  231.  * End:
  232.  *
  233.  * vim: ft=c ts=8 sts=4 sw=4 expandtab
  234.  */
  235.