1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-01 04:10:18 +02:00

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 }.
This commit is contained in:
Ken Raeburn 2012-05-21 00:30:45 -04:00 committed by Ludovic Courtès
parent 0854b87493
commit 67d8ccc0e4
2 changed files with 7 additions and 2 deletions

View file

@ -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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License * 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 */ /* a struct */
size_t off = 0; size_t off = 0;
size_t align = scm_to_size_t (scm_alignof (type));
while (scm_is_pair (type)) while (scm_is_pair (type))
{ {
off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type)))); off = ROUND_UP (off, scm_to_size_t (scm_alignof (scm_car (type))));
off += scm_to_size_t (scm_sizeof (scm_car (type))); off += scm_to_size_t (scm_sizeof (scm_car (type)));
type = scm_cdr (type); type = scm_cdr (type);
} }
return scm_from_size_t (off); return scm_from_size_t (ROUND_UP (off, align));
} }
else else
scm_wrong_type_arg (FUNC_NAME, 1, type); scm_wrong_type_arg (FUNC_NAME, 1, type);

View file

@ -342,6 +342,10 @@
(= (sizeof (list int8 double)) (= (sizeof (list int8 double))
(+ (alignof double) (sizeof 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 }" (pass-if "sizeof { short, int, long, pointer }"
(let ((layout (list short int long '*))) (let ((layout (list short int long '*)))
(>= (sizeof layout) (>= (sizeof layout)