NCDMethodIndex.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <ncd/NCDStringIndex.h>
  35. #define NCDMETHODINDEX_NUM_EXPECTED_METHOD_NAMES 64
  36. #define NCDMETHODINDEX_NUM_EXPECTED_ENTRIES 64
  37. struct NCDMethodIndex__method_name {
  38. char *method_name;
  39. int first_entry;
  40. int hash_next;
  41. };
  42. struct NCDMethodIndex__entry {
  43. NCD_string_id_t obj_type;
  44. const struct NCDInterpModule *module;
  45. int next;
  46. };
  47. typedef struct NCDMethodIndex__method_name NCDMethodIndex__hashentry;
  48. typedef const char *NCDMethodIndex__hashkey;
  49. typedef struct NCDMethodIndex__method_name *NCDMethodIndex__hasharg;
  50. #include "NCDMethodIndex_hash.h"
  51. #include <structure/CHash_decl.h>
  52. /**
  53. * The method index associates (object_type, method_name) pairs to pointers
  54. * to corresponding \link NCDInterpModule structures (whose type strings would
  55. * be "object_type::method_name").
  56. * More precisely, the method names are represented as indices into an
  57. * internal array, which allows very efficient lookup when the method names
  58. * are known in advance, but not the object types.
  59. */
  60. typedef struct {
  61. struct NCDMethodIndex__method_name *names;
  62. struct NCDMethodIndex__entry *entries;
  63. int names_capacity;
  64. int entries_capacity;
  65. int num_names;
  66. int num_entries;
  67. NCDMethodIndex__Hash hash;
  68. NCDStringIndex *string_index;
  69. } NCDMethodIndex;
  70. /**
  71. * Initializes the method index.
  72. *
  73. * @return 1 on success, 0 on failure
  74. */
  75. int NCDMethodIndex_Init (NCDMethodIndex *o, NCDStringIndex *string_index) WARN_UNUSED;
  76. /**
  77. * Frees the method index.
  78. */
  79. void NCDMethodIndex_Free (NCDMethodIndex *o);
  80. /**
  81. * Adds a method to the index.
  82. * Duplicate methods will not be detected here.
  83. *
  84. * @param obj_type object type of method, e.g. "cat" in "cat::meow".
  85. * Must not be NULL. Does not have to be null-terminated.
  86. * @param obj_type_len number of characters in obj_type
  87. * @param method_name name of method, e.g. "meow" in "cat::meow".
  88. * Must not be NULL.
  89. * @param module pointer to module structure. Must not be NULL.
  90. * @return on success, a non-negative identifier; on failure, -1
  91. */
  92. int NCDMethodIndex_AddMethod (NCDMethodIndex *o, const char *obj_type, size_t obj_type_len, const char *method_name, const struct NCDInterpModule *module);
  93. /**
  94. * Removes a method from the index.
  95. *
  96. * @param method_name_id method name identifier
  97. */
  98. void NCDMethodIndex_RemoveMethod (NCDMethodIndex *o, int method_name_id);
  99. /**
  100. * Obtains an internal integer identifier for a method name. The intention is that
  101. * this is stored and later passed to \link NCDMethodIndex_GetMethodModule for
  102. * efficient lookup of modules corresponding to methods.
  103. *
  104. * @param method_name name of method, e.g. "meow" in "cat::meow".
  105. * Must not be NULL.
  106. * @return non-negative integer on success, -1 on failure
  107. */
  108. int NCDMethodIndex_GetMethodNameId (NCDMethodIndex *o, const char *method_name);
  109. /**
  110. * Looks up the module corresponding to a method. The method name is passed as an
  111. * identifier obtained from \link NCDMethodIndex_GetMethodNameId.
  112. *
  113. * @param obj_type object type of method, e.g. "cat" in "cat::meow", as a string
  114. * identifier via {@link NCDStringIndex}
  115. * @param method_name_id method name identifier. Must have been previously returned
  116. * by a successfull call of \link NCDMethodIndex_GetMethodNameId.
  117. * @return module pointer, or NULL if no such method exists
  118. */
  119. const struct NCDInterpModule * NCDMethodIndex_GetMethodModule (NCDMethodIndex *o, NCD_string_id_t obj_type, int method_name_id);
  120. #endif