Skip to content

Commit

Permalink
RT 131375: Fix calculation for maximum array size
Browse files Browse the repository at this point in the history
We were off by elem_size - log2(elem_size). As we're calculating the
number of bits available for adressing individual array elements, we
have to substract the number of bits for addressing individual bytes in
an array element rather than the number of bytes.
For 64 bit array elements, i.e. 8 bytes, we need 3 bits for addressing
individual bytes. So we lose 3 bits when adressing elements in the
array.
  • Loading branch information
niner committed Aug 22, 2017
1 parent e4e9c04 commit f9b65a9
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/6model/reprs/VMArray.c
@@ -1,4 +1,5 @@
#include "moar.h"
#include "limits.h"

/* This representation's function pointer table. */
static const MVMREPROps VMArray_this_repr;
Expand Down Expand Up @@ -337,10 +338,24 @@ static void set_size_internal(MVMThreadContext *tc, MVMArrayBody *body, MVMuint6
else {
ssize = (n + 0x1000) & ~0xfffUL;
}
if (ssize > (1UL << (8 * sizeof(size_t) - repr_data->elem_size)))
MVM_exception_throw_adhoc(tc,
"Unable to allocate an array of %"PRIu64" elements",
ssize);
{
/* Our budget is 2^(
* <number of bits in an array index>
* - <number of bits to address individual bytes in an array element>
* ) */
size_t const elem_addr_size =
repr_data->elem_size == 8
? 4
: repr_data->elem_size == 4
? 3
: repr_data->elem_size == 2
? 2
: 1;
if (ssize > (1UL << (CHAR_BIT * sizeof(size_t) - elem_addr_size)))
MVM_exception_throw_adhoc(tc,
"Unable to allocate an array of %"PRIu64" elements",
ssize);
}

/* now allocate the new slot buffer */
slots = (slots)
Expand Down

0 comments on commit f9b65a9

Please sign in to comment.