shm_regex.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2009 iptelorg GmbH
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * History
  28. * -------
  29. * 2009-04-03 Initial version (Miklos)
  30. */
  31. #include <malloc.h> /* hook prototypes */
  32. #include "../../mem/shm_mem.h"
  33. #include "shm_regex.h"
  34. typedef void *(malloc_hook_t) (size_t, const void *);
  35. typedef void *(realloc_hook_t) (void *, size_t, const void *);
  36. typedef void (free_hook_t) (void *, const void *);
  37. /* The memory hooks are overwritten before calling regcomp(), regfree(),
  38. * and regexec(), and shared memory function are called
  39. * from the hooks instead of libc malloc/realloc/free.
  40. */
  41. static void *shm_malloc_hook(size_t size, const void *caller)
  42. {
  43. return shm_malloc (size);
  44. }
  45. static void *shm_realloc_hook(void *p, size_t size, const void *caller)
  46. {
  47. return shm_realloc (p, size);
  48. }
  49. static void shm_free_hook(void *ptr, const void *caller)
  50. {
  51. if (ptr) shm_free (ptr);
  52. }
  53. #define replace_malloc_hooks() \
  54. do { \
  55. orig_malloc_hook = __malloc_hook; \
  56. orig_realloc_hook = __realloc_hook; \
  57. orig_free_hook = __free_hook; \
  58. __malloc_hook = shm_malloc_hook; \
  59. __realloc_hook = shm_realloc_hook; \
  60. __free_hook = shm_free_hook; \
  61. } while (0)
  62. #define restore_malloc_hooks() \
  63. do { \
  64. __malloc_hook = orig_malloc_hook; \
  65. __realloc_hook = orig_realloc_hook; \
  66. __free_hook = orig_free_hook; \
  67. } while (0)
  68. int shm_regcomp(regex_t *preg, const char *regex, int cflags)
  69. {
  70. malloc_hook_t *orig_malloc_hook;
  71. realloc_hook_t *orig_realloc_hook;
  72. free_hook_t *orig_free_hook;
  73. int ret;
  74. replace_malloc_hooks();
  75. ret = regcomp(preg, regex, cflags);
  76. restore_malloc_hooks();
  77. return ret;
  78. }
  79. void shm_regfree(regex_t *preg)
  80. {
  81. malloc_hook_t *orig_malloc_hook;
  82. realloc_hook_t *orig_realloc_hook;
  83. free_hook_t *orig_free_hook;
  84. replace_malloc_hooks();
  85. regfree(preg);
  86. restore_malloc_hooks();
  87. }
  88. int shm_regexec(const regex_t *preg, const char *string, size_t nmatch,
  89. regmatch_t pmatch[], int eflags)
  90. {
  91. malloc_hook_t *orig_malloc_hook;
  92. realloc_hook_t *orig_realloc_hook;
  93. free_hook_t *orig_free_hook;
  94. int ret;
  95. /* regexec() allocates some memory for the pattern buffer
  96. * when it is successfully called for the first time, therefore
  97. * shared memory is required also here.
  98. * The drawback is that shared memory allocation is also used
  99. * needlessly for allocating the temporary space for
  100. * the elements of pmatch. -- Does not happen if pmatch and
  101. * nmatch are 0.
  102. * It is safe to call regexec() concurrently without locking,
  103. * because regexec() has its own locks.
  104. * (Miklos)
  105. */
  106. replace_malloc_hooks();
  107. ret = regexec(preg, string, nmatch,
  108. pmatch, eflags);
  109. restore_malloc_hooks();
  110. return ret;
  111. }