Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:12

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 2012, 2018 Chris Johns <chrisj@rtems.org>
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 /**
0028  * @file
0029  *
0030  * @ingroup rtems_rtl
0031  *
0032  * @brief RTEMS Run-Time Linker Object File cache buffers a section of the
0033  *        object file in a buffer to localise read performance.
0034  *
0035  * This is a simple object file cache that holds a buffer of data from the
0036  * offset in the file the read is requested from. Writes are not supported.
0037  *
0038  * The cache holds the file descriptor, the offset into the file and the amount
0039  * of valid data in the cache. If the file is ever modified the user of the
0040  * cache to responsible for flushing the cache. For example the cache should be
0041  * flused if the file is closed.
0042  *
0043  * The cache can return by reference or by value. By reference allow access to
0044  * the cache buffer. Do not modify the cache's data. By value will copy the
0045  * requested data into the user supplied buffer.
0046  *
0047  * The read by reference call allows you to probe the file's data. For example
0048  * a string in an object file can be an unknown length. You can request a read
0049  * up to the cache's size by reference. The code will attempt to have this data
0050  * in the buffer. If there is not enough data in the file the length will be
0051  * modifed to reflect this.
0052  *
0053  * You can have more than one cache for a single file all looking at different
0054  * parts of the file.
0055  */
0056 
0057 #if !defined (_RTEMS_RTL_OBJ_CACHE_H_)
0058 #define _RTEMS_RTL_OBJ_CACHE_H_
0059 
0060 #include <fcntl.h>
0061 #include <stdbool.h>
0062 #include <stdint.h>
0063 #include <stdlib.h>
0064 
0065 #ifdef __cplusplus
0066 extern "C" {
0067 #endif /* __cplusplus */
0068 
0069 /**
0070  * The buffer cache.
0071  */
0072 typedef struct rtems_rtl_obj_cache
0073 {
0074   int      fd;        /**< The file descriptor of the data in the cache. */
0075   size_t   file_size; /**< The size of the file. */
0076   off_t    offset;    /**< The base offset of the buffer. */
0077   size_t   size;      /**< The size of the cache. */
0078   size_t   level;     /**< The amount of data in the cache. A file can be
0079                        * smaller than the cache file. */
0080   uint8_t* buffer;    /**< The buffer */
0081 } rtems_rtl_obj_cache;
0082 
0083 /**
0084  * Open a cache allocating a single buffer of the size passed. The default
0085  * state of the cache is flushed. No already open checks are made.
0086  *
0087  * @param cache The cache to initialise.
0088  * @param size The size of the cache.
0089  * @retval true The cache is open.
0090  * @retval false The cache is not open. The RTL error is set.
0091  */
0092 bool rtems_rtl_obj_cache_open (rtems_rtl_obj_cache* cache, size_t size);
0093 
0094 /**
0095  * Close a cache.
0096  *
0097  * @param cache The cache to close.
0098  */
0099 void rtems_rtl_obj_cache_close (rtems_rtl_obj_cache* cache);
0100 
0101 /**
0102  * Flush the cache. Any further read will read the data from the file.
0103  *
0104  * @param cache The cache to flush.
0105  */
0106 void rtems_rtl_obj_cache_flush (rtems_rtl_obj_cache* cache);
0107 
0108 /**
0109  * Read data by reference. The length contains the amount of data that should
0110  * be available in the cache and referenced by the buffer handle. It must be
0111  * less than or equal to the size of the cache. This call will return the
0112  * amount of data that is available. It can be less than you ask if the offset
0113  * and size is past the end of the file.
0114  *
0115  * @param cache The cache to reference data from.
0116  * @param fd The file descriptor. Must be an open file.
0117  * @param offset The offset in the file to reference the data to.
0118  * @param buffer The location to reference the data from.
0119  * @param length The length of data to reference. Can be modified to a
0120  *               lesser value and true is still returned so check it.
0121  * @retval true The data referenced is in the cache.
0122  * @retval false The read failed and the RTL error has been set.
0123  */
0124 bool rtems_rtl_obj_cache_read (rtems_rtl_obj_cache* cache,
0125                                int                  fd,
0126                                off_t                offset,
0127                                void**               buffer,
0128                                size_t*              length);
0129 
0130 /**
0131  * Read data by value. The data is copied to the user supplied buffer.
0132  *
0133  * @param cache The cache to read the data from.
0134  * @param fd The file descriptor. Must be an open file.
0135  * @param offset The offset in the file to read the data from.
0136  * @param buffer The location the data is written into.
0137  * @param length The length of data to read.
0138  * @retval true The data has been read from the cache.
0139  * @retval false The read failed and the RTL error has been set.
0140  */
0141 bool rtems_rtl_obj_cache_read_byval (rtems_rtl_obj_cache* cache,
0142                                      int                  fd,
0143                                      off_t                offset,
0144                                      void*                buffer,
0145                                      size_t               length);
0146 
0147 #ifdef __cplusplus
0148 }
0149 #endif /* __cplusplus */
0150 
0151 #endif