summaryrefslogtreecommitdiffstats
path: root/contrib/syslinux-4.02/libfat/cache.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/syslinux-4.02/libfat/cache.c')
-rw-r--r--contrib/syslinux-4.02/libfat/cache.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/contrib/syslinux-4.02/libfat/cache.c b/contrib/syslinux-4.02/libfat/cache.c
new file mode 100644
index 0000000..85b7ead
--- /dev/null
+++ b/contrib/syslinux-4.02/libfat/cache.c
@@ -0,0 +1,65 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
+ * Boston MA 02111-1307, USA; either version 2 of the License, or
+ * (at your option) any later version; incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * cache.c
+ *
+ * Simple sector cache
+ */
+
+#include <stdlib.h>
+#include "libfatint.h"
+
+void *libfat_get_sector(struct libfat_filesystem *fs, libfat_sector_t n)
+{
+ struct libfat_sector *ls;
+
+ for (ls = fs->sectors; ls; ls = ls->next) {
+ if (ls->n == n)
+ return ls->data; /* Found in cache */
+ }
+
+ /* Not found in cache */
+ ls = malloc(sizeof(struct libfat_sector));
+ if (!ls) {
+ libfat_flush(fs);
+ ls = malloc(sizeof(struct libfat_sector));
+
+ if (!ls)
+ return NULL; /* Can't allocate memory */
+ }
+
+ if (fs->read(fs->readptr, ls->data, LIBFAT_SECTOR_SIZE, n)
+ != LIBFAT_SECTOR_SIZE) {
+ free(ls);
+ return NULL; /* I/O error */
+ }
+
+ ls->n = n;
+ ls->next = fs->sectors;
+ fs->sectors = ls;
+
+ return ls->data;
+}
+
+void libfat_flush(struct libfat_filesystem *fs)
+{
+ struct libfat_sector *ls, *lsnext;
+
+ lsnext = fs->sectors;
+ fs->sectors = NULL;
+
+ for (ls = lsnext; ls; ls = lsnext) {
+ lsnext = ls->next;
+ free(ls);
+ }
+}