summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-08-10 22:27:52 +0100
committerDamien George <damien.p.george@gmail.com>2014-08-10 22:27:52 +0100
commit6e6bcccdc19f35ccb09c064cb444eb498c307a33 (patch)
tree80d5d3e4267ad95f52a5a364eaaf3340e5e764b9 /py/binary.c
parent101d87da6ac24439cc0238775aba062edb7d649c (diff)
parent0c5498540b2005e39422647d2ca9cad1a4ff731b (diff)
downloadmicropython-6e6bcccdc19f35ccb09c064cb444eb498c307a33.tar.gz
micropython-6e6bcccdc19f35ccb09c064cb444eb498c307a33.zip
Merge branch 'master' of github.com:micropython/micropython
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/py/binary.c b/py/binary.c
index 3f1d7f2867..65688272aa 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -26,6 +26,7 @@
#include <stdint.h>
#include <stdlib.h>
+#include <stddef.h>
#include <string.h>
#include <assert.h>
@@ -37,6 +38,10 @@
// Helpers to work with binary-encoded data
+#ifndef alignof
+#define alignof(type) offsetof(struct { char c; type t; }, t)
+#endif
+
int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
int size = 0;
int align = 1;
@@ -68,16 +73,20 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
case 'b': case 'B':
align = size = 1; break;
case 'h': case 'H':
- align = size = sizeof(short); break;
+ align = alignof(short);
+ size = sizeof(short); break;
case 'i': case 'I':
- align = size = sizeof(int); break;
+ align = alignof(int);
+ size = sizeof(int); break;
case 'l': case 'L':
- align = size = sizeof(long); break;
+ align = alignof(long);
+ size = sizeof(long); break;
case 'q': case 'Q':
- // TODO: This is for x86
- align = sizeof(int); size = sizeof(long long); break;
+ align = alignof(long long);
+ size = sizeof(long long); break;
case 'P': case 'O': case 'S':
- align = size = sizeof(void*); break;
+ align = alignof(void*);
+ size = sizeof(void*); break;
}
}
}