memory.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2008-2010 Stefan Krah. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "mpdecimal.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "typearith.h"
  31. #include "memory.h"
  32. mpd_ssize_t MPD_MINALLOC = MPD_MINALLOC_MIN;
  33. void *(* mpd_mallocfunc)(size_t size) = malloc;
  34. void *(* mpd_reallocfunc)(void *ptr, size_t size) = realloc;
  35. void *(* mpd_callocfunc)(size_t nmemb, size_t size) = calloc;
  36. void (* mpd_free)(void *ptr) = free;
  37. /* emulate calloc if it is not available */
  38. void *
  39. mpd_callocfunc_em(size_t nmemb, size_t size)
  40. {
  41. void *ptr;
  42. size_t req;
  43. #if MPD_SIZE_MAX < SIZE_MAX
  44. if (nmemb > MPD_SIZE_MAX || size > MPD_SIZE_MAX) {
  45. return NULL;
  46. }
  47. #endif
  48. req = mul_size_t((mpd_size_t)nmemb, (mpd_size_t)size);
  49. if ((ptr = mpd_mallocfunc(req)) == NULL) {
  50. return NULL;
  51. }
  52. /* used on uint32_t or uint64_t */
  53. memset(ptr, 0, req);
  54. return ptr;
  55. }
  56. /* malloc with overflow checking */
  57. void *
  58. mpd_alloc(mpd_size_t nmemb, mpd_size_t size)
  59. {
  60. void *ptr;
  61. mpd_size_t req;
  62. req = mul_size_t(nmemb, size);
  63. if ((ptr = mpd_mallocfunc(req)) == NULL) {
  64. return NULL;
  65. }
  66. return ptr;
  67. }
  68. /* calloc with overflow checking */
  69. void *
  70. mpd_calloc(mpd_size_t nmemb, mpd_size_t size)
  71. {
  72. void *ptr;
  73. if ((ptr = mpd_callocfunc(nmemb, size)) == NULL) {
  74. return NULL;
  75. }
  76. return ptr;
  77. }
  78. /* realloc with overflow checking */
  79. void *
  80. mpd_realloc(void *ptr, mpd_size_t nmemb, mpd_size_t size, uint8_t *err)
  81. {
  82. void *new;
  83. mpd_size_t req;
  84. req = mul_size_t(nmemb, size);
  85. if ((new = mpd_reallocfunc(ptr, req)) == NULL) {
  86. *err = 1;
  87. return ptr;
  88. }
  89. return new;
  90. }
  91. /* struct hack malloc with overflow checking */
  92. void *
  93. mpd_sh_alloc(mpd_size_t struct_size, mpd_size_t nmemb, mpd_size_t size)
  94. {
  95. void *ptr;
  96. mpd_size_t req;
  97. req = mul_size_t(nmemb, size);
  98. req = add_size_t(req, struct_size);
  99. if ((ptr = mpd_mallocfunc(req)) == NULL) {
  100. return NULL;
  101. }
  102. return ptr;
  103. }
  104. /* Allocate a new decimal with data-size 'size'.
  105. * In case of an error the return value is NULL.
  106. */
  107. mpd_t *
  108. mpd_qnew_size(mpd_ssize_t size)
  109. {
  110. mpd_t *result;
  111. size = (size < MPD_MINALLOC) ? MPD_MINALLOC : size;
  112. if ((result = mpd_alloc(1, sizeof *result)) == NULL) {
  113. return NULL;
  114. }
  115. if ((result->data = mpd_alloc(size, sizeof *result->data)) == NULL) {
  116. mpd_free(result);
  117. return NULL;
  118. }
  119. result->flags = 0;
  120. result->exp = 0;
  121. result->digits = 0;
  122. result->len = 0;
  123. result->alloc = size;
  124. return result;
  125. }
  126. /* Allocate a new decimal with data-size MPD_MINALLOC.
  127. * In case of an error the return value is NULL.
  128. */
  129. mpd_t *
  130. mpd_qnew(void)
  131. {
  132. return mpd_qnew_size(MPD_MINALLOC);
  133. }
  134. /* Allocate new decimal. Caller can check for NULL or MPD_Malloc_error.
  135. * Raises on error.
  136. */
  137. mpd_t *
  138. mpd_new(mpd_context_t *ctx)
  139. {
  140. mpd_t *result;
  141. if ((result = mpd_qnew()) == NULL) {
  142. mpd_addstatus_raise(ctx, MPD_Malloc_error);
  143. }
  144. return result;
  145. }
  146. int
  147. mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t size, uint32_t *status)
  148. {
  149. mpd_uint_t *p = result->data;
  150. if ((result->data = mpd_alloc(size, sizeof *result->data)) == NULL) {
  151. result->data = p;
  152. mpd_set_qnan(result);
  153. mpd_set_positive(result);
  154. result->exp = result->digits = result->len = 0;
  155. *status |= MPD_Malloc_error;
  156. return 0;
  157. }
  158. memcpy(result->data, p, result->len * (sizeof *result->data));
  159. result->alloc = size;
  160. mpd_set_dynamic_data(result);
  161. return 1;
  162. }
  163. int
  164. mpd_switch_to_dyn_zero(mpd_t *result, mpd_ssize_t size, uint32_t *status)
  165. {
  166. mpd_uint_t *p = result->data;
  167. if ((result->data = mpd_calloc(size, sizeof *result->data)) == NULL) {
  168. result->data = p;
  169. mpd_set_qnan(result);
  170. mpd_set_positive(result);
  171. result->exp = result->digits = result->len = 0;
  172. *status |= MPD_Malloc_error;
  173. return 0;
  174. }
  175. result->alloc = size;
  176. mpd_set_dynamic_data(result);
  177. return 1;
  178. }
  179. int
  180. mpd_realloc_dyn(mpd_t *result, mpd_ssize_t size, uint32_t *status)
  181. {
  182. uint8_t err = 0;
  183. result->data = mpd_realloc(result->data, size, sizeof *result->data, &err);
  184. if (!err) {
  185. result->alloc = size;
  186. }
  187. else if (size > result->alloc) {
  188. mpd_set_qnan(result);
  189. mpd_set_positive(result);
  190. result->exp = result->digits = result->len = 0;
  191. *status |= MPD_Malloc_error;
  192. return 0;
  193. }
  194. return 1;
  195. }