allocator.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearman library
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include <libgearman/common.h>
  37. #include <cstdlib>
  38. #include <cstring>
  39. #include <libgearman/universal.hpp>
  40. #include <libgearman/allocator.hpp>
  41. void *gearman_real_malloc(gearman_allocator_t& allocator, size_t size, const char *func, const char *file, int line)
  42. {
  43. void *ptr;
  44. if (allocator.malloc)
  45. {
  46. ptr= allocator.malloc(size, allocator.context);
  47. }
  48. else
  49. {
  50. ptr= malloc(size);
  51. }
  52. #if 0
  53. fprintf(stderr, "gearman_real_malloc(%s, %lu) : %p -> %s:%d\n", func, static_cast<unsigned long>(size), ptr, file, line);
  54. #else
  55. (void)func; (void)file; (void)line;
  56. #endif
  57. return ptr;
  58. }
  59. void *gearman_real_calloc(gearman_allocator_t& allocator, size_t nelem, size_t size, const char *func, const char *file, int line)
  60. {
  61. void *ptr;
  62. if (allocator.calloc)
  63. {
  64. ptr= allocator.calloc(nelem, size, allocator.context);
  65. }
  66. else if (allocator.malloc)
  67. {
  68. ptr= gearman_real_malloc(allocator, nelem * size, func, file, line);
  69. if (not ptr)
  70. memset(ptr, 0, nelem * size);
  71. }
  72. else
  73. {
  74. ptr= calloc(nelem, size);
  75. }
  76. #if 0
  77. fprintf(stderr, "gearman_real_calloc(%s, %lu) : %p -> %s:%d\n", func, static_cast<unsigned long>(size), ptr, file, line);
  78. #else
  79. (void)func; (void)file; (void)line;
  80. #endif
  81. return ptr;
  82. }
  83. void *gearman_real_realloc(gearman_allocator_t& allocator, void *ptr, size_t size, const char *func, const char *file, int line)
  84. {
  85. void *new_ptr;
  86. if (allocator.realloc)
  87. {
  88. new_ptr= allocator.realloc(ptr, size, allocator.context);
  89. }
  90. else if (allocator.malloc)
  91. {
  92. new_ptr= NULL;
  93. }
  94. else
  95. {
  96. new_ptr= realloc(ptr, size);
  97. }
  98. #if 0
  99. fprintf(stderr, "gearman_real_realloc(%s, %lu) : %p -> %s:%d\n", func, static_cast<unsigned long>(size), ptr, file, line);
  100. #else
  101. (void)func; (void)file; (void)line;
  102. #endif
  103. return new_ptr;
  104. }
  105. void gearman_real_free(gearman_allocator_t& allocator, void *ptr, const char *func, const char *file, int line)
  106. {
  107. #if 0
  108. fprintf(stderr, "gearman_real_free(%s) : %p -> %s:%d\n", func, ptr, file, line);
  109. #else
  110. (void)func; (void)file; (void)line;
  111. #endif
  112. if (not ptr)
  113. return;
  114. if (allocator.free)
  115. {
  116. allocator.free(ptr, allocator.context);
  117. }
  118. else
  119. {
  120. free(ptr);
  121. }
  122. }
  123. gearman_allocator_t gearman_default_allocator()
  124. {
  125. static gearman_allocator_t _defaults= { 0, 0, 0, 0, 0 };
  126. return _defaults;
  127. }
  128. gearman_return_t gearman_set_memory_allocator(gearman_allocator_t& allocator,
  129. gearman_malloc_fn *malloc_fn,
  130. gearman_free_fn *free_fn,
  131. gearman_realloc_fn *realloc_fn,
  132. gearman_calloc_fn *calloc_fn,
  133. void *context)
  134. {
  135. /* All should be set, or none should be set */
  136. if (malloc_fn == NULL and free_fn == NULL and realloc_fn == NULL and calloc_fn == NULL)
  137. {
  138. allocator= gearman_default_allocator();
  139. }
  140. else if (malloc_fn == NULL or free_fn == NULL or realloc_fn == NULL or calloc_fn == NULL)
  141. {
  142. return GEARMAN_FATAL;
  143. }
  144. else
  145. {
  146. allocator.malloc= malloc_fn;
  147. allocator.free= free_fn;
  148. allocator.realloc= realloc_fn;
  149. allocator.calloc= calloc_fn;
  150. allocator.context= context;
  151. }
  152. return GEARMAN_SUCCESS;
  153. }