Jump to page: 1 2 3
Thread overview
Prevent Garbage Collector
Jan 04, 2014
Jeroen Bollen
Jan 04, 2014
Adam D. Ruppe
Jan 04, 2014
Jeroen Bollen
Jan 04, 2014
Adam D. Ruppe
Jan 04, 2014
Jeroen Bollen
Jan 04, 2014
Adam D. Ruppe
Jan 04, 2014
Jeroen Bollen
Jan 04, 2014
Adam D. Ruppe
Jan 04, 2014
bearophile
Jan 04, 2014
anonymous
Jan 05, 2014
Jeroen Bollen
Jan 05, 2014
Adam D. Ruppe
Jan 05, 2014
Jeroen Bollen
Jan 05, 2014
Adam D. Ruppe
Jan 05, 2014
Jeroen Bollen
Jan 05, 2014
Adam D. Ruppe
Jan 05, 2014
Jeroen Bollen
Jan 05, 2014
Adam D. Ruppe
Jan 05, 2014
bearophile
Jan 05, 2014
Adam D. Ruppe
Jan 05, 2014
Jacob Carlborg
January 04, 2014
Is there a way to prevent the Garbage collector from running on one particular object? Something like:

int* CreatePermanentInt() {
    int i = 5;
    return &i;
}

And i wouldn't be collected after this.
January 04, 2014
On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote:
> Is there a way to prevent the Garbage collector from running on one particular object? Something like:

I would just malloc it.

int* CreatePermanentInt() {
   int* i = malloc(int.sizeof);
   *i = 5;
   return i;
}


The malloced thing itself won't be freed, nor will its contents be scanned by the gc (unless you use GC.addRange yourself) so you can use it to hack some weak references too.
January 04, 2014
On Saturday, 4 January 2014 at 17:16:53 UTC, Adam D. Ruppe wrote:
> On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote:
>> Is there a way to prevent the Garbage collector from running on one particular object? Something like:
>
> I would just malloc it.
>
> int* CreatePermanentInt() {
>    int* i = malloc(int.sizeof);
>    *i = 5;
>    return i;
> }
>
>
> The malloced thing itself won't be freed, nor will its contents be scanned by the gc (unless you use GC.addRange yourself) so you can use it to hack some weak references too.

Do I get malloc from the C library or does D also have a function for this?
January 04, 2014
On Saturday, 4 January 2014 at 17:19:30 UTC, Jeroen Bollen wrote:
> Do I get malloc from the C library or does D also have a function for this?

import core.stdc.stdlib; // malloc and free are in here

it uses it from the C library but the standard c lib is easily accessible from D in the core.stdc package.
January 04, 2014
On Saturday, 4 January 2014 at 17:22:44 UTC, Adam D. Ruppe wrote:
> On Saturday, 4 January 2014 at 17:19:30 UTC, Jeroen Bollen wrote:
>> Do I get malloc from the C library or does D also have a function for this?
>
> import core.stdc.stdlib; // malloc and free are in here
>
> it uses it from the C library but the standard c lib is easily accessible from D in the core.stdc package.

Yeah I knew about the C library, just wasn't sure if D also had it's own function.
January 04, 2014
And GC.addRange is in core.memory if you want that.

http://dlang.org/phobos/core_memory.html#addRange
January 04, 2014
On Saturday, 4 January 2014 at 17:24:24 UTC, Adam D. Ruppe wrote:
> And GC.addRange is in core.memory if you want that.
>
> http://dlang.org/phobos/core_memory.html#addRange

Would that work with structs too?

Struct* i = malloc(Struct.sizeof);
i = &Struct(params);
January 04, 2014
On Saturday, 4 January 2014 at 17:15:12 UTC, Jeroen Bollen wrote:
> Is there a way to prevent the Garbage collector from running on one particular object? Something like:
>
> int* CreatePermanentInt() {
>     int i = 5;
>     return &i;
> }
>
> And i wouldn't be collected after this.

i isn't GC managed here. It's a stack variable. Returning
references to the stack is a no-no, because the stack will be
reused.

If i was actually GC managed, it would not be collected, because
there's a reference to it:
----
int* CreatePermanentInt() {
     int* i = new int;
     *i = 5;
     return i;
}
----
January 04, 2014
Adam D. Ruppe:

> int* CreatePermanentInt() {
>    int* i = malloc(int.sizeof);
>    *i = 5;
>    return i;
> }

In D malloc needs a cast void -> T*.


In D a wrapper like this could help you avoid some mistakes making the code more DRY (untested):


T* cMalloc(T)() nothrow {
    return cast(T*)malloc(T.sizeof);
}

T* cCalloc(T)(in size_t n) nothrow {
    return cast(T*)calloc(n, T.sizeof);
}


auto i = cMalloc!int;
auto j = cCalloc!int;

Bye,
bearophile
January 04, 2014
On Saturday, 4 January 2014 at 17:37:00 UTC, Jeroen Bollen wrote:
> Would that work with structs too?
>
> Struct* i = malloc(Struct.sizeof);
> i = &Struct(params);

You don't want to take the address of a temporary, not with structs nor int. But you could copy it with *i = Struct(params) and that should be ok. There's also std.conv.emplace that might be useful.

And bearophile is right too, of course, that it will need a cast on malloc.

Struct* i = cast(Struct*) malloc(Struct.sizeof);
« First   ‹ Prev
1 2 3