Open
Conversation
Right now the problem with Json is that it's using the Vec<T> on raw functions, which require the GLOBAL_ALLOCATOR, however it has not yet been declared at that point.
Author
|
At the moment, inner values implementing Drop are not dropped. struct StringHolder {
s: String;
}
fn test_string_holder(allocator: DebugAllocator*) {
var h = StringHolder {
s: String::from("hello")
};
}In the code above, the variable impl Drop for Vec<T: Drop>: {
fn drop(self) {
// Iterate all and call element.drop();
}
}But then issues with moving values start arising, such as: fn test_vec_box_string(allocator: DebugAllocator*) -> Box<String> {
var vec = Vec<String>::new();
for i in 0..100 {
vec.push(String::from("hello"));
}
return vec.get(4);
}In the code above, if the array is dropped and all elements are dropped, the element 4 would become invalid. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The purpose of this PR is to improve the memory management.
Right now, Zen C is bound to malloc/free functions. Well, on a big project, this might be a very big problem. To fix this, I suggest being able to implement custom allocators and be able to specify which allocator should be used when we need to handle manual memory allocation.
The problem that we get out of this is knowing which allocator was used for each value, and right now the only option I see is keeping a reference to the allocator on the value, which adds a tremendous overhead for tiny values:
So what also could be done is having a mirrored type that doesn't have the allocator like this:
Still I highly suggest replacing the current alloc functions into allocators. Implementing a Debug allocator that keeps track and detects memory leaks and stuff.
Also, Box now implements the
Droptrait. No need to manually call thefree()function, and I highly suggest its removal.