Skip to content

Commit

Permalink
py/binary: Change mp_uint_t to size_t for index, size, align args.
Browse files Browse the repository at this point in the history
Reduces code size for nan-box builds, otherwise changes nothing.
  • Loading branch information
dpgeorge committed Sep 2, 2019
1 parent 24c3e9b commit c348e79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
20 changes: 10 additions & 10 deletions py/binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define alignof(type) offsetof(struct { char c; type t; }, t)
#endif

size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) {
size_t size = 0;
int align = 1;
switch (struct_type) {
Expand Down Expand Up @@ -113,7 +113,7 @@ size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
return size;
}

mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
mp_int_t val = 0;
switch (typecode) {
case 'b':
Expand Down Expand Up @@ -162,7 +162,7 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
// The long long type is guaranteed to hold at least 64 bits, and size is at
// most 8 (for q and Q), so we will always be able to parse the given data
// and fit it into a long long.
long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, const byte *src) {
long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src) {
int delta;
if (!big_endian) {
delta = -1;
Expand All @@ -187,12 +187,12 @@ long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, con
#define is_signed(typecode) (typecode > 'Z')
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) {
byte *p = *ptr;
mp_uint_t align;
size_t align;

size_t size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
// Align p relative to p_base
p = p_base + (uintptr_t)MP_ALIGN(p - p_base, (size_t)align);
p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
#else
Expand Down Expand Up @@ -231,7 +231,7 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte *
}
}

void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
if (MP_ENDIANNESS_LITTLE && !big_endian) {
memcpy(dest, &val, val_sz);
} else if (MP_ENDIANNESS_BIG && big_endian) {
Expand All @@ -252,12 +252,12 @@ void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t

void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) {
byte *p = *ptr;
mp_uint_t align;
size_t align;

size_t size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
// Align p relative to p_base
p = p_base + (uintptr_t)MP_ALIGN(p - p_base, (size_t)align);
p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
if (MP_ENDIANNESS_LITTLE) {
struct_type = '<';
} else {
Expand Down Expand Up @@ -315,7 +315,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
}

void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) {
switch (typecode) {
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
Expand All @@ -342,7 +342,7 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v
}
}

void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val) {
void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) {
switch (typecode) {
case 'b':
((signed char*)p)[index] = val;
Expand Down
12 changes: 6 additions & 6 deletions py/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
// type-specification errors due to end-of-string.
#define BYTEARRAY_TYPECODE 1

size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign);
mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index);
void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in);
void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val);
size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign);
mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index);
void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in);
void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val);
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr);
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr);
long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, const byte *src);
void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val);
long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src);
void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val);

#endif // MICROPY_INCLUDED_PY_BINARY_H
2 changes: 1 addition & 1 deletion py/modstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ STATIC size_t calc_size_items(const char *fmt, size_t *total_sz) {
size += cnt;
} else {
total_cnt += cnt;
mp_uint_t align;
size_t align;
size_t sz = mp_binary_get_size(fmt_type, *fmt, &align);
while (cnt--) {
// Apply alignment
Expand Down

0 comments on commit c348e79

Please sign in to comment.