allocator.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "gear_config.h"
  37. #include <libgearman/common.h>
  38. #include <cstdlib>
  39. #include <cstring>
  40. #include <libgearman/universal.hpp>
  41. #include <libgearman/allocator.hpp>
  42. #include "util/memory.h"
  43. using namespace org::tangent;
  44. void *gearman_real_malloc(gearman_allocator_t& allocator, size_t size, const char *func, const char *file, int line)
  45. {
  46. void *ptr;
  47. if (allocator.malloc)
  48. {
  49. ptr= allocator.malloc(size, allocator.context);
  50. }
  51. else
  52. {
  53. ptr= malloc(size);
  54. }
  55. #if 0
  56. fprintf(stderr, "gearman_real_malloc(%s, %lu) : %p -> %s:%d\n", func, static_cast<unsigned long>(size), ptr, file, line);
  57. #else
  58. (void)func; (void)file; (void)line;
  59. #endif
  60. return ptr;
  61. }
  62. void *gearman_real_calloc(gearman_allocator_t& allocator, size_t nelem, size_t size, const char *func, const char *file, int line)
  63. {
  64. void *ptr;
  65. if (allocator.calloc)
  66. {
  67. ptr= allocator.calloc(nelem, size, allocator.context);
  68. }
  69. else if (allocator.malloc)
  70. {
  71. ptr= gearman_real_malloc(allocator, nelem * size, func, file, line);
  72. if (ptr)
  73. {
  74. memset(ptr, 0, nelem * size);
  75. }
  76. }
  77. else
  78. {
  79. ptr= calloc(nelem, size);
  80. }
  81. #if 0
  82. fprintf(stderr, "gearman_real_calloc(%s, %lu) : %p -> %s:%d\n", func, static_cast<unsigned long>(size), ptr, file, line);
  83. #else
  84. (void)func; (void)file; (void)line;
  85. #endif
  86. return ptr;
  87. }
  88. void *gearman_real_realloc(gearman_allocator_t& allocator, void *ptr, size_t size, const char *func, const char *file, int line)
  89. {
  90. void *new_ptr;
  91. if (allocator.realloc)
  92. {
  93. new_ptr= allocator.realloc(ptr, size, allocator.context);
  94. }
  95. else if (allocator.malloc)
  96. {
  97. new_ptr= NULL;
  98. }
  99. else
  100. {
  101. new_ptr= realloc(ptr, size);
  102. }
  103. #if 0
  104. fprintf(stderr, "gearman_real_realloc(%s, %lu) : %p -> %s:%d\n", func, static_cast<unsigned long>(size), ptr, file, line);
  105. #else
  106. (void)func; (void)file; (void)line;
  107. #endif
  108. return new_ptr;
  109. }
  110. void gearman_real_free(gearman_allocator_t& allocator, void *& ptr, const char *func, const char *file, int line)
  111. {
  112. #if 0
  113. fprintf(stderr, "gearman_real_free(%s) : %p -> %s:%d\n", func, ptr, file, line);
  114. #else
  115. (void)func; (void)file; (void)line;
  116. #endif
  117. if (ptr == NULL)
  118. {
  119. return;
  120. }
  121. if (allocator.free)
  122. {
  123. allocator.free(ptr, allocator.context);
  124. }
  125. else
  126. {
  127. util::free__(ptr);
  128. }
  129. ptr= NULL;
  130. }
  131. gearman_allocator_t gearman_default_allocator()
  132. {
  133. static gearman_allocator_t _defaults= { 0, 0, 0, 0, 0 };
  134. return _defaults;
  135. }
  136. gearman_return_t gearman_set_memory_allocator(gearman_allocator_t& allocator,
  137. gearman_malloc_fn *malloc_fn,
  138. gearman_free_fn *free_fn,
  139. gearman_realloc_fn *realloc_fn,
  140. gearman_calloc_fn *calloc_fn,
  141. void *context)
  142. {
  143. /* All should be set, or none should be set */
  144. if (malloc_fn == NULL and free_fn == NULL and realloc_fn == NULL and calloc_fn == NULL)
  145. {
  146. allocator= gearman_default_allocator();
  147. }
  148. else if (malloc_fn == NULL or free_fn == NULL or realloc_fn == NULL or calloc_fn == NULL)
  149. {
  150. return GEARMAN_FATAL;
  151. }
  152. else
  153. {
  154. allocator.malloc= malloc_fn;
  155. allocator.free= free_fn;
  156. allocator.realloc= realloc_fn;
  157. allocator.calloc= calloc_fn;
  158. allocator.context= context;
  159. }
  160. return GEARMAN_SUCCESS;
  161. }