lib_ringfs.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This program is free software. It comes without any warranty, to the extent
  4. * permitted by applicable law. You can redistribute it and/or modify it under
  5. * the terms of the Do What The Fuck You Want To Public License, Version 2, as
  6. * published by Sam Hocevar. See the COPYING file for more details.
  7. */
  8. #ifndef RINGFS_H
  9. #define RINGFS_H
  10. /**
  11. * @defgroup ringfs_api RingFS API
  12. * @{
  13. */
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stddef.h>
  17. //#include <unistd.h>
  18. typedef size_t ssize_t;
  19. #ifndef NDEBUG
  20. #define RFS_ASSERT(x) ((void)(x))
  21. #else /* debugging enabled */
  22. void ringfs_assert_failed(int8_t* func, int8_t* file, uint32_t line);
  23. #define RFS_ASSERT(e) ((e) ? (void)0 : ringfs_assert_failed(#e, __FILE__, __LINE__))
  24. #endif /* NDEBUG */
  25. /**
  26. * Flash memory+parition descriptor.
  27. */
  28. struct ringfs_flash_partition
  29. {
  30. int sector_size; /**< Sector size, in bytes. */
  31. int sector_offset; /**< Partition offset, in sectors. */
  32. int sector_count; /**< Partition size, in sectors. */
  33. /**
  34. * Erase a sector.
  35. * @param address Any address inside the sector.
  36. * @returns Zero on success, -1 on failure.
  37. * 擦除扇区。
  38. * @param address扇区内的任何地址。
  39. * @return 成功时为零,失败时为-1。
  40. */
  41. int (*sector_erase)(struct ringfs_flash_partition *flash, int address);
  42. /**
  43. * Program flash memory bits by toggling them from 1 to 0.
  44. * @param address Start address, in bytes.
  45. * @param data Data to program.
  46. * @param size Size of data.
  47. * @returns size on success, -1 on failure.
  48. */
  49. ssize_t (*program)(struct ringfs_flash_partition *flash, int address, const void *data, size_t size);
  50. /**
  51. * Read flash memory.
  52. * @param address Start address, in bytes.
  53. * @param data Buffer to store read data.
  54. * @param size Size of data.
  55. * @returns size on success, -1 on failure.
  56. */
  57. ssize_t (*read)(struct ringfs_flash_partition *flash, int address, void *data, size_t size);
  58. };
  59. /** @private */
  60. struct ringfs_loc {
  61. int sector;
  62. int slot;
  63. };
  64. /**
  65. * RingFS instance. Should be initialized with ringfs_init() befure use.
  66. * Structure fields should not be accessed directly.
  67. *RingFS实例。应在使用前使用ringfs_init()初始化。
  68. *不应直接访问结构字段。
  69. * */
  70. struct ringfs {
  71. /* Constant values, set once at ringfs_init(). */
  72. struct ringfs_flash_partition *flash;
  73. uint32_t version;
  74. int object_size;
  75. /* Cached values. */
  76. int slots_per_sector;
  77. /* Read/write pointers. Modified as needed. */
  78. struct ringfs_loc read;
  79. struct ringfs_loc write;
  80. struct ringfs_loc cursor;
  81. };
  82. /**
  83. * Initialize a RingFS instance. Must be called before the instance can be used
  84. * with the other ringfs_* functions.
  85. * 初始化RingFS实例。必须在使用实例之前调用
  86. * 与其他ringfs_*函数一起使用。
  87. * @param fs RingFS instance to be initialized.
  88. * @param flash Flash memory interface. Must be implemented externally.
  89. * @param version Object version. Should be incremented whenever the object's
  90. * semantics or size change in a backwards-incompatible way.
  91. * @param object_size Size of one stored object, in bytes.
  92. * @returns Zero on success, -1 on failure.
  93. */
  94. int ringfs_init(struct ringfs *fs, struct ringfs_flash_partition *flash, uint32_t version, int object_size);
  95. /**
  96. * Format the flash memory.
  97. * 格式化FLASH 存储器。
  98. *
  99. * @param fs Initialized RingFS instance.
  100. * @returns Zero on success, -1 on failure.
  101. */
  102. int ringfs_format(struct ringfs *fs);
  103. /**
  104. * Scan the flash memory for a valid filesystem.
  105. * 扫描FLASH 存储器以获取有效的文件系统
  106. *
  107. * @param fs Initialized RingFS instance.
  108. * @returns Zero on success, -1 on failure.
  109. */
  110. int ringfs_scan(struct ringfs *fs);
  111. /**
  112. * Calculate maximum RingFS capacity.
  113. * 计算RingFS最大容量。
  114. *
  115. * @param fs Initialized RingFS instance.
  116. * @returns Maximum capacity on success, -1 on failure.
  117. */
  118. int ringfs_capacity(struct ringfs *fs);
  119. /**
  120. * Calculate approximate object count.
  121. * Runs in O(1).
  122. * 计算近似对象数。
  123. * 在O(1)中运行。
  124. *
  125. * @param fs Initialized RingFS instance.
  126. * @returns Estimated object count on success, -1 on failure.
  127. */
  128. int ringfs_count_estimate(struct ringfs *fs);
  129. /**
  130. * Calculate exact object count.
  131. * Runs in O(n).
  132. * 计算确切的对象数量。
  133. * 以O(n)运行。
  134. *
  135. * @param fs Initialized RingFS instance.
  136. * @returns Exact object count on success, -1 on failure.
  137. */
  138. int ringfs_count_exact(struct ringfs *fs);
  139. /**
  140. * Append an object at the end of the ring. Deletes oldest objects as needed.
  141. * 在RingFS末端附加一个对象。根据需要删除最旧的对象。
  142. * @param fs Initialized RingFS instance.
  143. * @param object Object to be stored.
  144. * @returns Zero on success, -1 on failure.
  145. */
  146. int ringfs_append(struct ringfs *fs, const void *object);
  147. /**
  148. * Deletes newest objects.
  149. * 根据删除最新可用的对象。
  150. * @param fs Initialized RingFS instance.
  151. * @param object Object to be stored.
  152. * @returns Zero on success, -1 on failure.
  153. */
  154. int ringfs_pop(struct ringfs *fs, void *object);
  155. /**
  156. * Deletes cur valid objects.
  157. * 根据当前向前删除可用的对象。
  158. * @param fs Initialized RingFS instance.
  159. * @param object Object to be stored.
  160. * @returns Zero on success, -1 on failure.
  161. */
  162. int ringfs_del(struct ringfs *fs, void *object);
  163. /**
  164. * Deletes newest objects.
  165. * 根据删除最新可用的对象。
  166. * @param fs Initialized RingFS instance.
  167. * @param object Object to be stored.
  168. * @returns Zero on success, -1 on failure.
  169. */
  170. int ringfs_pop(struct ringfs *fs, void *object);
  171. /**
  172. * Fetch next object from the ring, oldest-first. Advances read cursor.
  173. * 从RingFS中取出下一个对象,先取出最旧的。前进读取光标。
  174. *
  175. * @param fs Initialized RingFS instance.
  176. * @param object Buffer to store retrieved object.
  177. * @returns Zero on success, -1 on failure.
  178. */
  179. int ringfs_fetch(struct ringfs *fs, void *object);
  180. /**
  181. * Discard all fetched objects up to the read cursor.
  182. * 丢弃到读取光标为止的所有提取对象。
  183. *
  184. * @param fs Initialized RingFS instance.
  185. * @returns Zero on success, -1 on failure.
  186. */
  187. int ringfs_discard(struct ringfs *fs);
  188. int ringfs_item_discard(struct ringfs *fs);
  189. /**
  190. * Rewind the read cursor back to the oldest object.
  191. * 将读取光标回退到最旧的对象。
  192. * @param fs Initialized RingFS instance.
  193. * @returns Zero on success, -1 on failure.
  194. */
  195. int ringfs_rewind(struct ringfs *fs);
  196. /**
  197. * Rewind the read cursor advance to the newest object.
  198. * 将读取光标前进到最新的对象。
  199. * @param fs Initialized RingFS instance.
  200. * @returns Zero on success, -1 on failure.
  201. */
  202. int ringfs_end(struct ringfs *fs);
  203. /**
  204. * Dump filesystem metadata. For debugging purposes.
  205. * 转储文件系统元数据。用于调试目的。
  206. * @param stream File stream to write to.
  207. * @param fs Initialized RingFS instance.
  208. */
  209. void ringfs_dump(FILE *stream, struct ringfs *fs);
  210. /**
  211. * 修正读取位置。
  212. * @param stream File stream to write to.
  213. * @param fs Initialized RingFS instance.
  214. */
  215. int ringfs_check(struct ringfs *fs);
  216. /**
  217. * @}
  218. */
  219. #endif
  220. /* vim: set ts=4 sw=4 et: */