memory.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2008-2016 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. /* Guaranteed minimum allocation for a coefficient. May be changed once
  33. at program start using mpd_setminalloc(). */
  34. mpd_ssize_t MPD_MINALLOC = MPD_MINALLOC_MIN;
  35. /* Custom allocation and free functions */
  36. void *(* mpd_mallocfunc)(size_t size) = malloc;
  37. void *(* mpd_reallocfunc)(void *ptr, size_t size) = realloc;
  38. void *(* mpd_callocfunc)(size_t nmemb, size_t size) = calloc;
  39. void (* mpd_free)(void *ptr) = free;
  40. /* emulate calloc if it is not available */
  41. void *
  42. mpd_callocfunc_em(size_t nmemb, size_t size)
  43. {
  44. void *ptr;
  45. size_t req;
  46. mpd_size_t overflow;
  47. #if MPD_SIZE_MAX < SIZE_MAX
  48. /* full_coverage test only */
  49. if (nmemb > MPD_SIZE_MAX || size > MPD_SIZE_MAX) {
  50. return NULL;
  51. }
  52. #endif
  53. req = mul_size_t_overflow((mpd_size_t)nmemb, (mpd_size_t)size,
  54. &overflow);
  55. if (overflow) {
  56. return NULL;
  57. }
  58. ptr = mpd_mallocfunc(req);
  59. if (ptr == NULL) {
  60. return NULL;
  61. }
  62. /* used on uint32_t or uint64_t */
  63. memset(ptr, 0, req);
  64. return ptr;
  65. }
  66. /* malloc with overflow checking */
  67. void *
  68. mpd_alloc(mpd_size_t nmemb, mpd_size_t size)
  69. {
  70. mpd_size_t req, overflow;
  71. req = mul_size_t_overflow(nmemb, size, &overflow);
  72. if (overflow) {
  73. return NULL;
  74. }
  75. return mpd_mallocfunc(req);
  76. }
  77. /* calloc with overflow checking */
  78. void *
  79. mpd_calloc(mpd_size_t nmemb, mpd_size_t size)
  80. {
  81. mpd_size_t overflow;
  82. (void)mul_size_t_overflow(nmemb, size, &overflow);
  83. if (overflow) {
  84. return NULL;
  85. }
  86. return mpd_callocfunc(nmemb, size);
  87. }
  88. /* realloc with overflow checking */
  89. void *
  90. mpd_realloc(void *ptr, mpd_size_t nmemb, mpd_size_t size, uint8_t *err)
  91. {
  92. void *new;
  93. mpd_size_t req, overflow;
  94. req = mul_size_t_overflow(nmemb, size, &overflow);
  95. if (overflow) {
  96. *err = 1;
  97. return ptr;
  98. }
  99. new = mpd_reallocfunc(ptr, req);
  100. if (new == NULL) {
  101. *err = 1;
  102. return ptr;
  103. }
  104. return new;
  105. }
  106. /* struct hack malloc with overflow checking */
  107. void *
  108. mpd_sh_alloc(mpd_size_t struct_size, mpd_size_t nmemb, mpd_size_t size)
  109. {
  110. mpd_size_t req, overflow;
  111. req = mul_size_t_overflow(nmemb, size, &overflow);
  112. if (overflow) {
  113. return NULL;
  114. }
  115. req = add_size_t_overflow(req, struct_size, &overflow);
  116. if (overflow) {
  117. return NULL;
  118. }
  119. return mpd_mallocfunc(req);
  120. }
  121. /* Allocate a new decimal with a coefficient of length 'nwords'. In case
  122. of an error the return value is NULL. */
  123. mpd_t *
  124. mpd_qnew_size(mpd_ssize_t nwords)
  125. {
  126. mpd_t *result;
  127. nwords = (nwords < MPD_MINALLOC) ? MPD_MINALLOC : nwords;
  128. result = mpd_alloc(1, sizeof *result);
  129. if (result == NULL) {
  130. return NULL;
  131. }
  132. result->data = mpd_alloc(nwords, sizeof *result->data);
  133. if (result->data == NULL) {
  134. mpd_free(result);
  135. return NULL;
  136. }
  137. result->flags = 0;
  138. result->exp = 0;
  139. result->digits = 0;
  140. result->len = 0;
  141. result->alloc = nwords;
  142. return result;
  143. }
  144. /* Allocate a new decimal with a coefficient of length MPD_MINALLOC.
  145. In case of an error the return value is NULL. */
  146. mpd_t *
  147. mpd_qnew(void)
  148. {
  149. return mpd_qnew_size(MPD_MINALLOC);
  150. }
  151. /* Allocate new decimal. Caller can check for NULL or MPD_Malloc_error.
  152. Raises on error. */
  153. mpd_t *
  154. mpd_new(mpd_context_t *ctx)
  155. {
  156. mpd_t *result;
  157. result = mpd_qnew();
  158. if (result == NULL) {
  159. mpd_addstatus_raise(ctx, MPD_Malloc_error);
  160. }
  161. return result;
  162. }
  163. /*
  164. * Input: 'result' is a static mpd_t with a static coefficient.
  165. * Assumption: 'nwords' >= result->alloc.
  166. *
  167. * Resize the static coefficient to a larger dynamic one and copy the
  168. * existing data. If successful, the value of 'result' is unchanged.
  169. * Otherwise, set 'result' to NaN and update 'status' with MPD_Malloc_error.
  170. */
  171. int
  172. mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
  173. {
  174. mpd_uint_t *p = result->data;
  175. assert(nwords >= result->alloc);
  176. result->data = mpd_alloc(nwords, sizeof *result->data);
  177. if (result->data == NULL) {
  178. result->data = p;
  179. mpd_set_qnan(result);
  180. mpd_set_positive(result);
  181. result->exp = result->digits = result->len = 0;
  182. *status |= MPD_Malloc_error;
  183. return 0;
  184. }
  185. memcpy(result->data, p, result->alloc * (sizeof *result->data));
  186. result->alloc = nwords;
  187. mpd_set_dynamic_data(result);
  188. return 1;
  189. }
  190. /*
  191. * Input: 'result' is a static mpd_t with a static coefficient.
  192. *
  193. * Convert the coefficient to a dynamic one that is initialized to zero. If
  194. * malloc fails, set 'result' to NaN and update 'status' with MPD_Malloc_error.
  195. */
  196. int
  197. mpd_switch_to_dyn_zero(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
  198. {
  199. mpd_uint_t *p = result->data;
  200. result->data = mpd_calloc(nwords, sizeof *result->data);
  201. if (result->data == NULL) {
  202. result->data = p;
  203. mpd_set_qnan(result);
  204. mpd_set_positive(result);
  205. result->exp = result->digits = result->len = 0;
  206. *status |= MPD_Malloc_error;
  207. return 0;
  208. }
  209. result->alloc = nwords;
  210. mpd_set_dynamic_data(result);
  211. return 1;
  212. }
  213. /*
  214. * Input: 'result' is a static or a dynamic mpd_t with a dynamic coefficient.
  215. * Resize the coefficient to length 'nwords':
  216. * Case nwords > result->alloc:
  217. * If realloc is successful:
  218. * 'result' has a larger coefficient but the same value. Return 1.
  219. * Otherwise:
  220. * Set 'result' to NaN, update status with MPD_Malloc_error and return 0.
  221. * Case nwords < result->alloc:
  222. * If realloc is successful:
  223. * 'result' has a smaller coefficient. result->len is undefined. Return 1.
  224. * Otherwise (unlikely):
  225. * 'result' is unchanged. Reuse the now oversized coefficient. Return 1.
  226. */
  227. int
  228. mpd_realloc_dyn(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
  229. {
  230. uint8_t err = 0;
  231. result->data = mpd_realloc(result->data, nwords, sizeof *result->data, &err);
  232. if (!err) {
  233. result->alloc = nwords;
  234. }
  235. else if (nwords > result->alloc) {
  236. mpd_set_qnan(result);
  237. mpd_set_positive(result);
  238. result->exp = result->digits = result->len = 0;
  239. *status |= MPD_Malloc_error;
  240. return 0;
  241. }
  242. return 1;
  243. }