tree.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * $Id: tree.c 4518 2008-07-28 15:39:28Z henningw $
  3. *
  4. * Copyright (C) 2006 Voice Sistem SRL
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. *
  22. *
  23. * History:
  24. * ---------
  25. * 2006-09-08 first version (bogdan)
  26. */
  27. /*!
  28. * \file
  29. * \brief MI :: Tree
  30. * \ingroup mi
  31. */
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <errno.h>
  35. #include "../../dprint.h"
  36. #include "mi_mem.h"
  37. #include "tree.h"
  38. #include "fmt.h"
  39. static int use_shm = 0;
  40. struct mi_root *init_mi_tree(unsigned int code, char *reason, int reason_len)
  41. {
  42. struct mi_root *root;
  43. if (use_shm)
  44. root = (struct mi_root *)shm_malloc(sizeof(struct mi_root));
  45. else
  46. root = (struct mi_root *)mi_malloc(sizeof(struct mi_root));
  47. if (!root) {
  48. LM_ERR("no more pkg mem\n");
  49. return NULL;
  50. }
  51. memset(root,0,sizeof(struct mi_root));
  52. root->node.next = root->node.last = &root->node;
  53. if (reason && reason_len) {
  54. root->reason.s = reason;
  55. root->reason.len = reason_len;
  56. }
  57. root->code = code;
  58. return root;
  59. }
  60. static void free_mi_node(struct mi_node *parent)
  61. {
  62. struct mi_node *p, *q;
  63. for(p = parent->kids ; p ; ){
  64. q = p;
  65. p = p->next;
  66. free_mi_node(q);
  67. }
  68. if (use_shm) {
  69. shm_free(parent);
  70. } else {
  71. del_mi_attr_list(parent);
  72. mi_free(parent);
  73. }
  74. }
  75. void free_mi_tree(struct mi_root *parent)
  76. {
  77. struct mi_node *p, *q;
  78. for(p = parent->node.kids ; p ; ){
  79. q = p;
  80. p = p->next;
  81. free_mi_node(q);
  82. }
  83. if (use_shm)
  84. shm_free(parent);
  85. else
  86. mi_free(parent);
  87. }
  88. static inline struct mi_node *create_mi_node(char *name, int name_len,
  89. char *value, int value_len, int flags)
  90. {
  91. struct mi_node *new;
  92. int size_mem;
  93. int name_pos;
  94. int value_pos;
  95. if (!name) name_len=0;
  96. if (!name_len) name=0;
  97. if (!value) value_len=0;
  98. if (!value_len) value=0;
  99. if (!name && !value)
  100. return NULL;
  101. size_mem = sizeof(struct mi_node);
  102. value_pos = name_pos = 0;
  103. if (name && (flags & MI_DUP_NAME)){
  104. name_pos = size_mem;
  105. size_mem += name_len;
  106. }
  107. if (value && (flags & MI_DUP_VALUE)){
  108. value_pos = size_mem;
  109. size_mem += value_len;
  110. }
  111. if (use_shm)
  112. new = (struct mi_node *)shm_malloc(size_mem);
  113. else
  114. new = (struct mi_node *)mi_malloc(size_mem);
  115. if(!new) {
  116. LM_ERR("no more pkg mem\n");
  117. return NULL;
  118. }
  119. memset(new,0,size_mem);
  120. if (name) {
  121. new->name.len = name_len;
  122. if(flags & MI_DUP_NAME){
  123. new->name.s = ((char *)new) + name_pos;
  124. strncpy(new->name.s, name, name_len);
  125. } else{
  126. new->name.s = name;
  127. }
  128. }
  129. if (value) {
  130. new->value.len = value_len;
  131. if(flags & MI_DUP_VALUE){
  132. new->value.s = ((char *)new) + value_pos;
  133. strncpy(new->value.s, value, value_len);
  134. }else{
  135. new->value.s = value;
  136. }
  137. }
  138. new->last = new;
  139. return new;
  140. }
  141. static inline struct mi_node *add_next(struct mi_node *brother,
  142. char *name, int name_len, char *value, int value_len, int flags)
  143. {
  144. struct mi_node *new;
  145. if(!brother)
  146. return NULL;
  147. new = create_mi_node(name, name_len, value, value_len, flags);
  148. if(!new)
  149. return NULL;
  150. brother->last->next = new;
  151. brother->last = new;
  152. return new;
  153. }
  154. struct mi_node *add_mi_node_sibling( struct mi_node *brother, int flags,
  155. char *name, int name_len, char *value, int value_len)
  156. {
  157. return add_next(brother, name, name_len, value, value_len, flags);
  158. }
  159. struct mi_node *addf_mi_node_sibling(struct mi_node *brother, int flags,
  160. char *name, int name_len, char *fmt_val, ...)
  161. {
  162. va_list ap;
  163. char *p;
  164. int len;
  165. va_start(ap, fmt_val);
  166. p = mi_print_fmt( fmt_val, ap, &len);
  167. va_end(ap);
  168. if (p==NULL)
  169. return 0;
  170. return add_mi_node_sibling( brother, flags|MI_DUP_VALUE,
  171. name, name_len, p, len);
  172. }
  173. struct mi_node *add_mi_node_child( struct mi_node *parent, int flags,
  174. char *name, int name_len, char *value, int value_len)
  175. {
  176. if(parent->kids){
  177. return add_next(parent->kids, name, name_len, value, value_len, flags);
  178. }else{
  179. parent->kids = create_mi_node(name, name_len, value, value_len, flags);
  180. return parent->kids;
  181. }
  182. }
  183. struct mi_node *addf_mi_node_child(struct mi_node *parent, int flags,
  184. char *name, int name_len, char *fmt_val, ...)
  185. {
  186. va_list ap;
  187. char *p;
  188. int len;
  189. va_start(ap, fmt_val);
  190. p = mi_print_fmt( fmt_val, ap, &len);
  191. va_end(ap);
  192. if (p==NULL)
  193. return 0;
  194. return add_mi_node_child( parent, flags|MI_DUP_VALUE,
  195. name, name_len, p, len);
  196. }
  197. static int clone_mi_node(struct mi_node *org, struct mi_node *parent)
  198. {
  199. struct mi_node *p, *q;
  200. for(p = org->kids ; p ; p=p->next){
  201. q = add_mi_node_child( parent, MI_DUP_VALUE|MI_DUP_NAME,
  202. p->name.s, p->name.len, p->value.s, p->value.len);
  203. if (q==NULL)
  204. return -1;
  205. if (clone_mi_node( p, q)!=0)
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. struct mi_root* clone_mi_tree(struct mi_root *org, int shm)
  211. {
  212. struct mi_root *root;
  213. use_shm = shm?1:0;
  214. root = init_mi_tree( org->code, org->reason.s, org->reason.len);
  215. if (root==NULL)
  216. goto done;
  217. if (clone_mi_node( &(org->node), &(root->node) )!=0 ) {
  218. free_mi_tree(root);
  219. root = NULL;
  220. goto done;
  221. }
  222. done:
  223. use_shm=0;
  224. return root;
  225. }
  226. void free_shm_mi_tree(struct mi_root *parent)
  227. {
  228. use_shm = 1;
  229. free_mi_tree(parent);
  230. use_shm = 0;
  231. }