Skip to content

Add context pointer to allocator functions#24

Open
linkdd wants to merge 1 commit into
Mintsuki:trunkfrom
linkdd:trunk
Open

Add context pointer to allocator functions#24
linkdd wants to merge 1 commit into
Mintsuki:trunkfrom
linkdd:trunk

Conversation

@linkdd

@linkdd linkdd commented Jun 16, 2026

Copy link
Copy Markdown

Context

In my kernel, I have a VTable-like interface for allocators:

typedef struct allocator allocator;
struct allocator {
  void *(*allocate)  (void *udata, usize sz);
  void *(*reallocate)(void *udata, void *ptr, usize oldsz, usize newsz);
  void  (*deallocate)(void *udata, void *ptr, usize sz);

  void *udata;
};

This allows me to implement multiple kinds of allocators (static size bump, scratch, slab, ...) and even nest them. The crucial point is the udata pointer that holds the allocator's state.

In flanterm, I can give an allocation and deallocation function, but they do not accept an equivalent to the udata pointer. This means I have to manage the allocator's state elsewhere (in static globals would be a code smell and a bug waiting to happen).

Changes

This PR adds an alloc_ctx pointer to the fb backend and pass it to the _malloc and _free functions.

To mimic previous usage, you can just pass NULL and ignore the parameter.

NB: This is a breaking change as it modifies the public API.

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

Successfully merging this pull request may close these issues.

1 participant