Manage int IDs via id_alloc
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m31s

This commit is contained in:
2026-02-22 20:40:12 +01:00
parent 8fc5418915
commit 084809ac99
13 changed files with 203 additions and 70 deletions

1
kernel/id/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.o

52
kernel/id/id_alloc.c Normal file
View File

@@ -0,0 +1,52 @@
#include <id/id_alloc.h>
#include <libk/bm.h>
#include <libk/std.h>
#include <mm/liballoc.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
bool id_alloc_init (struct id_alloc* ida, size_t nbits) {
size_t buffer_size = (nbits + 7) / 8;
uint8_t* buffer = malloc (buffer_size);
if (buffer == NULL)
return false;
bm_init (&ida->bm, buffer, nbits);
ida->lock = SPIN_LOCK_INIT;
return true;
}
void id_alloc_fini (struct id_alloc* ida) {
free (ida->bm.base);
ida->bm.base = NULL;
ida->bm.nbits = 0;
}
int id_alloc (struct id_alloc* ida) {
spin_lock (&ida->lock);
for (size_t bit = 0; bit < ida->bm.nbits; bit++) {
if (!bm_test (&ida->bm, bit)) {
bm_set (&ida->bm, bit);
spin_unlock (&ida->lock);
return (int)bit;
}
}
spin_unlock (&ida->lock);
return -ST_OOM_ERROR;
}
void id_free (struct id_alloc* ida, int id) {
if (id < 0)
return;
spin_lock (&ida->lock);
bm_clear (&ida->bm, (size_t)id);
spin_unlock (&ida->lock);
}

21
kernel/id/id_alloc.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef _KERNEL_ID_ID_ALLOC_H
#define _KERNEL_ID_ID_ALLOC_H
#include <libk/bm.h>
#include <libk/std.h>
#include <sync/spin_lock.h>
struct id_alloc {
struct bm bm;
spin_lock_t lock;
};
int id_alloc (struct id_alloc* ida);
void id_free (struct id_alloc* ida, int id);
bool id_alloc_init (struct id_alloc* ida, size_t nbits);
void id_alloc_fini (struct id_alloc* ida);
#endif // _KERNEL_ID_ID_ALLOC_H

3
kernel/id/src.mk Normal file
View File

@@ -0,0 +1,3 @@
c += id/id_alloc.c
o += id/id_alloc.o