Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
1355cf42
Commit
1355cf42
authored
11 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
modstruct: Fix .calcsize() to account for struct type/alignment.
parent
5695e072
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/binary.c
+57
-74
57 additions, 74 deletions
py/binary.c
py/binary.h
+1
-1
1 addition, 1 deletion
py/binary.h
py/modstruct.c
+4
-2
4 additions, 2 deletions
py/modstruct.c
py/objarray.c
+3
-3
3 additions, 3 deletions
py/objarray.c
with
65 additions
and
80 deletions
py/binary.c
+
57
−
74
View file @
1355cf42
#include
<stdint.h>
#include
<stdlib.h>
#include
<assert.h>
#include
"misc.h"
...
...
@@ -9,34 +10,52 @@
// Helpers to work with binary-encoded data
int
mp_binary_get_size
(
char
typecode
)
{
// This assumes that unsigned and signed types are of the same type,
// which is invariant for [u]intN_t.
switch
(
typecode
)
{
int
mp_binary_get_size
(
char
struct_type
,
char
val_type
,
uint
*
palign
)
{
int
size
=
0
;
int
align
=
1
;
switch
(
struct_type
)
{
case
'<'
:
case
'>'
:
switch
(
val_type
)
{
case
'b'
:
case
'B'
:
size
=
1
;
break
;
case
'h'
:
case
'H'
:
size
=
2
;
break
;
case
'i'
:
case
'I'
:
size
=
4
;
break
;
case
'l'
:
case
'L'
:
size
=
4
;
break
;
case
'q'
:
case
'Q'
:
size
=
8
;
break
;
}
break
;
case
'@'
:
{
// TODO:
// The simplest heuristic for alignment is to align by value
// size, but that doesn't work for "bigger than int" types,
// for example, long long may very well have long alignment
// So, we introduce separate alignment handling, but having
// formal support for that is different from actually supporting
// particular (or any) ABI.
switch
(
val_type
)
{
case
BYTEARRAY_TYPECODE
:
case
'b'
:
case
'B'
:
return
sizeof
(
int8_t
);
case
'h'
:
case
'H'
:
return
sizeof
(
int16_t
);
case
'i'
:
case
'I'
:
return
sizeof
(
int32_t
);
case
'l'
:
case
'L'
:
return
sizeof
(
int32_t
);
case
'q'
:
case
'Q'
:
return
sizeof
(
long
long
);
#if MICROPY_ENABLE_FLOAT
case
'f'
:
return
sizeof
(
float
);
case
'd'
:
return
sizeof
(
double
);
#endif
case
'b'
:
case
'B'
:
align
=
size
=
1
;
break
;
case
'h'
:
case
'H'
:
align
=
size
=
sizeof
(
short
);
break
;
case
'i'
:
case
'I'
:
align
=
size
=
sizeof
(
int
);
break
;
case
'l'
:
case
'L'
:
align
=
size
=
sizeof
(
long
);
break
;
case
'q'
:
case
'Q'
:
// TODO: This is for x86
align
=
sizeof
(
int
);
size
=
sizeof
(
long
long
);
break
;
}
}
}
return
-
1
;
if
(
palign
!=
NULL
)
{
*
palign
=
align
;
}
return
size
;
}
mp_obj_t
mp_binary_get_val_array
(
char
typecode
,
void
*
p
,
int
index
)
{
...
...
@@ -80,44 +99,10 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
#define is_signed(typecode) (typecode > 'Z')
mp_obj_t
mp_binary_get_val
(
char
struct_type
,
char
val_type
,
byte
**
ptr
)
{
byte
*
p
=
*
ptr
;
uint
size
=
0
;
switch
(
struct_type
)
{
case
'<'
:
case
'>'
:
switch
(
val_type
)
{
case
'b'
:
case
'B'
:
size
=
1
;
break
;
case
'h'
:
case
'H'
:
size
=
2
;
break
;
case
'i'
:
case
'I'
:
size
=
4
;
break
;
case
'l'
:
case
'L'
:
size
=
4
;
break
;
case
'q'
:
case
'Q'
:
size
=
8
;
break
;
}
break
;
case
'@'
:
{
// TODO:
// The simplest heuristic for alignment is to align by value
// size, but that doesn't work for "bigger than int" types,
// for example, long long may very well have long alignment
// So, we introduce separate alignment handling, but having
// formal support for that is different from actually supporting
// particular (or any) ABI.
uint
align
=
0
;
switch
(
val_type
)
{
case
'b'
:
case
'B'
:
align
=
size
=
1
;
break
;
case
'h'
:
case
'H'
:
align
=
size
=
sizeof
(
short
);
break
;
case
'i'
:
case
'I'
:
align
=
size
=
sizeof
(
int
);
break
;
case
'l'
:
case
'L'
:
align
=
size
=
sizeof
(
long
);
break
;
case
'q'
:
case
'Q'
:
// TODO: This is for x86
align
=
sizeof
(
int
);
size
=
sizeof
(
long
long
);
break
;
}
uint
align
;
int
size
=
mp_binary_get_size
(
struct_type
,
val_type
,
&
align
);
if
(
struct_type
==
'@'
)
{
// Make pointer aligned
p
=
(
byte
*
)(((
machine_uint_t
)
p
+
align
-
1
)
&
~
(
align
-
1
));
#if MP_ENDIANNESS_LITTLE
...
...
@@ -125,8 +110,6 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
#else
struct_type
=
'>'
;
#endif
break
;
}
}
int
delta
;
...
...
This diff is collapsed.
Click to expand it.
py/binary.h
+
1
−
1
View file @
1355cf42
...
...
@@ -2,7 +2,7 @@
// (underlyingly they're same).
#define BYTEARRAY_TYPECODE 0
int
mp_binary_get_size
(
char
typecode
);
int
mp_binary_get_size
(
char
struct_type
,
char
val_type
,
uint
*
palign
);
mp_obj_t
mp_binary_get_val_array
(
char
typecode
,
void
*
p
,
int
index
);
mp_obj_t
mp_binary_get_val
(
char
struct_type
,
char
val_type
,
byte
**
ptr
);
void
mp_binary_set_val_array
(
char
typecode
,
void
*
p
,
int
index
,
mp_obj_t
val_in
);
...
...
This diff is collapsed.
Click to expand it.
py/modstruct.c
+
4
−
2
View file @
1355cf42
...
...
@@ -37,12 +37,14 @@ STATIC uint calcsize_items(const char *fmt) {
STATIC
mp_obj_t
struct_calcsize
(
mp_obj_t
fmt_in
)
{
const
char
*
fmt
=
mp_obj_str_get_str
(
fmt_in
);
char
fmt_type
=
get_fmt_type
(
&
fmt
);
(
void
)
fmt_type
;
machine_uint_t
size
;
for
(
size
=
0
;
*
fmt
;
fmt
++
)
{
int
sz
=
mp_binary_get_size
(
*
fmt
);
uint
align
;
int
sz
=
mp_binary_get_size
(
fmt_type
,
*
fmt
,
&
align
);
// TODO
assert
(
sz
!=
-
1
);
// Apply alignment
size
=
(
size
+
align
-
1
)
&
~
(
align
-
1
);
size
+=
sz
;
}
return
MP_OBJ_NEW_SMALL_INT
(
size
);
...
...
This diff is collapsed.
Click to expand it.
py/objarray.c
+
3
−
3
View file @
1355cf42
...
...
@@ -121,7 +121,7 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_array
)
||
MP_OBJ_IS_TYPE
(
self_in
,
&
mp_type_bytearray
));
mp_obj_array_t
*
self
=
self_in
;
if
(
self
->
free
==
0
)
{
int
item_sz
=
mp_binary_get_size
(
self
->
typecode
);
int
item_sz
=
mp_binary_get_size
(
'@'
,
self
->
typecode
,
NULL
);
// TODO: alloc policy
self
->
free
=
8
;
self
->
items
=
m_realloc
(
self
->
items
,
item_sz
*
self
->
len
,
item_sz
*
(
self
->
len
+
self
->
free
));
...
...
@@ -154,7 +154,7 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
STATIC
machine_int_t
array_get_buffer
(
mp_obj_t
o_in
,
mp_buffer_info_t
*
bufinfo
,
int
flags
)
{
mp_obj_array_t
*
o
=
o_in
;
bufinfo
->
buf
=
o
->
items
;
bufinfo
->
len
=
o
->
len
*
mp_binary_get_size
(
o
->
typecode
);
bufinfo
->
len
=
o
->
len
*
mp_binary_get_size
(
'@'
,
o
->
typecode
,
NULL
);
bufinfo
->
typecode
=
o
->
typecode
;
return
0
;
}
...
...
@@ -190,7 +190,7 @@ const mp_obj_type_t mp_type_bytearray = {
};
STATIC
mp_obj_array_t
*
array_new
(
char
typecode
,
uint
n
)
{
int
typecode_size
=
mp_binary_get_size
(
typecode
);
int
typecode_size
=
mp_binary_get_size
(
'@'
,
typecode
,
NULL
);
if
(
typecode_size
<=
0
)
{
nlr_raise
(
mp_obj_new_exception_msg
(
&
mp_type_ValueError
,
"bad typecode"
));
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment