How to store structure members offset into constants table?

Other misc things
Post Reply
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

How to store structure members offset into constants table?

Post by DarkSchneider »

Hi, I am trying to save a struct members offset into a constants table, for parameterizing their access. The concept is something like:

Code: Select all

// everything is constant
typedef struct {
type1 table1[size1];
...
typen tablen[sizen];
} Tables;

const size_t offsets[] = { offsetof(table1), ... , offsetof(tablen) };
Then we can access with:

Code: Select all

Tables *tables = somewhere;
void *tableX = tables + offsets[X];
But the const only seems to accept direct values, if using things like these it fails with error: Expecting constant expression:

Code: Select all

(size_t)((Tables*)&0)->tableX
((Tables)&0).tableX
sizeof(type1) <= to get offset of table2
Curiously the method shown first works within the execution code, but not at constant declaration, even if it is a constant value by itself.
User avatar
jorgegv
Well known member
Posts: 287
Joined: Wed Nov 18, 2020 5:08 pm

Re: How to store structure members offset into constants table?

Post by jorgegv »

You are doing pointer arithmetic with void * pointers, I believe that is not allowed. For pointer arithmetic to work the compiler needs to know the size of the pointed-to type.
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: How to store structure members offset into constants table?

Post by dom »

It looks like that sort of operation is supported by sdcc, but not by sccz80 (so I''ll add it in).

If you include <stddef.h> you'll get the usual offsetof() macro which maps to a __builtin function
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

Re: How to store structure members offset into constants table?

Post by DarkSchneider »

dom wrote: Thu Mar 16, 2023 3:58 pm It looks like that sort of operation is supported by sdcc, but not by sccz80 (so I''ll add it in).

If you include <stddef.h> you'll get the usual offsetof() macro which maps to a __builtin function
Thanks that would be great.
Also tested the offsetof in stddef but didn’t work for defining it into a const table.
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: How to store structure members offset into constants table?

Post by dom »

I've committed a fix that works with offsetof() in stddef.h

Code: Select all

#include <stddef.h>

 struct x {
    long table1[20];
    double tablen[10];
 };

 typedef struct x xt;

 static struct x data;

 long offs_x = offsetof(struct x, tablen);
 long offs_x2 = offsetof(x, tablen);
 int offs_xt = offsetof(xt, tablen);
 int offs_data = offsetof(data, tablen);


 void func()
 {
     long offs_x = offsetof(struct x, tablen);
     long offs_x2 = offsetof(x, tablen);
     int offs_xt = offsetof(xt, tablen);
     int offs_data = offsetof(data, tablen);
}
Hopefully that will cover your use cases.
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

Re: How to store structure members offset into constants table?

Post by DarkSchneider »

To use it I have to update all the z88dk to new version or downloading the new stddef.h is enough?
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: How to store structure members offset into constants table?

Post by dom »

DarkSchneider wrote: Fri Mar 17, 2023 11:18 am To use it I have to update all the z88dk to new version or downloading the new stddef.h is enough?
It was a change in sccz80, so a new build of that is needed. The new version is in the 202030317 nightly build.
DarkSchneider
Member
Posts: 71
Joined: Sun Apr 01, 2018 4:02 pm

Re: How to store structure members offset into constants table?

Post by DarkSchneider »

OK, tested and works fine.
As side note, the install documentation should be updated for macOS as when trying to use it I got the advice that executable cannot be verified blahblahblah...
One solution could be to use the official Apple about clicking in Finder one by one then Open to add it as exception.
But the batch one is better, simply go to the z88dk bin folder and execute in shell:

Code: Select all

xattr -d com.apple.quarantine  *
(source: https://stackoverflow.com/questions/708 ... heck-macos )
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: How to store structure members offset into constants table?

Post by dom »

That's great.

The binaries should be signed (by me). I probably ought to add in the notarise step which I hope will mean that the binaries will just be able to run. I think the fact that you get a "can't be verified" and not "unknown developer" means that the signing actually worked (hurrah).

The quarantine attribute is I believe added by the browser when you download, you can use wget/curl to bypass it.
Post Reply