mem.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of sip-router, a free SIP server.
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. *
  19. * History:
  20. * --------
  21. * 2003-04-08 init_mallocs split into init_{pkg,shm}_malloc (andrei)
  22. *
  23. */
  24. /**
  25. * \file
  26. * \brief Main definitions for memory manager
  27. *
  28. * Main definitions for memory manager, like malloc, free and realloc
  29. * \ingroup mem
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include "../config.h"
  34. #include "../dprint.h"
  35. #include "../globals.h"
  36. #include "mem.h"
  37. #ifdef PKG_MALLOC
  38. #include "q_malloc.h"
  39. #endif
  40. #ifdef SHM_MEM
  41. #include "shm_mem.h"
  42. #endif
  43. #ifdef PKG_MALLOC
  44. #ifndef DL_MALLOC
  45. char* mem_pool = 0;
  46. #endif
  47. #ifdef F_MALLOC
  48. struct fm_block* mem_block = 0;
  49. #elif defined DL_MALLOC
  50. /* don't need this */
  51. #else
  52. struct qm_block* mem_block = 0;
  53. #endif
  54. #endif
  55. /**
  56. * \brief Initialize private memory pool
  57. * \return 0 if the memory allocation was successful, -1 otherwise
  58. */
  59. int init_pkg_mallocs(void)
  60. {
  61. #ifdef PKG_MALLOC
  62. /*init mem*/
  63. #ifndef DL_MALLOC
  64. if (pkg_mem_size == 0)
  65. pkg_mem_size = PKG_MEM_POOL_SIZE;
  66. mem_pool = malloc(pkg_mem_size);
  67. #endif
  68. #ifdef F_MALLOC
  69. if (mem_pool)
  70. mem_block=fm_malloc_init(mem_pool, pkg_mem_size);
  71. #elif DL_MALLOC
  72. /* don't need this */
  73. #else
  74. if (mem_pool)
  75. mem_block=qm_malloc_init(mem_pool, pkg_mem_size);
  76. #endif
  77. #ifndef DL_MALLOC
  78. if (mem_block==0){
  79. LOG(L_CRIT, "could not initialize memory pool\n");
  80. fprintf(stderr, "Too much pkg memory demanded: %ld bytes\n",
  81. pkg_mem_size);
  82. return -1;
  83. }
  84. #endif
  85. #endif
  86. return 0;
  87. }
  88. /**
  89. * \brief Destroy private memory pool
  90. */
  91. void destroy_pkg_mallocs(void)
  92. {
  93. #ifdef PKG_MALLOC
  94. #ifndef DL_MALLOC
  95. if (mem_pool) {
  96. free(mem_pool);
  97. mem_pool = 0;
  98. }
  99. #endif
  100. #endif /* PKG_MALLOC */
  101. }
  102. /**
  103. * \brief Initialize shared memory pool
  104. * \param force_alloc Force allocation of memory, e.g. initialize complete block with zero
  105. * \return 0 if the memory allocation was successful, -1 otherwise
  106. */
  107. int init_shm_mallocs(int force_alloc)
  108. {
  109. #ifdef SHM_MEM
  110. if (shm_mem_init(force_alloc)<0) {
  111. LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
  112. fprintf(stderr, "Too much shared memory demanded: %ld\n",
  113. shm_mem_size );
  114. return -1;
  115. }
  116. #endif
  117. return 0;
  118. }