Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec766a6

Browse files
author
Stefan Krah
committed
1) Remove claim of an input invariant that is only true for static mpd_t.
Resizing is used _inside_ libmpdec functions, and it is permitted to change x->alloc several times while setting x->len at the end of the function. Therefore, for dynamic mpd_t x->alloc can _temporarily_ drop below x->len. Of course the final result always has x->len <= x->alloc. For static mpd_t this cannot happen, since resizing to a smaller coefficient is a no-op. 2) Remove micro optimization in mpd_switch_to_dyn(): Previously only the valid initialized part of the existing coefficient up to x->len was copied to the new dynamic memory area. Now copying does the same as realloc() and the entire old memory area is copied. The rationale for this change is that it is no longer needed to memorize the explanation given in 1).
1 parent 7b544ca commit ec766a6

2 files changed

Lines changed: 2 additions & 4 deletions

File tree

Modules/_decimal/libmpdec/memory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
222222
return 0;
223223
}
224224

225-
memcpy(result->data, p, result->len * (sizeof *result->data));
225+
memcpy(result->data, p, result->alloc * (sizeof *result->data));
226226
result->alloc = nwords;
227227
mpd_set_dynamic_data(result);
228228
return 1;

Modules/_decimal/libmpdec/mpdecimal.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ mpd_del(mpd_t *dec)
454454
* Resize the coefficient. Existing data up to 'nwords' is left untouched.
455455
* Return 1 on success, 0 otherwise.
456456
*
457-
* Input invariants:
458-
* 1) MPD_MINALLOC <= result->alloc.
459-
* 2) 0 <= result->len <= result->alloc.
457+
* Input invariant: MPD_MINALLOC <= result->alloc.
460458
*
461459
* Case nwords == result->alloc:
462460
* 'result' is unchanged. Return 1.

0 commit comments

Comments
 (0)