File Caching - Untitled

Next: Linker Functions, Previous: Internal, Up: BFD front end

### 2.17 File caching

The file caching mechanism is embedded within BFD and allows the application to open as many BFDs as it wants without regard to the underlying operating system's file descriptor limit (often as low as 20 open files). The module in `cache.c` maintains a least recently used list of `BFD_CACHE_MAX_OPEN` files, and exports the name `bfd_cache_lookup`, which runs around and makes sure that the required BFD is open. If not, then it chooses a file to close, closes it and opens the one wanted, returning its file handle.

##### 2.17.0.1 `BFD_CACHE_MAX_OPEN macro`

Description
The maximum number of files which the cache will keep open at one time.

         #define BFD_CACHE_MAX_OPEN 10

##### 2.17.0.2 `bfd_last_cache`

Synopsis

         extern bfd *bfd_last_cache;

Description
Zero, or a pointer to the topmost BFD on the chain. This is used by the `bfd_cache_lookup` macro in libbfd.h to determine when it can avoid a function call.

##### 2.17.0.3 `bfd_cache_lookup`

Description
Check to see if the required BFD is the same as the last one looked up. If so, then it can use the stream in the BFD with impunity, since it can't have changed since the last lookup; otherwise, it has to perform the complicated lookup function.

         #define bfd_cache_lookup(x) \
             ((x) == bfd_last_cache ? \
               (FILE *) (bfd_last_cache->iostream): \
                bfd_cache_lookup_worker (x))

##### 2.17.0.4 `bfd_cache_init`

Synopsis

         bfd_boolean bfd_cache_init (bfd *abfd);

Description
Add a newly opened BFD to the cache.

##### 2.17.0.5 `bfd_cache_close`

Synopsis

         bfd_boolean bfd_cache_close (bfd *abfd);

Description
Remove the BFD abfd from the cache. If the attached file is open, then close it too.

Returns
`FALSE` is returned if closing the file fails, `TRUE` is returned if all is well.

##### 2.17.0.6 `bfd_cache_close_all`

Synopsis

         bfd_boolean bfd_cache_close_all (void);

Description
Remove all BFDs from the cache. If the attached file is open, then close it too.

Returns
`FALSE` is returned if closing one of the file fails, `TRUE` is returned if all is well.

##### 2.17.0.7 `bfd_open_file`

Synopsis

         FILE* bfd_open_file (bfd *abfd);

Description
Call the OS to open a file for abfd. Return the `FILE *` (possibly `NULL`) that results from this operation. Set up the BFD so that future accesses know the file is open. If the `FILE *` returned is `NULL`, then it won't have been put in the cache, so it won't have to be removed from it.

##### 2.17.0.8 `bfd_cache_lookup_worker`

Synopsis

         FILE *bfd_cache_lookup_worker (bfd *abfd);

Description
Called when the macro `bfd_cache_lookup` fails to find a quick answer. Find a file descriptor for abfd. If necessary, it open it. If there are already more than `BFD_CACHE_MAX_OPEN` files open, it tries to close one first, to avoid running out of file descriptors. It will abort rather than returning NULL if it is unable to (re)open the abfd.