June 12

On Wednesday, 12 June 2024 at 21:59:54 UTC, drug007 wrote:

> >

Yes, but you get all the benefits of double[] for free if you do it that way, including the more concise foo[10] syntax.

I meant you do not need to add ptr field at all

I see. You're right. I thought it would be easier for someone new to the language to read more explicit code rather than assuming knowledge about data.ptr. In practice it's better to not have a ptr field.

June 13

On Wednesday, 12 June 2024 at 16:50:04 UTC, Vinod K Chandran wrote:

>

On Wednesday, 12 June 2024 at 01:35:26 UTC, monkyyy wrote:

>

rather then worring about the gc, just have 95% of data on the stack

How's that even possible ? AFAIK, we need heap allocated memory in order to make GUI lib as a DLL. So creating things in heap and modify it, that's the nature of my project.

gui is a bit harder and maybe aim for 70%

but if you went down the rabbit hole you could have strings be in an "arena" of which the first 5000 chars are a global scope array; or full me and just an array that doesnt expand

June 13
Lance Bachmeier kirjoitti 13.6.2024 klo 1.32:
> 
> Why would it be different from calling malloc and free manually? I guess I'm not understanding, because you put the same calls to malloc and free that you'd otherwise be doing inside this and ~this.

Because with `SafeRefCounted`, you have to decide the size of your allocations at compile time, meaning you need to do a varying number of `malloc`s and `free`s to vary the size of your allocation at runtime. Even if you were to use templates to vary the type of `SafeRefCounted` object based on size of your allocation, the spec puts an upper bound of 16MiB to size of a static array.

So for example, if you have a program that sometimes needs 600Mib and sometimes needs 1100MiB, you can in any case allocate all that in one go with one `malloc` or one `new`, but you'll need at least 38/59 `SafeRefCounted` static arrays, and therefore `malloc`s, to accomplish the same.
June 13
Dukc kirjoitti 13.6.2024 klo 10.18:
> So for example, if you have a program that sometimes needs 600Mib and sometimes needs 1100MiB, you can in any case allocate all that in one go with one `malloc` or one `new`, but you'll need at least 38/59 `SafeRefCounted` static arrays, and therefore `malloc`s, to accomplish the same.

Now granted, 16MiB (or even smaller amounts, like 256 KiB) sounds big enough that it probably isn't making a difference since it's a long way into multiples of page size anyway. But I'm not sure.
June 14

On Thursday, 13 June 2024 at 07:18:48 UTC, Dukc wrote:

>

Lance Bachmeier kirjoitti 13.6.2024 klo 1.32:

>

Why would it be different from calling malloc and free manually? I guess I'm not understanding, because you put the same calls to malloc and free that you'd otherwise be doing inside this and ~this.

Because with SafeRefCounted, you have to decide the size of your allocations at compile time, meaning you need to do a varying number of mallocs and frees to vary the size of your allocation at runtime. Even if you were to use templates to vary the type of SafeRefCounted object based on size of your allocation, the spec puts an upper bound of 16MiB to size of a static array.

We must be talking about different things. You could, for instance, call a function in a C library to allocate memory at runtime. That function returns a pointer and you pass it to SafeRefCounted to ensure it gets freed. Nothing is known about the allocation at compile time. This is in fact my primary use case - allocating an opaque struct allocated by a C library, and not wanting to concern myself with freeing it when I'm done with it.

June 14
Lance Bachmeier kirjoitti 14.6.2024 klo 4.23:
> We must be talking about different things. You could, for instance, call a function in a C library to allocate memory at runtime. That function returns a pointer and you pass it to SafeRefCounted to ensure it gets freed. Nothing is known about the allocation at compile time. This is in fact my primary use case - allocating an opaque struct allocated by a C library, and not wanting to concern myself with freeing it when I'm done with it.

Using a raw pointer as the `SafeRefCounted` type like that isn't going to work. `SafeRefCounted` will free only the pointer itself at the end, not the struct it's referring to. If you use some sort of RAII wrapper for the pointer that `free`s it at it's destructor, then it'll work - maybe that's what you meant.
June 14

On Friday, 14 June 2024 at 07:52:35 UTC, Dukc wrote:

>

Lance Bachmeier kirjoitti 14.6.2024 klo 4.23:

>

We must be talking about different things. You could, for instance, call a function in a C library to allocate memory at runtime. That function returns a pointer and you pass it to SafeRefCounted to ensure it gets freed. Nothing is known about the allocation at compile time. This is in fact my primary use case - allocating an opaque struct allocated by a C library, and not wanting to concern myself with freeing it when I'm done with it.

Using a raw pointer as the SafeRefCounted type like that isn't going to work. SafeRefCounted will free only the pointer itself at the end, not the struct it's referring to. If you use some sort of RAII wrapper for the pointer that frees it at it's destructor, then it'll work - maybe that's what you meant.

See the example I posted elsewhere in this thread: https://forum.dlang.org/post/mwerxaolbkuxlgfepzwc@forum.dlang.org

I defined

@nogc ~this() {
  free(ptr);
  printf("Data has been freed\n");
}

and that gets called when the reference count hits zero.

June 14
bachmeier kirjoitti 14.6.2024 klo 16.48:
> See the example I posted elsewhere in this thread: https://forum.dlang.org/post/mwerxaolbkuxlgfepzwc@forum.dlang.org
> 
> I defined
> 
> ```
> @nogc ~this() {
>    free(ptr);
>    printf("Data has been freed\n");
> }
> ```
> 
> and that gets called when the reference count hits zero.

Oh sorry, missed that.
1 2 3 4
Next ›   Last »