#include #include #include "spinlock/spinlock.h" #include "errors.h" #include "dlmalloc/malloc.h" #include "ramsd.h" #include "storedev.h" #include "hal/hal.h" #include "util/util.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; } return E_OK; } int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, size_t n, size_t off) { spinlock_acquire(&sd->spinlock); hal_memcpy(buffer, sd->sd.ramsd.buffer + off, MIN(n, sd->sd.ramsd.capacity - off)); spinlock_release(&sd->spinlock); return E_OK; } int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, size_t n, size_t off) { spinlock_acquire(&sd->spinlock); hal_memcpy(sd->sd.ramsd.buffer + off, buffer, MIN(n, sd->sd.ramsd.capacity - off)); spinlock_release(&sd->spinlock); return E_OK; } int32_t ramsd_cleanup(struct StoreDev *sd) { spinlock_acquire(&sd->spinlock); dlfree(sd->sd.ramsd.buffer); spinlock_release(&sd->spinlock); }