-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RAII - memory management #658
Conversation
I generally like the idea, but (at least) your Also: I first thought, that you do not have a method for move assignment (of existing variables), but it's of course possible to make an Also Also: Your RAII code does not do any memory allocation, only the not-shared_ptr test script. Maybe clarify on that topic, to avoid stirring up any memory management debates Also Also Also: you commented out some |
__move
__copy
__init Also Also: The point of the contribution is to add some metamethods to enable RAII, without introducing breaking changes. This leaves memory allocation using the stdlib C library in the hands of the programmer. Also Also Also: right, the commented out platform code is a separate issue that I forgot to undo. |
As I said on Zulip, I appreciate the work that @renehiemstra has put into this and agree that something along these lines would be useful. Unfortunately I am continuing to run out of time to actually look at this in depth. I think it would be nice to make sure that this can capture basic patterns similar to I have enabled the CI to run so that you can at least see those results. I may have to re-enable it on each push since this is a first-time contribution. Edit: I also wanted to check that the metaprogramming APIs are compositional: i.e., you can create a |
I was able to implement a I also implemented a
and / or
The arguments of It can also be used on object construction,
which is the same as writing
The I think the above covers all the use cases of smart containers and pointers in c++ (e.g. I still need to make the metamethods work properly in the case of compositional API's, like you mentioned ( Also, I need to handle some corner cases. In Terra its allowed to combine a logical statement in a return statment, e.g.
I am not sure how to deal with these yet, because this is runtime information. |
Reopened in #659 |
This pull request adds memory management to terra using the concept of RAII.
The design adds the following four metamethods to the language that enable the implementation of smart (shared) pointer types:
__init(self : &A)
__move(self : &A)
__copy(self : &A)
__dtor(self : &A)
If implemented, these methods are inserted judiciously during the type checking phase, implemented in
terralub.lua
. All these metamethods can be implemented as macro's or as terra functions.The design does not introduce any breaking changes. No new keywords are introduced. Heap resources are acquired and released using the regular C stdlib functions such
malloc
andfree
.I will explain the metamethods in some more depth:
__init
is used to initialize pointer variables to nil, making them safe to delete. The implementation checks for an __init metamethod in any defvar statement:var a : A
and applies it if it exists. If there is an initializer,var a : A = ...
then the implementation does not search for an__init
metamethod. A deferred call to__dtor
is added in this case and a call to__copy
if the right-hand-side involves a struct variable.__dtor
is used to free heap memory. A defer node to this metamethod is added during type-checking such that__dtor
is called at the end of a scope.__dtor
is also applied before any assignment, e.g.a = alloc(...)
, to free old resources.__move
is needed in order to return from a function. It moves resources to a temporary variable , and sets the old pointer variable tonil
such that the deferred destructor call does not free the heap resources.__move
will be called on every returned variable (that implements it) in a return statement.__copy
enables side effects in a statementvar b = a
orb = a
, such as increasing a reference counter. It acts solely on the right-hand-side variable in the assignment expression.I have added a test file smartptr.t in the tests folder. It contains an implementation of a smart shared pointer and two integration tests that test all four metamethods. These tests print out the control flow of the program to check when each meta method is invoked.
All existing tests in the tests folder pass on my machine (except for a few that did not pass prior to the changes)
If the design is accepted, there are a few things left to do:
prettystring
in terralib.lua.I am happy to work further on improving the design where needed.