Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If this is timesharing, give me my share right now.


devel / comp.lang.c / Re: Used-Defined Builtins

SubjectAuthor
* Used-Defined BuiltinsLawrence D'Oliveiro
+* Re: Used-Defined BuiltinsKaz Kylheku
|`- Re: Used-Defined BuiltinsDavid Brown
+* Re: Used-Defined BuiltinsThiago Adams
|+* Re: Used-Defined BuiltinsKeith Thompson
||`- Re: Used-Defined BuiltinsThiago Adams
|+* Re: Used-Defined BuiltinsDavid Brown
||`- Re: Used-Defined BuiltinsThiago Adams
|`- Re: Used-Defined BuiltinsThiago Adams
+- Re: Used-Defined Builtinsbart
`- Re: Used-Defined BuiltinsJanis Papanagnou

1
Used-Defined Builtins

<uq4fbf$2gc02$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32920&group=comp.lang.c#32920

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Used-Defined Builtins
Date: Fri, 9 Feb 2024 06:05:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <uq4fbf$2gc02$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 06:05:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b81b1b5b0886ece37827f7a23070dda";
logging-data="2633730"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+UmtoLpsrLhFUgRLlpnAwL"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:2qCpdJUx1IPndaGGhmq9AWXg5no=
 by: Lawrence D'Oliv - Fri, 9 Feb 2024 06:05 UTC

Modula-2 had this interesting feature where, for example if you had a
pointer variable whose pointed-to objects were of type T

VAR
ptr : POINTER TO T;

and you had a statement like

ptr := NEW(T);

then the compiler would translate the “NEW(T)” into “ALLOCATE(TSIZE(T))”,
where the “ALLOCATE” function was not actually provided by the language,
but had to be defined/introduced in the current scope somehow (perhaps
IMPORTed from some implementation-provided library).

This allowed for memory allocations (and also deallocations) to be
expressed in a type-safe fashion, while still leaving the low-level
details of memory management up to some library/user code that was not an
integral part of the language implementation.

So you had a builtin function, NEW(), that could take a type as an
argument and return a result of the appropriate pointer type, which a
normal user-defined function could not do, but the compiler would delegate
the main work to a lower-level function that didn’t need to know anything
about the language type system, it only needed to know how much memory to
allocate, and would return an untyped pointer, which the compiler would
then take care of turning into a pointer of the required type.

C could benefit from some similar high-level+low-level layering like this,
don’t you think?

Re: Used-Defined Builtins

<20240209000556.927@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32926&group=comp.lang.c#32926

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Fri, 9 Feb 2024 08:08:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <20240209000556.927@kylheku.com>
References: <uq4fbf$2gc02$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 08:08:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="f0d9c4d56e1bf3d087a60d362bc7520c";
logging-data="2679200"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ozLgbyRPrWpnbJyvdmR5hDWCjmro2ook="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:9neGqim72EXAjaxcTxyjG5GrOI4=
 by: Kaz Kylheku - Fri, 9 Feb 2024 08:08 UTC

On 2024-02-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
> Modula-2 had this interesting feature where, for example if you had a
> pointer variable whose pointed-to objects were of type T
>
> VAR
> ptr : POINTER TO T;
>
> and you had a statement like
>
> ptr := NEW(T);

[ ... ]

> C could benefit from some similar high-level+low-level layering like this,
> don’t you think?

Yes, someone thought that (and other things) and came up with C++.

T *p = new T;

If T is a class, it can have its own allocator. The new operator
ensures that the constructor is called to initialize the T object.
You can pass constructor parameters to choose different constructor
overloads:

T *p = new T(4, "blah");

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Used-Defined Builtins

<uq4n2b$2hive$4@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32928&group=comp.lang.c#32928

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Fri, 9 Feb 2024 09:17:15 +0100
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <uq4n2b$2hive$4@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me> <20240209000556.927@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 08:17:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9c8bd9f8f89cf9a03af351ae8d81a71b";
logging-data="2673646"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wznHEIfMZQ4qPjv8TOdaOg+vZ7gPK6ZU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:ZOn/IE5ViMEm78Qqcj3/Bt/OAU4=
In-Reply-To: <20240209000556.927@kylheku.com>
Content-Language: en-GB
 by: David Brown - Fri, 9 Feb 2024 08:17 UTC

On 09/02/2024 09:08, Kaz Kylheku wrote:
> On 2024-02-09, Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>> Modula-2 had this interesting feature where, for example if you had a
>> pointer variable whose pointed-to objects were of type T
>>
>> VAR
>> ptr : POINTER TO T;
>>
>> and you had a statement like
>>
>> ptr := NEW(T);
>
> [ ... ]
>
>> C could benefit from some similar high-level+low-level layering like this,
>> don’t you think?
>
> Yes, someone thought that (and other things) and came up with C++.
>
> T *p = new T;
>
> If T is a class, it can have its own allocator. The new operator
> ensures that the constructor is called to initialize the T object.
> You can pass constructor parameters to choose different constructor
> overloads:
>
> T *p = new T(4, "blah");
>

You can also provide a new global "new" operator if you want.

Making a "safer" replacement, alternative or enhancement to C was one of
the motivations for C++. Perhaps unfortunately, you can write C++ as
unsafely as you want - but if you decide to put in the effort it has the
features to make your code a great deal safer than plain old C.

Re: Used-Defined Builtins

<uq5erg$2m46u$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32955&group=comp.lang.c#32955

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Fri, 9 Feb 2024 12:03:11 -0300
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <uq5erg$2m46u$1@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 15:03:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="40edd4c395ae4b3d76b1a6acfc65bec1";
logging-data="2822366"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190I6OWBEEQnVsOErGNTT+DLfa5+o/8Tvg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:CV7ImK1KsSCAZ10txDiOlX6vjDc=
Content-Language: en-GB
In-Reply-To: <uq4fbf$2gc02$1@dont-email.me>
 by: Thiago Adams - Fri, 9 Feb 2024 15:03 UTC

Em 2/9/2024 3:05 AM, Lawrence D'Oliveiro escreveu:
> Modula-2 had this interesting feature where, for example if you had a
> pointer variable whose pointed-to objects were of type T
>
> VAR
> ptr : POINTER TO T;
>
> and you had a statement like
>
> ptr := NEW(T);
>
> then the compiler would translate the “NEW(T)” into “ALLOCATE(TSIZE(T))”,
> where the “ALLOCATE” function was not actually provided by the language,
> but had to be defined/introduced in the current scope somehow (perhaps
> IMPORTed from some implementation-provided library).
>
> This allowed for memory allocations (and also deallocations) to be
> expressed in a type-safe fashion, while still leaving the low-level
> details of memory management up to some library/user code that was not an
> integral part of the language implementation.
>
> So you had a builtin function, NEW(), that could take a type as an
> argument and return a result of the appropriate pointer type, which a
> normal user-defined function could not do, but the compiler would delegate
> the main work to a lower-level function that didn’t need to know anything
> about the language type system, it only needed to know how much memory to
> allocate, and would return an untyped pointer, which the compiler would
> then take care of turning into a pointer of the required type.
>
> C could benefit from some similar high-level+low-level layering like this,
> don’t you think?

This is C23 code

#include <stdlib.h>
#include <string.h>

static inline void* allocate_and_copy(void* s, size_t n) {
void* p = malloc(n);
if (p) {
memcpy(p, s, n);
}
return p;
}

#define NEW(...) (typeof(__VA_ARGS__)*)
allocate_and_copy(&(__VA_ARGS__), sizeof(__VA_ARGS__))
#pragma expand NEW

struct X {
const int i;
};

int main() {
auto p = NEW((struct X) {});
}

https://thradams.com/cake/playground.html?code=CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKc3RhdGljIGlubGluZSB2b2lkKiBhbGxvY2F0ZV9hbmRfY29weSh2b2lkKiBzLCBzaXplX3QgbikgewogICAgdm9pZCogcCA9IG1hbGxvYyhuKTsKICAgIGlmIChwKSB7CiAgICAgICAgbWVtY3B5KHAsIHMsIG4pOwogICAgfQogICAgcmV0dXJuIHA7Cn0KCiNkZWZpbmUgTkVXKC4uLikgKHR5cGVvZihfX1ZBX0FSR1NfXykqKSBhbGxvY2F0ZV9hbmRfY29weSgmKF9fVkFfQVJHU19fKSwgc2l6ZW9mKF9fVkFfQVJHU19fKSkKI3ByYWdtYSBleHBhbmQgTkVXCgpzdHJ1Y3QgWCB7CiAgICBjb25zdCBpbnQgaTsKfTsKCmludCBtYWluKCkgeyAKICAgIGF1dG8gcCA9IE5FVygoc3RydWN0IFgpIHt9KTsgICAgIAp9Cg%3D%3D&to=-1&options=

https://godbolt.org/z/b7z8YndWb

Re: Used-Defined Builtins

<uq5gp2$2mgb3$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32956&group=comp.lang.c#32956

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Fri, 9 Feb 2024 15:36:02 +0000
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <uq5gp2$2mgb3$1@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 15:36:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7249358d931a886a5d72096d92eeb1eb";
logging-data="2834787"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18S1StQ6ekNEzYT03/iDlj1Mjsk1++cbxc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:GVEAXWxw2sh3lLC9Kn2yk3hj2J4=
In-Reply-To: <uq4fbf$2gc02$1@dont-email.me>
Content-Language: en-GB
 by: bart - Fri, 9 Feb 2024 15:36 UTC

On 09/02/2024 06:05, Lawrence D'Oliveiro wrote:
> Modula-2 had this interesting feature where, for example if you had a
> pointer variable whose pointed-to objects were of type T
>
> VAR
> ptr : POINTER TO T;
>
> and you had a statement like
>
> ptr := NEW(T);
>
> then the compiler would translate the “NEW(T)” into “ALLOCATE(TSIZE(T))”,
> where the “ALLOCATE” function was not actually provided by the language,
> but had to be defined/introduced in the current scope somehow (perhaps
> IMPORTed from some implementation-provided library).
>
> This allowed for memory allocations (and also deallocations) to be
> expressed in a type-safe fashion, while still leaving the low-level
> details of memory management up to some library/user code that was not an
> integral part of the language implementation.
>
> So you had a builtin function, NEW(), that could take a type as an
> argument and return a result of the appropriate pointer type, which a
> normal user-defined function could not do, but the compiler would delegate
> the main work to a lower-level function that didn’t need to know anything
> about the language type system, it only needed to know how much memory to
> allocate, and would return an untyped pointer, which the compiler would
> then take care of turning into a pointer of the required type.
>
> C could benefit from some similar high-level+low-level layering like this,
> don’t you think?

It's not particularly high level if all it does is allocate a possibly
uninitialised block of memory of a suitable size.

You will probably still need to deallocate manually or rely on a GC.
(Can you do F(NEW(T), NEW(T), NEW(T)?)

Where T has a variable size attached to it, for example ARRAY OF U, then
NEW will need to know the size, but also, this needs to be somehow
associated with that instance of T.

(And can U here itself be something you'd call NEW on?)

It's hard to retrofit such features into a lower-level language. In C
you can do:

ptr = malloc(sizeof(*ptr))

but it is not checked by the compiler. (I assume Modular 2 will complain
if you do 'ptr := NEW(U)'.)

Re: Used-Defined Builtins

<877cjdfw2g.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32961&group=comp.lang.c#32961

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Fri, 09 Feb 2024 08:20:55 -0800
Organization: None to speak of
Lines: 39
Message-ID: <877cjdfw2g.fsf@nosuchdomain.example.com>
References: <uq4fbf$2gc02$1@dont-email.me> <uq5erg$2m46u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="639f38d912c33920e0e5a36e0f09675f";
logging-data="2845310"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19KMzs8UPB2XPgYxoReHu4b"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:B7JfrCYAbx1f2vlEbELpwYhXDgs=
sha1:b6k3FSQhkt5Q5mshxKSs/UYupTQ=
 by: Keith Thompson - Fri, 9 Feb 2024 16:20 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
[...]
> This is C23 code
>
>
> #include <stdlib.h>
> #include <string.h>
>
> static inline void* allocate_and_copy(void* s, size_t n) {
> void* p = malloc(n);
> if (p) {
> memcpy(p, s, n);
> }
> return p;
> }
>
> #define NEW(...) (typeof(__VA_ARGS__)*)
> allocate_and_copy(&(__VA_ARGS__), sizeof(__VA_ARGS__))
> #pragma expand NEW
>
> struct X {
> const int i;
> };
>
> int main() {
> auto p = NEW((struct X) {});
> }

The "#define NEW" line was wrapped, probably by your newsreader.
Either join the lines or add a backslash on the "#define" line.

"#pragma expand" is non-standard. I think it's specific to "cake", a
tool that translates C23 to earlier versions of C
(https://github.com/thradams/cake/).

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Medtronic
void Void(void) { Void(); } /* The recursive call of the void */

Re: Used-Defined Builtins

<uq5jsq$2n0g2$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=32964&group=comp.lang.c#32964

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Fri, 9 Feb 2024 17:29:14 +0100
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <uq5jsq$2n0g2$2@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me> <uq5erg$2m46u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 9 Feb 2024 16:29:14 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="9c8bd9f8f89cf9a03af351ae8d81a71b";
logging-data="2851330"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18AYtmJUqm5bepz/F8mMGnqqRZawjGZFIM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:Cs5ExmIdj+70iEnas+GixBdt9W0=
In-Reply-To: <uq5erg$2m46u$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Fri, 9 Feb 2024 16:29 UTC

On 09/02/2024 16:03, Thiago Adams wrote:

> This is C23 code
>
>
> #include <stdlib.h>
> #include <string.h>
>
> static inline void* allocate_and_copy(void* s, size_t n) {
>     void* p = malloc(n);
>     if (p) {
>         memcpy(p, s, n);
>     }
>     return p;
> }
>
> #define NEW(...) (typeof(__VA_ARGS__)*)
> allocate_and_copy(&(__VA_ARGS__), sizeof(__VA_ARGS__))
> #pragma expand NEW
>
> struct X {
>     const int i;
> };
>
> int main() {
>     auto p = NEW((struct X) {});
> }
>

"#pragma expand" is not standard C. What does it mean, and what tools
support it?

Re: Used-Defined Builtins

<uqbvup$18n3p$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33118&group=comp.lang.c#33118

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Mon, 12 Feb 2024 03:31:52 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <uqbvup$18n3p$1@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 12 Feb 2024 02:31:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c23c05b8d923f6a663128bb40f9cc95d";
logging-data="1334393"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/WOrx+rIoXpmVtnOWoJM2"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:6dvAdrCWdFCkdffbFim/YTwuyk0=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <uq4fbf$2gc02$1@dont-email.me>
 by: Janis Papanagnou - Mon, 12 Feb 2024 02:31 UTC

On 09.02.2024 07:05, Lawrence D'Oliveiro wrote:
> Modula-2 had this interesting feature where, for example if you had a
> pointer variable whose pointed-to objects were of type T
>
> VAR
> ptr : POINTER TO T;
>
> and you had a statement like
>
> ptr := NEW(T);
>
> then the compiler would translate the “NEW(T)” into “ALLOCATE(TSIZE(T))”,
> [...]

The type bound pointer type was already introduced by Niklaus Wirth
in Pascal (with an only slightly different syntax, type PT = ^T ),
so it's not too surprising that he used that concept also in Modula.

>
> C could benefit from some similar high-level+low-level layering like this,
> don’t you think?

Yes. Type bound pointers have been said (I think by F. L. Bauer)
to be sort of a tamed/controlled version of an inherently insecure
(pointer-)concept. I'm not sure, though, (and too tired to ponder
about it) but I seem to recall that with that coupling there's the
possibility to write type-safe pointer constructs; that would have
(IIRC) to be supported by the compiler. So introducing it in C as a
layer on top would probably not suffice to gain the same safety.
Hiding only the allocation would obviously be just syntactic sugar.

You find that binding also in C++ (as has been mentioned elsethread)
but that had been borrowed from Simula: REF(T) p; p :- new T; Both
languages allow to assign subtypes in a class hierarchy, of course,
to make most sense.

Janis

Re: Used-Defined Builtins

<uqc757$1c1qk$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33124&group=comp.lang.c#33124

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Mon, 12 Feb 2024 01:34:46 -0300
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <uqc757$1c1qk$1@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me> <uq5erg$2m46u$1@dont-email.me>
<877cjdfw2g.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 12 Feb 2024 04:34:47 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0fdb2e3a634e10568940305ab09526a7";
logging-data="1443668"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4jIeFBisteXg5B0piRhapVaXNKfVTlZ4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:3umFbcb50M04FdOTkRwZzWApli0=
In-Reply-To: <877cjdfw2g.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: Thiago Adams - Mon, 12 Feb 2024 04:34 UTC

Em 2/9/2024 1:20 PM, Keith Thompson escreveu:
> Thiago Adams <thiago.adams@gmail.com> writes:
> [...]
>> This is C23 code
>>
>>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> static inline void* allocate_and_copy(void* s, size_t n) {
>> void* p = malloc(n);
>> if (p) {
>> memcpy(p, s, n);
>> }
>> return p;
>> }
>>
>> #define NEW(...) (typeof(__VA_ARGS__)*)
>> allocate_and_copy(&(__VA_ARGS__), sizeof(__VA_ARGS__))
>> #pragma expand NEW
>>
>> struct X {
>> const int i;
>> };
>>
>> int main() {
>> auto p = NEW((struct X) {});
>> }
>
> The "#define NEW" line was wrapped, probably by your newsreader.
> Either join the lines or add a backslash on the "#define" line.
>
> "#pragma expand" is non-standard. I think it's specific to "cake", a
> tool that translates C23 to earlier versions of C
> (https://github.com/thradams/cake/).
>

Exactly.
I forgot to remove pragma expand from the sample.
But it can be removed and the code still works.

Re: Used-Defined Builtins

<uqc7a0$1c1qk$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33125&group=comp.lang.c#33125

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Mon, 12 Feb 2024 01:37:19 -0300
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <uqc7a0$1c1qk$2@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me> <uq5erg$2m46u$1@dont-email.me>
<uq5jsq$2n0g2$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 12 Feb 2024 04:37:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0fdb2e3a634e10568940305ab09526a7";
logging-data="1443668"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+x5sGGC8c7lKYvAMNF2zytFn3kQGnFS0w="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:fgQiCFm+yYjv2txQzmV4QN3dxwk=
Content-Language: en-GB
In-Reply-To: <uq5jsq$2n0g2$2@dont-email.me>
 by: Thiago Adams - Mon, 12 Feb 2024 04:37 UTC

Em 2/9/2024 1:29 PM, David Brown escreveu:
> On 09/02/2024 16:03, Thiago Adams wrote:
>
>> This is C23 code
>>
>>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> static inline void* allocate_and_copy(void* s, size_t n) {
>>      void* p = malloc(n);
>>      if (p) {
>>          memcpy(p, s, n);
>>      }
>>      return p;
>> }
>>
>> #define NEW(...) (typeof(__VA_ARGS__)*)
>> allocate_and_copy(&(__VA_ARGS__), sizeof(__VA_ARGS__))
>> #pragma expand NEW
>>
>> struct X {
>>      const int i;
>> };
>>
>> int main() {
>>      auto p = NEW((struct X) {});
>> }
>>
>
> "#pragma expand" is not standard C.  What does it mean, and what tools
> support it?
>

It can be removed in this sample. (It is not standard)

It is necessary in cake because cake is a transpiler and transformations
(typeof) inside the macro NEW need to be expanded at the generated code.

https://thradams.com/cake/playground.html?code=CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKc3RhdGljIGlubGluZSB2b2lkKiBhbGxvY2F0ZV9hbmRfY29weSh2b2lkKiBzLCBzaXplX3QgbikgewogICAgdm9pZCogcCA9IG1hbGxvYyhuKTsKICAgIGlmIChwKSB7CiAgICAgICAgbWVtY3B5KHAsIHMsIG4pOwogICAgfQogICAgcmV0dXJuIHA7Cn0KCiNkZWZpbmUgTkVXKC4uLikgKHR5cGVvZihfX1ZBX0FSR1NfXykqKSBhbGxvY2F0ZV9hbmRfY29weSgmKF9fVkFfQVJHU19fKSwgc2l6ZW9mKF9fVkFfQVJHU19fKSkKI3ByYWdtYSBleHBhbmQgTkVXCgpzdHJ1Y3QgWCB7CiAgICBjb25zdCBpbnQgaTsKfTsKCmludCBtYWluKCkgeyAKICAgIGF1dG8gcCA9IE5FVygoc3RydWN0IFgpIHt9KTsgICAgIAp9Cg%3D%3D&to=-1&options=

Re: Used-Defined Builtins

<uqc7nf$1c1qk$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33127&group=comp.lang.c#33127

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Used-Defined Builtins
Date: Mon, 12 Feb 2024 01:44:30 -0300
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <uqc7nf$1c1qk$3@dont-email.me>
References: <uq4fbf$2gc02$1@dont-email.me> <uq5erg$2m46u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 12 Feb 2024 04:44:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="0fdb2e3a634e10568940305ab09526a7";
logging-data="1443668"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FuO+IWHTgY5XSATrekzP3uUFrkaSchdQ="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:V5AlGSuIGIyj2pe7MXkLvQXE5XY=
In-Reply-To: <uq5erg$2m46u$1@dont-email.me>
Content-Language: en-GB
 by: Thiago Adams - Mon, 12 Feb 2024 04:44 UTC

Em 2/9/2024 12:03 PM, Thiago Adams escreveu:
> Em 2/9/2024 3:05 AM, Lawrence D'Oliveiro escreveu:
>> Modula-2 had this interesting feature where, for example if you had a
>> pointer variable whose pointed-to objects were of type T
>>
>>      VAR
>>          ptr : POINTER TO T;
>>
>> and you had a statement like
>>
>>      ptr := NEW(T);
>>
>> then the compiler would translate the “NEW(T)” into “ALLOCATE(TSIZE(T))”,
>> where the “ALLOCATE” function was not actually provided by the language,
>> but had to be defined/introduced in the current scope somehow (perhaps
>> IMPORTed from some implementation-provided library).
>>
>> This allowed for memory allocations (and also deallocations) to be
>> expressed in a type-safe fashion, while still leaving the low-level
>> details of memory management up to some library/user code that was not an
>> integral part of the language implementation.
>>
>> So you had a builtin function, NEW(), that could take a type as an
>> argument and return a result of the appropriate pointer type, which a
>> normal user-defined function could not do, but the compiler would
>> delegate
>> the main work to a lower-level function that didn’t need to know anything
>> about the language type system, it only needed to know how much memory to
>> allocate, and would return an untyped pointer, which the compiler would
>> then take care of turning into a pointer of the required type.
>>
>> C could benefit from some similar high-level+low-level layering like
>> this,
>> don’t you think?
>
>
>
> This is C23 code
>
>
> #include <stdlib.h>
> #include <string.h>
>
> static inline void* allocate_and_copy(void* s, size_t n) {
>     void* p = malloc(n);
>     if (p) {
>         memcpy(p, s, n);
>     }
>     return p;
> }
>
> #define NEW(...) (typeof(__VA_ARGS__)*)
> allocate_and_copy(&(__VA_ARGS__), sizeof(__VA_ARGS__))
> #pragma expand NEW
>
> struct X {
>     const int i;
> };
>
> int main() {
>     auto p = NEW((struct X) {});
> }
>
>
>
> https://thradams.com/cake/playground.html?code=CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKc3RhdGljIGlubGluZSB2b2lkKiBhbGxvY2F0ZV9hbmRfY29weSh2b2lkKiBzLCBzaXplX3QgbikgewogICAgdm9pZCogcCA9IG1hbGxvYyhuKTsKICAgIGlmIChwKSB7CiAgICAgICAgbWVtY3B5KHAsIHMsIG4pOwogICAgfQogICAgcmV0dXJuIHA7Cn0KCiNkZWZpbmUgTkVXKC4uLikgKHR5cGVvZihfX1ZBX0FSR1NfXykqKSBhbGxvY2F0ZV9hbmRfY29weSgmKF9fVkFfQVJHU19fKSwgc2l6ZW9mKF9fVkFfQVJHU19fKSkKI3ByYWdtYSBleHBhbmQgTkVXCgpzdHJ1Y3QgWCB7CiAgICBjb25zdCBpbnQgaTsKfTsKCmludCBtYWluKCkgeyAKICAgIGF1dG8gcCA9IE5FVygoc3RydWN0IFgpIHt9KTsgICAgIAp9Cg%3D%3D&to=-1&options=
>
> https://godbolt.org/z/b7z8YndWb
>
>

Initially in cake, new was an operator.
The reason it was removed is because I didn't find a good way of inform
the "allocator" function.

In C++ for instance, new is very complicated with several overrides.

The open problem is how to allocate an object in heap that have constant
members.

struct X {
const int type;
};

how to create struct X on heap?

this macro solves this problem.

I am not using this macro in production.(maybe because I am not using c23)

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor