Skip to content
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

Write a page on memory management, allocator scopes, for temporary allocator and arenas #58

Open
joshring opened this issue Oct 4, 2024 · 5 comments

Comments

@joshring
Copy link
Contributor

joshring commented Oct 4, 2024

We could do with a dedicated section on memory allocation, including allocator scopes, how to use the temporary allocator for which automatically frees

Expanding this content of this into a section like defer etc

include

@tomaskallup
Copy link
Contributor

tomaskallup commented Oct 5, 2024

My initial thoughts on this are to have multiple sections:

Allocators

Section describing all the current allocators with an example of how to acquire it and brief info of how it works.

Using allocators

This should describe how to actually use an allocators once you have it. @pool and its friends, probably even how to supply the allocator to a std initializer (for example to allocate a List or something like that)

Memory allocation & freeing

This should maybe be first and should expand on the section you linked, mainly describing how you can allocate a piece of memory the C way (not caring about allocators, just using the default heap) and how to free it afterwards. This may sound simple to some, but will probably help most people get started with the basics.
Ideally this should also demonstrate how to use defer & try defer (or catch defer? Not sure what it's called) to free the allocated memory in some simple examples.

Also thinking about this, there should maybe be a small sub-section showing how to free stuff you get from std (DString, List, ...).

@joshring
Copy link
Contributor Author

joshring commented Oct 5, 2024

I think with defer it's helpful but could be distracting, we should try and show the non-defer option then introduce the defer option with a link to that section then we can "take people with us" through that jump.

Seems like a good set of ideas 👍 so far, I am currently on step 0, getting some more understanding of them at the moment (which will probably shape the content quite a bit based on what I found hard!)

@joshring
Copy link
Contributor Author

joshring commented Oct 5, 2024

today I learned:
using an arena outside of a @scoped block

module test;
import std::io;

fn void main()
{

    DynamicArenaAllocator dynamic_arena;
    dynamic_arena.init(4096, allocator::heap());
   
    double *f = allocator::alloc(&dynamic_arena, double);
    io::printfn("is this valid memory? %s", *f);

    // TODO test out
    /// double f[] = allocator::alloc_array(&dynamic_arena, double, 100);


    // Release any dynamic arena memory.
    dynamic_arena.free();
}

also worth mentioning that you can do this globally inside main, then any nested function has the default allocator changed

@joshring
Copy link
Contributor Author

Temporary allocator:

It's like an arena allocator, but you can push the current mark of the arena and automatically pop it using

@pool()
{
  ... temp allocations ...
}; // Temp allocations all are invalidated when exiting the scope
// variables declared inside the scope are no longer available, use after free is not possible

@joshring
Copy link
Contributor Author

Are there any good tools/techniques for finding/monitoring memory leaks or unsafe memory practices?

One simple thing is just wrap your call from main in @report_heap_allocs_in_scope like:

fn void main()
{
  mem::@report_heap_allocs_in_scope()
  {
    start_stuff();
    cleanup();
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants