db_gen.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG FOKUS
  5. * Copyright (C) 2006-2007 iptelorg GmbH
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #ifndef _DB_GEN_H
  29. #define _DB_GEN_H 1
  30. /** \ingroup DB_API
  31. * @{
  32. */
  33. #include "db_drv.h"
  34. #include "../../str.h"
  35. #include "../../list.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif /* __cplusplus */
  39. /*
  40. * Declare a list of DB API structures with given structure
  41. * name
  42. */
  43. #define DBLIST_HEAD(name) \
  44. STAILQ_HEAD(name, db_gen)
  45. /*
  46. * All structures that ought to be members of the DB API
  47. * linked lists must have this element as the _first_
  48. * element in the structure
  49. */
  50. #define DBLIST_ENTRY \
  51. STAILQ_ENTRY(db_gen) next
  52. /*
  53. * Initialize the static head of linked lists of DB API
  54. * structures
  55. */
  56. #define DBLIST_INITIALIZER(head) \
  57. STAILQ_HEAD_INITIALIZER(head)
  58. /*
  59. * Initialize the head of the list
  60. */
  61. #define DBLIST_INIT(head) \
  62. STAILQ_INIT(head)
  63. #define DBLIST_FIRST(head) SLIST_FIRST(head)
  64. /*
  65. * Insert a new DB API structure at the beginning of the
  66. * linked list
  67. */
  68. #define DBLIST_INSERT_HEAD(head, elem) \
  69. STAILQ_INSERT_HEAD((head), (struct db_gen*)(elem), next)
  70. /*
  71. * Add an element at the tail of the list
  72. */
  73. #define DBLIST_INSERT_TAIL(head, elem) \
  74. STAILQ_INSERT_TAIL((head), ((struct db_gen*)(elem)), next)
  75. /*
  76. * Remove a given structure from a linked list of DB API
  77. * structures
  78. */
  79. #define DBLIST_REMOVE(head, elem) \
  80. STAILQ_REMOVE(head, (struct db_gen*)(elem), db_gen, next)
  81. /*
  82. * Remove a given structure from a linked list of DB API
  83. * structures
  84. */
  85. #define DBLIST_REMOVE_HEAD(head) \
  86. STAILQ_REMOVE_HEAD(head, next)
  87. /*
  88. * Iterate through the elements of the list, store
  89. * the pointer to the current element in var variable
  90. */
  91. /*
  92. * FIXME: We should find some other way of doing this than just copying
  93. * and pasting the code from STAILQ_FOREACH
  94. */
  95. #define DBLIST_FOREACH(var, head) \
  96. for((var) = (void*)STAILQ_FIRST((head)); \
  97. (var); \
  98. (var) = (void*)STAILQ_NEXT(((struct db_gen*)(var)), next))
  99. /*
  100. * Iterate through the elements of the list, the pointer
  101. * to the current element is stored in var variable, this
  102. * is the safe version of the macro which allows you to
  103. * remove the element from the list. tvar is a temporary
  104. * variable for internal use by the macro
  105. */
  106. /*
  107. * FIXME: We should find some other way of doing this than just copying
  108. * and pasting the code from STAILQ_FOREACH_SAFE
  109. */
  110. #define DBLIST_FOREACH_SAFE(var, head, tvar) \
  111. for ((var) = (void*)STAILQ_FIRST((head)); \
  112. (var) && ((tvar) = (void*)STAILQ_NEXT(((struct db_gen*)(var)), next), 1); \
  113. (var) = (tvar))
  114. /*
  115. * Maximum number of payload structures that can be attached to
  116. * any DB API structure at a time.
  117. */
  118. #define DB_PAYLOAD_MAX 16
  119. struct db_drv;
  120. /*
  121. * Template for generic data structures defined in the
  122. * DB API. Drivers can cast structure pointers to this to
  123. * obtain the pointer to driver specific data
  124. *
  125. * All variables and attributes to be shared across all DB API
  126. * structures should be put into this structure. This structure
  127. * is at the beginnning of each DB API structure to ensure that
  128. * all DB API structures share some common variables.
  129. */
  130. typedef struct db_gen {
  131. DBLIST_ENTRY;
  132. /* Array of pointers to driver-specific data. The database API
  133. * supports access to multiple databases at the same time and each
  134. * database driver may want to append some data to generic DB structures,
  135. * hence an array. The current position in the array is stored
  136. * in db_data_idx
  137. */
  138. struct db_drv* data[DB_PAYLOAD_MAX];
  139. } db_gen_t;
  140. /*
  141. * Global variable holding the current index of the payload of the driver that
  142. * is being executed. DB API is responsible for setting this vaiable before
  143. * calling functions of DB drivers.
  144. */
  145. extern int db_payload_idx;
  146. /*
  147. * Macros to set/get variable (DB driver specific)
  148. * payload to/from generic DB API structures
  149. */
  150. /*
  151. * Attach a driver specific data structure to a generic
  152. * DB API structure
  153. */
  154. #define DB_SET_PAYLOAD(db_struct, drv_data) do { \
  155. ((struct db_gen*)(db_struct))->data[db_payload_idx] = (struct db_drv*)(drv_data); \
  156. } while(0)
  157. /*
  158. * Return a driver specific data structure
  159. */
  160. #define DB_GET_PAYLOAD(db_struct) \
  161. ((void*)(((struct db_gen*)(db_struct))->data[db_payload_idx]))
  162. /*
  163. * Initialize a db_gen structure and make space for the data
  164. * from n database drivers
  165. */
  166. int db_gen_init(struct db_gen* gen);
  167. /*
  168. * Free all memory allocated by a db_gen structure
  169. */
  170. void db_gen_free(struct db_gen* gen);
  171. #ifdef __cplusplus
  172. }
  173. #endif /* __cplusplus */
  174. /** @} */
  175. #endif /* _DB_GEN_H */