lib_ringfs.h 7.2 KB

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