sector_cache.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "sector_cache.h"
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <furi.h>
  6. #include <furi_hal_memory.h>
  7. #define SECTOR_SIZE 512
  8. #define N_SECTORS 8
  9. typedef struct {
  10. uint32_t itr;
  11. uint32_t sectors[N_SECTORS];
  12. uint8_t sector_data[N_SECTORS][SECTOR_SIZE];
  13. } SectorCache;
  14. static SectorCache* cache = NULL;
  15. void sector_cache_init() {
  16. if(cache == NULL) {
  17. cache = furi_hal_memory_alloc(sizeof(SectorCache));
  18. }
  19. if(cache != NULL) {
  20. FURI_LOG_I("SectorCache", "Initializing sector cache");
  21. memset(cache, 0, sizeof(SectorCache));
  22. } else {
  23. FURI_LOG_E("SectorCache", "Cannot enable sector cache");
  24. }
  25. }
  26. uint8_t* sector_cache_get(uint32_t n_sector) {
  27. if(cache != NULL && n_sector != 0) {
  28. for(int sector_i = 0; sector_i < N_SECTORS; ++sector_i) {
  29. if(cache->sectors[sector_i] == n_sector) {
  30. return cache->sector_data[sector_i];
  31. }
  32. }
  33. }
  34. return NULL;
  35. }
  36. void sector_cache_put(uint32_t n_sector, uint8_t* data) {
  37. if(cache == NULL) return;
  38. cache->sectors[cache->itr % N_SECTORS] = n_sector;
  39. memcpy(cache->sector_data[cache->itr % N_SECTORS], data, SECTOR_SIZE);
  40. cache->itr++;
  41. }
  42. void sector_cache_invalidate_range(uint32_t start_sector, uint32_t end_sector) {
  43. if(cache == NULL) return;
  44. for(int sector_i = 0; sector_i < N_SECTORS; ++sector_i) {
  45. if((cache->sectors[sector_i] >= start_sector) &&
  46. (cache->sectors[sector_i] <= end_sector)) {
  47. cache->sectors[sector_i] = 0;
  48. }
  49. }
  50. }