1 | /* |
---|
2 | * COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | /** |
---|
9 | * @file |
---|
10 | * |
---|
11 | * @ingroup rtems_rtl |
---|
12 | * |
---|
13 | * @brief RTEMS Run-Time Linker Object File cache buffers a section of the |
---|
14 | * object file in a buffer to localise read performance. |
---|
15 | * |
---|
16 | * This is a simple object file cache that holds a buffer of data from the |
---|
17 | * offset in the file the read is requested from. Writes are not supported. |
---|
18 | * |
---|
19 | * The cache holds the file descriptor, the offset into the file and the amount |
---|
20 | * of valid data in the cache. If the file is ever modified the user of the |
---|
21 | * cache to responsible for flushing the cache. For example the cache should be |
---|
22 | * flused if the file is closed. |
---|
23 | * |
---|
24 | * The cache can return by reference or by value. By reference allow access to |
---|
25 | * the cache buffer. Do not modify the cache's data. By value will copy the |
---|
26 | * requested data into the user supplied buffer. |
---|
27 | * |
---|
28 | * The read by reference call allows you to probe the file's data. For example |
---|
29 | * a string in an object file can be an unknown length. You can request a read |
---|
30 | * up to the cache's size by reference. The code will attempt to have this data |
---|
31 | * in the buffer. If there is not enough data in the file the length will be |
---|
32 | * modifed to reflect this. |
---|
33 | * |
---|
34 | * You can have more than one cache for a single file all looking at different |
---|
35 | * parts of the file. |
---|
36 | */ |
---|
37 | |
---|
38 | #if !defined (_RTEMS_RTL_OBJ_CACHE_H_) |
---|
39 | #define _RTEMS_RTL_OBJ_CACHE_H_ |
---|
40 | |
---|
41 | #include <fcntl.h> |
---|
42 | #include <stdbool.h> |
---|
43 | #include <stdint.h> |
---|
44 | #include <stdlib.h> |
---|
45 | |
---|
46 | #ifdef __cplusplus |
---|
47 | extern "C" { |
---|
48 | #endif /* __cplusplus */ |
---|
49 | |
---|
50 | /** |
---|
51 | * The buffer cache. |
---|
52 | */ |
---|
53 | typedef struct rtems_rtl_obj_cache_s |
---|
54 | { |
---|
55 | int fd; /**< The file descriptor of the data in the cache. */ |
---|
56 | size_t file_size; /**< The size of the file. */ |
---|
57 | off_t offset; /**< The base offset of the buffer. */ |
---|
58 | size_t size; /**< The size of the cache. */ |
---|
59 | size_t level; /**< The amount of data in the cache. A file can be |
---|
60 | * smaller than the cache file. */ |
---|
61 | uint8_t* buffer; /**< The buffer */ |
---|
62 | } rtems_rtl_obj_cache_t; |
---|
63 | |
---|
64 | /** |
---|
65 | * Open a cache allocating a single buffer of the size passed. The default |
---|
66 | * state of the cache is flushed. No already open checks are made. |
---|
67 | * |
---|
68 | * @param cache The cache to initialise. |
---|
69 | * @param size The size of the cache. |
---|
70 | * @retval true The cache is open. |
---|
71 | * @retval false The cache is not open. The RTL error is set. |
---|
72 | */ |
---|
73 | bool rtems_rtl_obj_cache_open (rtems_rtl_obj_cache_t* cache, size_t size); |
---|
74 | |
---|
75 | /** |
---|
76 | * Close a cache. |
---|
77 | * |
---|
78 | * @param cache The cache to close. |
---|
79 | */ |
---|
80 | void rtems_rtl_obj_cache_close (rtems_rtl_obj_cache_t* cache); |
---|
81 | |
---|
82 | /** |
---|
83 | * Flush the cache. Any further read will read the data from the file. |
---|
84 | * |
---|
85 | * @param cache The cache to flush. |
---|
86 | */ |
---|
87 | void rtems_rtl_obj_cache_flush (rtems_rtl_obj_cache_t* cache); |
---|
88 | |
---|
89 | /** |
---|
90 | * Read data by reference. The length contains the amount of data that should |
---|
91 | * be available in the cache and referenced by the buffer handle. It must be |
---|
92 | * less than or equal to the size of the cache. This call will return the |
---|
93 | * amount of data that is available. It can be less than you ask if the offset |
---|
94 | * and size is past the end of the file. |
---|
95 | * |
---|
96 | * @param cache The cache to reference data from. |
---|
97 | * @param fd The file descriptor. Must be an open file. |
---|
98 | * @param offset The offset in the file to reference the data to. |
---|
99 | * @param buffer The location to reference the data from. |
---|
100 | * @param length The length of data to reference. Can be modified to a |
---|
101 | * lesser value and true is still returned so check it. |
---|
102 | * @retval true The data referenced is in the cache. |
---|
103 | * @retval false The read failed and the RTL error has been set. |
---|
104 | */ |
---|
105 | bool rtems_rtl_obj_cache_read (rtems_rtl_obj_cache_t* cache, |
---|
106 | int fd, |
---|
107 | off_t offset, |
---|
108 | void** buffer, |
---|
109 | size_t* length); |
---|
110 | |
---|
111 | /** |
---|
112 | * Read data by value. The data is copied to the user supplied buffer. |
---|
113 | * |
---|
114 | * @param cache The cache to read the data from. |
---|
115 | * @param fd The file descriptor. Must be an open file. |
---|
116 | * @param offset The offset in the file to read the data from. |
---|
117 | * @param buffer The location the data is written into. |
---|
118 | * @param length The length of data to read. |
---|
119 | * @retval true The data has been read from the cache. |
---|
120 | * @retval false The read failed and the RTL error has been set. |
---|
121 | */ |
---|
122 | bool rtems_rtl_obj_cache_read_byval (rtems_rtl_obj_cache_t* cache, |
---|
123 | int fd, |
---|
124 | off_t offset, |
---|
125 | void* buffer, |
---|
126 | size_t length); |
---|
127 | |
---|
128 | #ifdef __cplusplus |
---|
129 | } |
---|
130 | #endif /* __cplusplus */ |
---|
131 | |
---|
132 | #endif |
---|