lib: add generic resource allocator

This commit is contained in:
Matheus
2026-01-05 18:14:33 -03:00
parent 152f1ef94a
commit 832836d010
2 changed files with 90 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
#ifndef _RESOURCE_ALLOCATOR_H
#define _RESOURCE_ALLOCATOR_H
// simple FIFO allocator for an arbritary resource
#include <kernel/scheduler.h>
#include <mutex.h>
// TODO move this to a proper dequeue abstraction
typedef struct resource_allocator_waiter_t {
size_t requested;
thread_t *thread;
struct resource_allocator_waiter_t *next;
} resource_allocator_waiter_t;
typedef struct {
mutex_t mutex;
resource_allocator_waiter_t *queue;
resource_allocator_waiter_t *tail;
size_t resource_current;
size_t allocate_max;
} resource_allocator_t;
void resource_allocator_init(resource_allocator_t *allocator, size_t resource_total, size_t allocate_max);
void resource_allocate(resource_allocator_t *allocator, size_t count);
void resource_free(resource_allocator_t *allocator, size_t count);
#endif