Memory Management

From Adam Colton
Jump to navigation Jump to search

In some cases manual memory management is required but in most cases garbage collection is preferred, so we need to support both.

Strict Domains

I think the best way to accomplish this also simplifies other concerns. A 'process' will have a single memory manager. You cannot mix strategies within one process. And you cannot share memory with another process. Communication between processes is done with message passing.

Hrm...

On the surface, this could work like Rust. Pass from one process to another and ownership of that memory changes hands. It is no longer available in the original process and the new owner is responsible for freeing the memory. But how do we deal with pointers within the message?

  • walk the graph and transfer the pointers
  • disallow messages with pointers

Fat/Boundary Pointers

Pretty sure I don't like this, but I need to work through it before I discount it.

When memory under one controller points to memory under another controller there needs to be a sort of handshake. The pointer needs to inform the pointee at assignment. And the pointer needs to inform the pointee when the pointer is freed. The pointee needs to inform the pointer if it's freeing the memory.

Semi-Count

The top level memory manager is a memory pool. This just reduces the calls to the OS. It performs alloc and dealloc. But it also implements a counter.

For simplicity I want to assume that a level down everything is either manual or GC. In GC, the first time a memory address is encountered, it calls alloc. When it's collected, call dealloc. In manual, alloc and dealloc are just called normally.

This means that I don't have to worry about pointers crossing memory domain boundaries. If a manual domain allocates something and passes it into a GC domain, the GC will see it as new memory and call alloc. So that memory will be double allocated. But this means that it will only reach dealloc 0 when both domains call dealloc.

The problem is overhead. For each address, I need a counter. This should probably be a header. But it would be nice if there was a way to assume one allocation until that was no longer true and only check that at deallocation. For a GC, tracking if an address has been encountered doesn't add much. But for manual memory management, this could get tricky.