Thanks to visit codestin.com
Credit goes to github.com

Skip to content

py/misc: Change sizeof to offsetof for variable-length alloc. #12866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

jimmo
Copy link
Member

@jimmo jimmo commented Nov 3, 2023

This fixes the case where e.g.

struct foo_t {
  mp_obj_t x;
  uint16_t y;
  char buf[];
};

will have sizeof(struct foo_t)==8, but offsetof(struct foo_t, buf)==6.

When computing the size to allocate for m_new_obj_var we need to use offsetof to avoid over-allocating. This is important especially when it might cause it to spill over into another GC block.

All of the places that m_new_obj_var is currently used will be unchanged as the variable length field always follows a word-size field, but see e.g. #12836 which requires this.

This work was funded through GitHub Sponsors.

Copy link

github-actions bot commented Nov 3, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link

codecov bot commented Nov 3, 2023

Codecov Report

Merging #12866 (070ecc1) into master (c85db05) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 070ecc1 differs from pull request most recent head b6a9778. Consider uploading reports for the commit b6a9778 to get more accurate results

@@           Coverage Diff           @@
##           master   #12866   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         158      158           
  Lines       20971    20973    +2     
=======================================
+ Hits        20635    20637    +2     
  Misses        336      336           
Files Coverage Δ
extmod/modre.c 100.00% <100.00%> (ø)
extmod/vfs_lfsx_file.c 94.39% <100.00%> (ø)
py/misc.h 100.00% <ø> (ø)
py/modthread.c 98.96% <100.00%> (ø)
py/objexcept.c 96.42% <100.00%> (ø)
py/objfun.c 100.00% <100.00%> (ø)
py/objnamedtuple.c 100.00% <100.00%> (ø)
py/objtuple.c 100.00% <100.00%> (ø)
py/objtype.c 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

@dpgeorge dpgeorge added py-core Relates to py/ directory in source micropython-lib and removed micropython-lib labels Nov 3, 2023
This fixes the case where e.g.

    struct foo_t {
      mp_obj_t x;
      uint16_t y;
      char buf[];
    };

will have `sizeof(struct foo_t)==8`, but `offsetof(struct foo_t, buf)==6`.

When computing the size to allocate for `m_new_obj_var` we need to use
offsetof to avoid over-allocating. This is important especially when it
might cause it to spill over into another GC block.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <[email protected]>
@dpgeorge dpgeorge force-pushed the m_new_obj_var-offsetof branch from 070ecc1 to b6a9778 Compare November 3, 2023 05:05
@dpgeorge dpgeorge merged commit b6a9778 into micropython:master Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants