[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 01/01: FFI: sizeof: Account for trailing padding of stru
From: |
Ludovic Courtès |
Subject: |
[Guile-commits] 01/01: FFI: sizeof: Account for trailing padding of structs. |
Date: |
Thu, 25 Jun 2015 12:47:02 +0000 |
civodul pushed a commit to branch stable-2.0
in repository guile.
commit 67d8ccc0e4cfd654b45a23264f55a420cf5fd863
Author: Ken Raeburn <address@hidden>
Date: Mon May 21 00:30:45 2012 -0400
FFI: sizeof: Account for trailing padding of structs.
* libguile/foreign.c (scm_sizeof): Make sure the overall size is a
multiple of the alignment of the structure.
* test-suite/tests/foreign.test: Test size of { double, int8 }.
---
libguile/foreign.c | 5 +++--
test-suite/tests/foreign.test | 4 ++++
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/libguile/foreign.c b/libguile/foreign.c
index 5c30d54..29cfc73 100644
--- a/libguile/foreign.c
+++ b/libguile/foreign.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 2010-2014 Free Software Foundation, Inc.
+/* Copyright (C) 2010-2015 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -547,13 +547,14 @@ SCM_DEFINE (scm_sizeof, "sizeof", 1, 0, 0, (SCM type),
{
/* a struct */
size_t off = 0;
+ size_t align = scm_to_size_t (scm_alignof (type));
while (scm_is_pair (type))
{
off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type))));
off += scm_to_size_t (scm_sizeof (scm_car (type)));
type = scm_cdr (type);
}
- return scm_from_size_t (off);
+ return scm_from_size_t (ROUND_UP (off, align));
}
else
scm_wrong_type_arg (FUNC_NAME, 1, type);
diff --git a/test-suite/tests/foreign.test b/test-suite/tests/foreign.test
index acdb3db..8ba989e 100644
--- a/test-suite/tests/foreign.test
+++ b/test-suite/tests/foreign.test
@@ -342,6 +342,10 @@
(= (sizeof (list int8 double))
(+ (alignof double) (sizeof double))))
+ (pass-if "sizeof { double, int8 }"
+ (= (sizeof (list double int8))
+ (+ (alignof double) (sizeof double))))
+
(pass-if "sizeof { short, int, long, pointer }"
(let ((layout (list short int long '*)))
(>= (sizeof layout)