Subversion Repositories HelenOS

Rev

Rev 2883 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup sctrace
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <ipc/ipc.h>
  38. #include <libadt/hash_table.h>
  39.  
  40. #include "proto.h"
  41.  
  42. #define SRV_PROTO_TABLE_CHAINS 32
  43. #define METHOD_OPER_TABLE_CHAINS 32
  44.  
  45. hash_table_t srv_proto;
  46.  
  47. typedef struct {
  48.     int srv;
  49.     proto_t *proto;
  50.     link_t link;
  51. } srv_proto_t;
  52.  
  53. typedef struct {
  54.     ipcarg_t method;
  55.     oper_t *oper;
  56.     link_t link;
  57. } method_oper_t;
  58.  
  59. hash_index_t srv_proto_hash(unsigned long key[])
  60. {
  61.     return key[0] % SRV_PROTO_TABLE_CHAINS;
  62. }
  63.  
  64. int srv_proto_compare(unsigned long key[], hash_count_t keys,
  65.     link_t *item)
  66. {
  67.     srv_proto_t *sp;
  68.  
  69.     sp = hash_table_get_instance(item, srv_proto_t, link);
  70.  
  71.     return key[0] == sp->srv;
  72. }
  73.  
  74. void srv_proto_remove_callback(link_t *item)
  75. {
  76. }
  77.  
  78. hash_table_operations_t srv_proto_ops = {
  79.     .hash = srv_proto_hash,
  80.     .compare = srv_proto_compare,
  81.     .remove_callback = srv_proto_remove_callback
  82. };
  83.  
  84. hash_index_t method_oper_hash(unsigned long key[])
  85. {
  86.     return key[0] % METHOD_OPER_TABLE_CHAINS;
  87. }
  88.  
  89. int method_oper_compare(unsigned long key[], hash_count_t keys,
  90.     link_t *item)
  91. {
  92.     method_oper_t *mo;
  93.  
  94.     mo = hash_table_get_instance(item, method_oper_t, link);
  95.  
  96.     return key[0] == mo->method;
  97. }
  98.  
  99. void method_oper_remove_callback(link_t *item)
  100. {
  101. }
  102.  
  103. hash_table_operations_t method_oper_ops = {
  104.     .hash = method_oper_hash,
  105.     .compare = method_oper_compare,
  106.     .remove_callback = method_oper_remove_callback
  107. };
  108.  
  109.  
  110. void proto_init(void)
  111. {
  112.     hash_table_create(&srv_proto, SRV_PROTO_TABLE_CHAINS, 1,
  113.         &srv_proto_ops);
  114. }
  115.  
  116. void proto_cleanup(void)
  117. {
  118.     hash_table_destroy(&srv_proto);
  119. }
  120.  
  121. void proto_register(int srv, proto_t *proto)
  122. {
  123.     srv_proto_t *sp;
  124.     unsigned long key;
  125.  
  126.     sp = malloc(sizeof(srv_proto_t));
  127.     sp->srv = srv;
  128.     sp->proto = proto;
  129.     key = srv;
  130.  
  131.     hash_table_insert(&srv_proto, &key, &sp->link);
  132. }
  133.  
  134. proto_t *proto_get_by_srv(int srv)
  135. {
  136.     unsigned long key;
  137.     link_t *item;
  138.     srv_proto_t *sp;
  139.  
  140.     key = srv;
  141.     item = hash_table_find(&srv_proto, &key);
  142.     if (item == NULL) return NULL;
  143.  
  144.     sp = hash_table_get_instance(item, srv_proto_t, link);
  145.     return sp->proto;
  146. }
  147.  
  148. static void proto_struct_init(proto_t *proto, char *name)
  149. {
  150.     proto->name = name;
  151.     hash_table_create(&proto->method_oper, SRV_PROTO_TABLE_CHAINS, 1,
  152.         &method_oper_ops);
  153. }
  154.  
  155. proto_t *proto_new(char *name)
  156. {
  157.     proto_t *p;
  158.  
  159.     p = malloc(sizeof(proto_t));
  160.     proto_struct_init(p, name);
  161.  
  162.     return p;
  163. }
  164.  
  165. void proto_add_oper(proto_t *proto, int method, oper_t *oper)
  166. {
  167.     method_oper_t *mo;
  168.     unsigned long key;
  169.  
  170.     mo = malloc(sizeof(method_oper_t));
  171.     mo->method = method;
  172.     mo->oper = oper;
  173.     key = method;
  174.  
  175.     hash_table_insert(&proto->method_oper, &key, &mo->link);   
  176. }
  177.  
  178. oper_t *proto_get_oper(proto_t *proto, int method)
  179. {
  180.     unsigned long key;
  181.     link_t *item;
  182.     method_oper_t *mo;
  183.  
  184.     key = method;
  185.     item = hash_table_find(&proto->method_oper, &key);
  186.     if (item == NULL) return NULL;
  187.  
  188.     mo = hash_table_get_instance(item, method_oper_t, link);
  189.     return mo->oper;
  190. }
  191.  
  192. static void oper_struct_init(oper_t *oper, char *name)
  193. {
  194.     oper->name = name;
  195. }
  196.  
  197. oper_t *oper_new(char *name)
  198. {
  199.     oper_t *o;
  200.  
  201.     o = malloc(sizeof(oper_t));
  202.     oper_struct_init(o, name);
  203.  
  204.     return o;
  205. }
  206.  
  207. /** @}
  208.  */
  209.