cache.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * cache.h
  3. *
  4. * Copyright (C) 2013 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _CACHE_H_
  20. #define _CACHE_H_
  21. #include <common.h>
  22. #include <lock.h>
  23. #include <device.h>
  24. #include <sdk/avltree.h>
  25. #define CACHE_WRITE_THROUGH (1 << 0)
  26. typedef dword_t (*read_write_buffer_proc_t)(void *context, void *buffer, qword_t offset, size_t length, size_t *bytes_read_or_written);
  27. typedef struct
  28. {
  29. avl_node_t node;
  30. qword_t address;
  31. bool_t dirty;
  32. byte_t data[VARIABLE_SIZE];
  33. } cache_entry_t;
  34. #ifndef DEVICE_TYPEDEF
  35. #define DEVICE_TYPEDEF
  36. typedef struct device device_t;
  37. #endif
  38. typedef struct
  39. {
  40. bool_t enabled;
  41. lock_t lock;
  42. dword_t flags;
  43. dword_t block_size;
  44. read_write_buffer_proc_t read_proc, write_proc;
  45. avl_tree_t entries;
  46. } cache_descriptor_t;
  47. void init_cache(cache_descriptor_t *cache,
  48. dword_t flags,
  49. dword_t block_size,
  50. read_write_buffer_proc_t read_proc,
  51. read_write_buffer_proc_t write_proc);
  52. void cleanup_cache(cache_descriptor_t *cache);
  53. dword_t read_cache(cache_descriptor_t *cache, void *context, byte_t *buffer, qword_t offset, dword_t length, dword_t *bytes_read);
  54. dword_t write_cache(cache_descriptor_t *cache, void *context, const byte_t *buffer, qword_t offset, dword_t length, dword_t *bytes_written);
  55. dword_t flush_cache(cache_descriptor_t *cache, void *context);
  56. #endif