sector_cache.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = memmgr_alloc_from_pool(sizeof(SectorCache));
  18. }
  19. if(cache != NULL) {
  20. memset(cache, 0, sizeof(SectorCache));
  21. }
  22. }
  23. uint8_t* sector_cache_get(uint32_t n_sector) {
  24. if(cache != NULL && n_sector != 0) {
  25. for(int sector_i = 0; sector_i < N_SECTORS; ++sector_i) {
  26. if(cache->sectors[sector_i] == n_sector) {
  27. return cache->sector_data[sector_i];
  28. }
  29. }
  30. }
  31. return NULL;
  32. }
  33. void sector_cache_put(uint32_t n_sector, uint8_t* data) {
  34. if(cache == NULL) return;
  35. cache->sectors[cache->itr % N_SECTORS] = n_sector;
  36. memcpy(cache->sector_data[cache->itr % N_SECTORS], data, SECTOR_SIZE);
  37. cache->itr++;
  38. }
  39. void sector_cache_invalidate_range(uint32_t start_sector, uint32_t end_sector) {
  40. if(cache == NULL) return;
  41. for(int sector_i = 0; sector_i < N_SECTORS; ++sector_i) {
  42. if((cache->sectors[sector_i] >= start_sector) &&
  43. (cache->sectors[sector_i] <= end_sector)) {
  44. cache->sectors[sector_i] = 0;
  45. }
  46. }
  47. }