Implement storedevs, prepare to port littlefs
This commit is contained in:
57
kernel/storedev/storedev.c
Normal file
57
kernel/storedev/storedev.c
Normal file
@ -0,0 +1,57 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "storedev.h"
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "kprintf.h"
|
||||
#include "errors.h"
|
||||
#include "dlmalloc/malloc.h"
|
||||
#include "ramsd.h"
|
||||
#include "util/util.h"
|
||||
|
||||
StoreDevList STOREDEV_LIST;
|
||||
|
||||
void storedev_init(void) {
|
||||
spinlock_init(&STOREDEV_LIST.spinlock);
|
||||
STOREDEV_LIST.head = NULL;
|
||||
|
||||
LOG("storedev", "init\n");
|
||||
}
|
||||
|
||||
StoreDev *storedev_create(int32_t sdtype, void *extra) {
|
||||
StoreDev *sd = dlmalloc(sizeof(*sd));
|
||||
if (sd == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
spinlock_acquire(&STOREDEV_LIST.spinlock);
|
||||
|
||||
switch (sdtype) {
|
||||
case STOREDEV_RAMSD: {
|
||||
spinlock_init(&sd->spinlock);
|
||||
sd->sdtype = STOREDEV_RAMSD;
|
||||
sd->init = &ramsd_init;
|
||||
sd->cleanup = &ramsd_cleanup;
|
||||
sd->read = &ramsd_read;
|
||||
sd->write = &ramsd_write;
|
||||
|
||||
int32_t err = sd->init(sd, extra);
|
||||
if (err != E_OK) {
|
||||
spinlock_release(&STOREDEV_LIST.spinlock);
|
||||
return NULL;
|
||||
}
|
||||
LL_APPEND(STOREDEV_LIST.head, sd);
|
||||
} break;
|
||||
default:
|
||||
spinlock_release(&STOREDEV_LIST.spinlock);
|
||||
return NULL;
|
||||
}
|
||||
spinlock_release(&STOREDEV_LIST.spinlock);
|
||||
return sd;
|
||||
}
|
||||
|
||||
int32_t storedev_delete(StoreDev *sd) {
|
||||
spinlock_acquire(&STOREDEV_LIST.spinlock);
|
||||
LL_REMOVE(STOREDEV_LIST.head, sd);
|
||||
int32_t err = sd->cleanup(sd);
|
||||
spinlock_release(&STOREDEV_LIST.spinlock);
|
||||
}
|
Reference in New Issue
Block a user