NCDMethodIndex.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file NCDMethodIndex.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. 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. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_NCDMETHODINDEX_H
  30. #define BADVPN_NCDMETHODINDEX_H
  31. #include <misc/debug.h>
  32. #include <structure/CHash.h>
  33. #include <ncd/NCDModule.h>
  34. #define NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES 64
  35. #define NCDMETHODINDEX_NUM_EXPECTED_ENTRIES 64
  36. struct NCDMethodIndex__method_name {
  37. char *method_name;
  38. int first_entry;
  39. int hash_next;
  40. };
  41. struct NCDMethodIndex__entry {
  42. char *obj_type;
  43. const struct NCDModule *module;
  44. int next;
  45. };
  46. typedef struct NCDMethodIndex__method_name NCDMethodIndex__hashentry;
  47. typedef const char *NCDMethodIndex__hashkey;
  48. typedef struct NCDMethodIndex__method_name *NCDMethodIndex__hasharg;
  49. #include "NCDMethodIndex_hash.h"
  50. #include <structure/CHash_decl.h>
  51. /**
  52. * The method index associates (object_type, method_name) pairs to pointers
  53. * to corresponding \link NCDModule structures (whose type strings would
  54. * be "object_type::method_name").
  55. * More precisely, the method names are represented as indices into an
  56. * internal array, which allows very efficient lookup when the method names
  57. * are known in advance, but not the object types.
  58. */
  59. typedef struct {
  60. struct NCDMethodIndex__method_name *names;
  61. struct NCDMethodIndex__entry *entries;
  62. int names_capacity;
  63. int entries_capacity;
  64. int num_names;
  65. int num_entries;
  66. NCDMethodIndex__Hash hash;
  67. } NCDMethodIndex;
  68. /**
  69. * Initializes the method index.
  70. *
  71. * @return 1 on success, 0 on failure
  72. */
  73. int NCDMethodIndex_Init (NCDMethodIndex *o) WARN_UNUSED;
  74. /**
  75. * Frees the method index.
  76. */
  77. void NCDMethodIndex_Free (NCDMethodIndex *o);
  78. /**
  79. * Adds a method to the index.
  80. * Duplicate methods will not be detected here.
  81. *
  82. * @param obj_type object type of method, e.g. "cat" in "cat::meow".
  83. * Must not be NULL.
  84. * @param method_name name of method, e.g. "meow" in "cat::meow".
  85. * Must not be NULL.
  86. * @param module pointer to module structure. Must not be NULL.
  87. * @return 1 on success, 0 on failure
  88. */
  89. int NCDMethodIndex_AddMethod (NCDMethodIndex *o, const char *obj_type, const char *method_name, const struct NCDModule *module) WARN_UNUSED;
  90. /**
  91. * Obtains an internal integer identifier for a method name. The intention is that
  92. * this is stored and later passed to \link NCDMethodIndex_GetMethodModule for
  93. * efficient lookup of modules corresponding to methods.
  94. *
  95. * @param method_name name of method, e.g. "meow" in "cat::meow".
  96. * Must not be NULL.
  97. * @return non-negative integer on success, -1 on failure
  98. */
  99. int NCDMethodIndex_GetMethodNameId (NCDMethodIndex *o, const char *method_name);
  100. /**
  101. * Looks up the module corresponding to a method. The method name is passed as an
  102. * identifier obtained from \link NCDMethodIndex_GetMethodNameId.
  103. *
  104. * @param obj_type object type of method, e.g. "cat" in "cat::meow".
  105. * Must not be NULL.
  106. * @param method_name_id method name identifier. Must have been previously returned
  107. * by a successfull call of \link NCDMethodIndex_GetMethodNameId.
  108. * @return module pointer, or NULL if no such method exists
  109. */
  110. const struct NCDModule * NCDMethodIndex_GetMethodModule (NCDMethodIndex *o, const char *obj_type, int method_name_id);
  111. #endif