Working port of Little FS
This commit is contained in:
@ -7,13 +7,18 @@
|
||||
#include "storedev.h"
|
||||
#include "hal/hal.h"
|
||||
#include "util/util.h"
|
||||
#include "kprintf.h"
|
||||
|
||||
int32_t ramsd_init(struct StoreDev *sd, void *extra) {
|
||||
RamSdInitExtra *e = extra;
|
||||
sd->sd.ramsd.capacity = e->capacity;
|
||||
sd->sd.ramsd.buffer = dlmalloc(sd->sd.ramsd.capacity);
|
||||
if (sd->sd.ramsd.buffer == NULL) {
|
||||
return E_NOMEMORY;
|
||||
if (e->preallocbuffer != NULL) {
|
||||
sd->sd.ramsd.buffer = e->preallocbuffer;
|
||||
} else {
|
||||
sd->sd.ramsd.buffer = dlmalloc(sd->sd.ramsd.capacity);
|
||||
if (sd->sd.ramsd.buffer == NULL) {
|
||||
return E_NOMEMORY;
|
||||
}
|
||||
}
|
||||
return E_OK;
|
||||
}
|
||||
@ -36,4 +41,13 @@ int32_t ramsd_cleanup(struct StoreDev *sd) {
|
||||
spinlock_acquire(&sd->spinlock);
|
||||
dlfree(sd->sd.ramsd.buffer);
|
||||
spinlock_release(&sd->spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
size_t ramsd_capacity(struct StoreDev *sd) {
|
||||
size_t capacity;
|
||||
spinlock_acquire(&sd->spinlock);
|
||||
capacity = sd->sd.ramsd.capacity;
|
||||
spinlock_release(&sd->spinlock);
|
||||
return capacity;
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
#ifndef STOREDEV_RAMSD_H_
|
||||
#define STOREDEV_RAMSD_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct StoreDev;
|
||||
|
||||
typedef struct {
|
||||
@ -10,11 +13,13 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
size_t capacity;
|
||||
uint8_t *preallocbuffer;
|
||||
} RamSdInitExtra;
|
||||
|
||||
int32_t ramsd_init(struct StoreDev *sd, void *extra);
|
||||
int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t ramsd_cleanup(struct StoreDev *sd);
|
||||
size_t ramsd_capacity(struct StoreDev *sd);
|
||||
|
||||
#endif // STOREDEV_RAMSD_H_
|
||||
|
@ -33,6 +33,7 @@ StoreDev *storedev_create(int32_t sdtype, void *extra) {
|
||||
sd->cleanup = &ramsd_cleanup;
|
||||
sd->read = &ramsd_read;
|
||||
sd->write = &ramsd_write;
|
||||
sd->capacity = &ramsd_capacity;
|
||||
|
||||
int32_t err = sd->init(sd, extra);
|
||||
if (err != E_OK) {
|
||||
@ -53,5 +54,10 @@ int32_t storedev_delete(StoreDev *sd) {
|
||||
spinlock_acquire(&STOREDEV_LIST.spinlock);
|
||||
LL_REMOVE(STOREDEV_LIST.head, sd);
|
||||
int32_t err = sd->cleanup(sd);
|
||||
if (err < 0) {
|
||||
spinlock_release(&STOREDEV_LIST.spinlock);
|
||||
return err;
|
||||
}
|
||||
spinlock_release(&STOREDEV_LIST.spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ typedef struct StoreDev {
|
||||
int32_t (*read)(struct StoreDev *sd, uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t (*write)(struct StoreDev *sd, const uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t (*cleanup)(struct StoreDev *sd);
|
||||
size_t (*capacity)(struct StoreDev *sd);
|
||||
|
||||
union {
|
||||
RamSd ramsd;
|
||||
|
Reference in New Issue
Block a user