Thread overview | ||||||
---|---|---|---|---|---|---|
|
September 12, 2013 may gc free malloced memory? | ||||
---|---|---|---|---|
| ||||
Some C function malloc-ed memory. This memory should be freeed much later. I don't want to manually call C function to free this memory in some point later, so may I in some way ask gc to free this memory using something like addRoot(for instance) or else or the true way is to copy malloc-ed memory to gc-allocated memory and free malloc-ed memory at once? Like: ubyte data* = cfunction_allocates_memory(); auto gcmemory = data[0..length(data)]; cfunction_frees_memory(data); // work with gcmemory only or ubyte data* = cfunction_allocates_memory(); GC.someUnknownToMeFunction(data); // now gc will control this memory |
September 12, 2013 Re: may gc free malloced memory? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alexandr Druzhinin | On Thursday, 12 September 2013 at 05:36:31 UTC, Alexandr Druzhinin wrote:
> Some C function malloc-ed memory. This memory should be freeed much later. I don't want to manually call C function to free this memory in some point later, so may I in some way ask gc to free this memory using something like addRoot(for instance) or else or the true way is to copy malloc-ed memory to gc-allocated memory and free malloc-ed memory at once? Like:
>
> ubyte data* = cfunction_allocates_memory();
> auto gcmemory = data[0..length(data)];
> cfunction_frees_memory(data);
> // work with gcmemory only
>
> or
>
> ubyte data* = cfunction_allocates_memory();
> GC.someUnknownToMeFunction(data); // now gc will control this memory
No.
Only free can be used with malloc. The memory comes from distinct pools.
Another option could be to use "GC.malloc", and memcpy your old mmory into your new memory, free the old memory, and use your new block. GC.malloc, as the name suggests, is a malloc, but done by the GC.
|
September 12, 2013 Re: may gc free malloced memory? | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | On Thursday, 12 September 2013 at 05:59:33 UTC, monarch_dodra wrote: > On Thursday, 12 September 2013 at 05:36:31 UTC, Alexandr Druzhinin wrote: >> Some C function malloc-ed memory. This memory should be freeed much later. I don't want to manually call C function to free this memory in some point later, so may I in some way ask gc to free this memory using something like addRoot(for instance) or else or the true way is to copy malloc-ed memory to gc-allocated memory and free malloc-ed memory at once? Like: >> >> ubyte data* = cfunction_allocates_memory(); >> auto gcmemory = data[0..length(data)]; >> cfunction_frees_memory(data); >> // work with gcmemory only >> >> or >> >> ubyte data* = cfunction_allocates_memory(); >> GC.someUnknownToMeFunction(data); // now gc will control this memory > > No. > > Only free can be used with malloc. The memory comes from distinct pools. > > Another option could be to use "GC.malloc", and memcpy your old mmory into your new memory, free the old memory, and use your new block. GC.malloc, as the name suggests, is a malloc, but done by the GC. You could also use some kind of helper class. Perhaps Phobos has a facility for this, but to illustrate the idea: http://dpaste.dzfl.pl/805a61c0 However note that the memory isn't guaranteed to be freed this way. Only if the GC heap gets full and the collector runs. |
September 12, 2013 Re: may gc free malloced memory? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | 12.09.2013 14:45, Rene Zwanenburg пишет:
> On Thursday, 12 September 2013 at 05:59:33 UTC, monarch_dodra
> wrote:
>> On Thursday, 12 September 2013 at 05:36:31 UTC, Alexandr Druzhinin wrote:
>>> Some C function malloc-ed memory. This memory should be freeed much
>>> later. I don't want to manually call C function to free this memory
>>> in some point later, so may I in some way ask gc to free this memory
>>> using something like addRoot(for instance) or else or the true way is
>>> to copy malloc-ed memory to gc-allocated memory and free malloc-ed
>>> memory at once? Like:
>>>
>>> ubyte data* = cfunction_allocates_memory();
>>> auto gcmemory = data[0..length(data)];
>>> cfunction_frees_memory(data);
>>> // work with gcmemory only
>>>
>>> or
>>>
>>> ubyte data* = cfunction_allocates_memory();
>>> GC.someUnknownToMeFunction(data); // now gc will control this memory
>>
>> No.
>>
>> Only free can be used with malloc. The memory comes from distinct pools.
>>
>> Another option could be to use "GC.malloc", and memcpy your old mmory
>> into your new memory, free the old memory, and use your new block.
>> GC.malloc, as the name suggests, is a malloc, but done by the GC.
>
> You could also use some kind of helper class. Perhaps Phobos has
> a facility for this, but to illustrate the idea:
> http://dpaste.dzfl.pl/805a61c0
>
> However note that the memory isn't guaranteed to be freed this
> way. Only if the GC heap gets full and the collector runs.
Ok. I just think that copying from one pool to another is excessive and may be there is a some way to avoid it.
Thanks for the answers!
|
Copyright © 1999-2021 by the D Language Foundation