Deutsch   English   Français   Italiano  
<vr1moa$1kn9g$2@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Richard Heathfield <rjh@cpax.org.uk>
Newsgroups: comp.lang.c
Subject: Re: What is your opinion about init_malloc?
Date: Fri, 14 Mar 2025 16:50:18 +0000
Organization: Fix this later
Lines: 50
Message-ID: <vr1moa$1kn9g$2@dont-email.me>
References: <vr1e67$1fa1p$1@dont-email.me>
 <vr1j5o$1jg76$1@raubtier-asyl.eternal-september.org>
 <6255bb4153bcc571db879cf847ee3a110080c4fc.camel@gmail.com>
 <vr1kiu$1knn5$1@raubtier-asyl.eternal-september.org>
 <085b1065a664645eb2fc8b3d20b20c22af2cbcb6.camel@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 14 Mar 2025 17:50:18 +0100 (CET)
Injection-Info: dont-email.me; posting-host="5fc3e144d8b4428c522cf2161043773b";
	logging-data="1727792"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18XsBZKdTIXDKXptjOEPkOAniD0Sx+YCcQjMmI4kMLjFA=="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:S1njvWPAUS61kahE6rdaKlyXyQg=
In-Reply-To: <085b1065a664645eb2fc8b3d20b20c22af2cbcb6.camel@gmail.com>
Content-Language: en-GB

On 14/03/2025 16:31, wij wrote:
> On Fri, 2025-03-14 at 17:13 +0100, Bonita Montero wrote:
>> Am 14.03.2025 um 16:56 schrieb wij:
>>
>>> If it tries to exceed 'High level assembly', it could be ridiculous.
>>> Same as C++, if it tries to be 'not-C'.
>>
>> Thiago wants to mimic construction with new,
> 
> I've been wondering how C would mimic ctor/dtor for long long time,...
> 
> void * init_malloc(size_t size, void * src) looked reasonable, and
> may be necessary in 'my assembly model'.

Why not pass it a constructor function as well? Something like:

void * init_malloc(void *src,
                    size_t size,
                    int(*ctor)(void *, const void *))
{
   void *p = malloc(size);
   if(p)
   {
     if(ctor != NULL)
     {
       if((*ctor)(p, src) == CTOR_FAIL)
       {
         free(p);
         p = NULL;
       }
     }
     else
     {
       memcpy(p, src, size);
     }
   }
   return p;
}

This way, you can do a "deep copy" instead of a naive bit blit, 
and one init_malloc fits all because you just write a new 
constructor for each type that needs one and pass a NULL 
constructor when a bit blit suffices.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within