Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 21, 2013 Call destructor directly. | ||||
---|---|---|---|---|
| ||||
I'm implementing some custom memory allocator, is possible to call an object destructor directly? For example void deallocate(T)(T object) { assert(object !is null); object.~this(); rawDeallocate(cast(void *)object); } |
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Monday, 21 October 2013 at 02:06:02 UTC, Agustin wrote: > I'm implementing some custom memory allocator, is possible to call an object destructor directly? destroy(object); destroy is in the automatically imported object.dm so you don't have to import anything, The source code is in dmd2/src/druntime/src/object_.d, there's a few overloads if you are curious how it is implemented. Short answer is there's a pointer to the destructor in the TypeInfo and destroy calls it. |
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 21 October 2013 at 02:17:54 UTC, Adam D. Ruppe wrote:
> On Monday, 21 October 2013 at 02:06:02 UTC, Agustin wrote:
>> I'm implementing some custom memory allocator, is possible to call an object destructor directly?
>
>
> destroy(object);
>
> destroy is in the automatically imported object.dm so you don't have to import anything,
>
> The source code is in dmd2/src/druntime/src/object_.d, there's a few overloads if you are curious how it is implemented. Short answer is there's a pointer to the destructor in the TypeInfo and destroy calls it.
Thank you :)
|
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Monday, 21 October 2013 at 02:26:03 UTC, Agustin wrote: > On Monday, 21 October 2013 at 02:17:54 UTC, Adam D. Ruppe wrote: >> On Monday, 21 October 2013 at 02:06:02 UTC, Agustin wrote: >>> I'm implementing some custom memory allocator, is possible to call an object destructor directly? >> >> >> destroy(object); >> >> destroy is in the automatically imported object.dm so you don't have to import anything, >> >> The source code is in dmd2/src/druntime/src/object_.d, there's a few overloads if you are curious how it is implemented. Short answer is there's a pointer to the destructor in the TypeInfo and destroy calls it. > > Thank you :) What about constructor?. My current code is: T allocate(T : Object, A...)(auto ref A arguments) { auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof); // Return void* emplace!T(cast(T *)pMemory, arguments); return cast(T) pMemory; } Doesn't seems to work, and i can't find any good documentation about it. |
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Monday, October 21, 2013 05:07:02 Agustin wrote:
> What about constructor?. My current code is:
>
> T allocate(T : Object, A...)(auto ref A arguments) {
> auto pMemory = rawAllocate(__traits(classInstanceSize, T),
> T.alignof); // Return void*
>
> emplace!T(cast(T *)pMemory, arguments);
> return cast(T) pMemory;
> }
>
> Doesn't seems to work, and i can't find any good documentation about it.
IIRC, the constructor should be name __ctor.
- Jonathan M Davis
|
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Monday, 21 October 2013 at 03:46:33 UTC, Jonathan M Davis wrote:
> On Monday, October 21, 2013 05:07:02 Agustin wrote:
>> What about constructor?. My current code is:
>>
>> T allocate(T : Object, A...)(auto ref A arguments) {
>> auto pMemory = rawAllocate(__traits(classInstanceSize, T),
>> T.alignof); // Return void*
>>
>> emplace!T(cast(T *)pMemory, arguments);
>> return cast(T) pMemory;
>> }
>>
>> Doesn't seems to work, and i can't find any good documentation
>> about it.
>
> IIRC, the constructor should be name __ctor.
>
> - Jonathan M Davis
no property 'opCall' for type 'Main.MyClass' :(
|
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Monday, 21 October 2013 at 03:50:24 UTC, Agustin wrote: > On Monday, 21 October 2013 at 03:46:33 UTC, Jonathan M Davis wrote: >> On Monday, October 21, 2013 05:07:02 Agustin wrote: >>> What about constructor?. My current code is: >>> >>> T allocate(T : Object, A...)(auto ref A arguments) { >>> auto pMemory = rawAllocate(__traits(classInstanceSize, T), >>> T.alignof); // Return void* >>> >>> emplace!T(cast(T *)pMemory, arguments); >>> return cast(T) pMemory; >>> } >>> >>> Doesn't seems to work, and i can't find any good documentation >>> about it. >> >> IIRC, the constructor should be name __ctor. >> >> - Jonathan M Davis > > no property 'opCall' for type 'Main.MyClass' :( Trait allMember return "__ctor", but seems like i cannot call it directly: (cast(T)pMemory).__ctor(arguments); // Being pMemory void* |
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Monday, October 21, 2013 05:53:46 Agustin wrote: > On Monday, 21 October 2013 at 03:50:24 UTC, Agustin wrote: > > On Monday, 21 October 2013 at 03:46:33 UTC, Jonathan M Davis > > > > wrote: > >> On Monday, October 21, 2013 05:07:02 Agustin wrote: > >>> What about constructor?. My current code is: > >>> T allocate(T : Object, A...)(auto ref A arguments) { > >>> > >>> auto pMemory = rawAllocate(__traits(classInstanceSize, T), > >>> > >>> T.alignof); // Return void* > >>> > >>> emplace!T(cast(T *)pMemory, arguments); > >>> return cast(T) pMemory; > >>> > >>> } > >>> > >>> Doesn't seems to work, and i can't find any good documentation about it. > >> > >> IIRC, the constructor should be name __ctor. > >> > >> - Jonathan M Davis > > > > no property 'opCall' for type 'Main.MyClass' :( > > Trait allMember return "__ctor", but seems like i cannot call it directly: > > (cast(T)pMemory).__ctor(arguments); // Being pMemory void* If you want to see how to use emplace, I'd advise looking at std.typecons.RefCounted's implementation: https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L3505 emplace calls the constructor for you, so I don't know why you'd be trying to call it. But you can look at emplace's implementation if you want to see how to call __ctor. For structs: https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L3976 For classes: https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L4716 I don't think that it'll work if the constructor is private though, so maybe that's your problem. - Jonathan M Davis |
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Monday, 21 October 2013 at 05:17:01 UTC, Jonathan M Davis wrote: > On Monday, October 21, 2013 05:53:46 Agustin wrote: >> On Monday, 21 October 2013 at 03:50:24 UTC, Agustin wrote: >> > On Monday, 21 October 2013 at 03:46:33 UTC, Jonathan M Davis >> > >> > wrote: >> >> On Monday, October 21, 2013 05:07:02 Agustin wrote: >> >>> What about constructor?. My current code is: >> >>> T allocate(T : Object, A...)(auto ref A arguments) { >> >>> >> >>> auto pMemory = rawAllocate(__traits(classInstanceSize, T), >> >>> >> >>> T.alignof); // Return void* >> >>> >> >>> emplace!T(cast(T *)pMemory, arguments); >> >>> return cast(T) pMemory; >> >>> >> >>> } >> >>> >> >>> Doesn't seems to work, and i can't find any good documentation >> >>> about it. >> >> >> >> IIRC, the constructor should be name __ctor. >> >> >> >> - Jonathan M Davis >> > >> > no property 'opCall' for type 'Main.MyClass' :( >> >> Trait allMember return "__ctor", but seems like i cannot call it >> directly: >> >> (cast(T)pMemory).__ctor(arguments); // Being pMemory void* > > If you want to see how to use emplace, I'd advise looking at > std.typecons.RefCounted's implementation: > > https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L3505 > > emplace calls the constructor for you, so I don't know why you'd be trying to > call it. But you can look at emplace's implementation if you want to see how > to call __ctor. > > For structs: > https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L3976 > > For classes: > https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L4716 > > I don't think that it'll work if the constructor is private though, so maybe > that's your problem. > > - Jonathan M Davis I'm silly the issue was at this line auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof); emplace(pMemory, arguments); Correct was auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof); emplace(&pMemory, arguments); Thanks guys |
October 21, 2013 Re: Call destructor directly. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Agustin | On Monday, 21 October 2013 at 05:40:13 UTC, Agustin wrote: > On Monday, 21 October 2013 at 05:17:01 UTC, Jonathan M Davis wrote: >> On Monday, October 21, 2013 05:53:46 Agustin wrote: >>> On Monday, 21 October 2013 at 03:50:24 UTC, Agustin wrote: >>> > On Monday, 21 October 2013 at 03:46:33 UTC, Jonathan M Davis >>> > >>> > wrote: >>> >> On Monday, October 21, 2013 05:07:02 Agustin wrote: >>> >>> What about constructor?. My current code is: >>> >>> T allocate(T : Object, A...)(auto ref A arguments) { >>> >>> >>> >>> auto pMemory = rawAllocate(__traits(classInstanceSize, T), >>> >>> >>> >>> T.alignof); // Return void* >>> >>> >>> >>> emplace!T(cast(T *)pMemory, arguments); >>> >>> return cast(T) pMemory; >>> >>> >>> >>> } >>> >>> >>> >>> Doesn't seems to work, and i can't find any good documentation >>> >>> about it. >>> >> >>> >> IIRC, the constructor should be name __ctor. >>> >> >>> >> - Jonathan M Davis >>> > >>> > no property 'opCall' for type 'Main.MyClass' :( >>> >>> Trait allMember return "__ctor", but seems like i cannot call it >>> directly: >>> >>> (cast(T)pMemory).__ctor(arguments); // Being pMemory void* >> >> If you want to see how to use emplace, I'd advise looking at >> std.typecons.RefCounted's implementation: >> >> https://github.com/D-Programming-Language/phobos/blob/master/std/typecons.d#L3505 >> >> emplace calls the constructor for you, so I don't know why you'd be trying to >> call it. But you can look at emplace's implementation if you want to see how >> to call __ctor. >> >> For structs: >> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L3976 >> >> For classes: >> https://github.com/D-Programming-Language/phobos/blob/master/std/conv.d#L4716 >> >> I don't think that it'll work if the constructor is private though, so maybe >> that's your problem. >> >> - Jonathan M Davis > > I'm silly the issue was at this line > > auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof); > > emplace(pMemory, arguments); > > Correct was > > auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof); > > emplace(&pMemory, arguments); > > Thanks guys That didn't work, but after reading how emplace works, i had to make some changes. public T allocate(T : Object, A...)(auto ref A arguments) { auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof); assert(pMemory !is null, "Not enought memory on the allocator"); byte[] * pByteMemory = cast(byte[] *) pMemory; *pByteMemory = typeid(T).init[]; auto pObject = cast(T) pMemory; static if (is(typeof(pObject.__ctor(arguments)))) { pObject.__ctor(arguments); } return pObject; } That would work. |
Copyright © 1999-2021 by the D Language Foundation