summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Krah <skrah@bytereef.org>2012-04-10 23:11:54 +0200
committerStefan Krah <skrah@bytereef.org>2012-04-10 23:11:54 +0200
commit10a3d65964817be7c1a182472f4d713e56648a63 (patch)
tree90a42d6ad51ba0a0fe622a6e6094045b52cf611a
parentc13e97d5263b95d9aad8648306961bb2a41080a0 (diff)
downloadcpython-10a3d65964817be7c1a182472f4d713e56648a63.tar.gz
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).
-rw-r--r--Modules/_decimal/libmpdec/memory.c2
-rw-r--r--Modules/_decimal/libmpdec/mpdecimal.c4
2 files changed, 2 insertions, 4 deletions
diff --git a/Modules/_decimal/libmpdec/memory.c b/Modules/_decimal/libmpdec/memory.c
index 037ba35e37..bf6350f904 100644
--- a/Modules/_decimal/libmpdec/memory.c
+++ b/Modules/_decimal/libmpdec/memory.c
@@ -222,7 +222,7 @@ mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t nwords, uint32_t *status)
return 0;
}
- memcpy(result->data, p, result->len * (sizeof *result->data));
+ memcpy(result->data, p, result->alloc * (sizeof *result->data));
result->alloc = nwords;
mpd_set_dynamic_data(result);
return 1;
diff --git a/Modules/_decimal/libmpdec/mpdecimal.c b/Modules/_decimal/libmpdec/mpdecimal.c
index b83334e723..844f2387a1 100644
--- a/Modules/_decimal/libmpdec/mpdecimal.c
+++ b/Modules/_decimal/libmpdec/mpdecimal.c
@@ -454,9 +454,7 @@ mpd_del(mpd_t *dec)
* Resize the coefficient. Existing data up to 'nwords' is left untouched.
* Return 1 on success, 0 otherwise.
*
- * Input invariants:
- * 1) MPD_MINALLOC <= result->alloc.
- * 2) 0 <= result->len <= result->alloc.
+ * Input invariant: MPD_MINALLOC <= result->alloc.
*
* Case nwords == result->alloc:
* 'result' is unchanged. Return 1.