wlad-svennik / type-segregated-pools

An implementation of the memory-management method in floooh's "Handles are the better pointers"

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is an implementation of the ideas in floooh's essay Handles are the better pointers.

The idea is to use an alternative to pointers and heaps for managing memory, which I'll call type segregated pools. These are fixed-size arrays that hold elements of a fixed type. Instead of passing pointers around, you pass around handles that contain indices into these arrays. The style of programming with type segregated pools is similar to typical malloc() and free() based programming in C, but allocates and frees from pools of the corresponding type. The essay proposes a heuristic for catching use-after-free errors and even memory leaks.

  • For use-after-free detection, it proposes attaching a uniqueness ID to each handle, and a corresponding uniqueness ID to each element of the pool arrays. If these don't match when a user dereferences a handle then this proves that a use-after-free error has taken place.
  • If more memory is allocated than there is room in the array, then this could indicate a memory leak has taken place.

The author also claims that it has better cache locality than C++'s smart pointers.

This implementation adds exception-safety, which is important in any language with exception-handling like C++. It accomplishes this with with statements. In C++ you would use destructors.

Here is an example of how to use this implementation of type-segregated pools:

myPool = Pool[int](100, constructor=int)
myHandle = myPool.alloc()
print(myPool[myHandle])
myPool.free(myHandle)

floatPool = Pool[float](100)
strPool = Pool[str](100)
with ScopedHandle(floatPool, lambda: 1.0) as float_handle,\
        ScopedHandle(strPool, lambda: "blabla") as str_handle:
            print(floatPool[float_handle])
            print(strPool[str_handle])

About

An implementation of the memory-management method in floooh's "Handles are the better pointers"


Languages

Language:Python 100.0%