Prechádzať zdrojové kódy

filecache: remove par_byte in favor of uint8_t, add const correctness.

Philip Rideout 10 rokov pred
rodič
commit
9baaf3fbf4
2 zmenil súbory, kde vykonal 24 pridanie a 26 odobranie
  1. 9 11
      par_filecache.h
  2. 15 15
      test/test_filecache.c

+ 9 - 11
par_filecache.h

@@ -22,8 +22,6 @@
 
 #include <stdbool.h>
 
-typedef uint8_t par_byte;
-
 // Initialize the filecache using the given prefix (usually a folder path with
 // a trailing slash) and the given maximum byte count.  If items already exist
 // in the cache when this is called, they are not evicted.  Cached items are
@@ -33,14 +31,14 @@ void par_filecache_init(char const* prefix, int maxsize);
 // Save a blob to the cache using the given unique name.  If adding the blob
 // would cause the cache to exceed maxsize, the least-recently-used item is
 // evicted at this time.
-void par_filecache_save(char const* name, par_byte* payload, int payloadsize,
-    par_byte* header, int headersize);
+void par_filecache_save(char const* name, uint8_t const* payload,
+    int payloadsize, uint8_t const* header, int headersize);
 
 // Check if the given blob is in the cache; if not, return 0.  If so, return 1
 // and allocate new memory for payload.  The caller should free the payload.
 // The header is preallocated so the caller needs to know its size beforehand.
-bool par_filecache_load(char const* name, par_byte** payload, int* payloadsize,
-    par_byte* header, int headersize);
+bool par_filecache_load(char const* name, uint8_t** payload, int* payloadsize,
+    uint8_t* header, int headersize);
 
 // Remove all items from the cache.
 void par_filecache_evict_all();
@@ -169,8 +167,8 @@ static bool par_filecache__read(void* dest, int nbytes, FILE* file)
     return consumed == 1;
 }
 
-bool par_filecache_load(char const* name, par_byte** payload, int* payloadsize,
-    par_byte* header, int headersize)
+bool par_filecache_load(char const* name, uint8_t** payload, int* payloadsize,
+    uint8_t* header, int headersize)
 {
     char qualified[PATH_MAX];
     size_t len = strlen(name);
@@ -213,14 +211,14 @@ bool par_filecache_load(char const* name, par_byte** payload, int* payloadsize,
     char* dbuff = cbuff;
 #endif
     fclose(cachefile);
-    *payload = (par_byte*) dbuff;
+    *payload = (uint8_t*) dbuff;
     *payloadsize = dnbytes;
     _update_table(name, (int) cnbytes);
     return true;
 }
 
-void par_filecache_save(char const* name, par_byte* payload, int payloadsize,
-    par_byte* header, int headersize)
+void par_filecache_save(char const* name, uint8_t const* payload,
+    int payloadsize, uint8_t const* header, int headersize)
 {
     char qualified[PATH_MAX];
     size_t len = strlen(name);

+ 15 - 15
test/test_filecache.c

@@ -22,8 +22,8 @@ int main()
         }
         it("should save and load small files") {
             char const* payload = "01234";
-            par_filecache_save("small", (par_byte*) payload, 5, 0, 0);
-            par_byte* received = 0;
+            par_filecache_save("small", (uint8_t*) payload, 5, 0, 0);
+            uint8_t* received = 0;
             int nbytes = 0;
             int loaded = par_filecache_load("small", &received, &nbytes, 0, 0);
             assert_ok(loaded);
@@ -38,8 +38,8 @@ int main()
             for (int i = 0; i < 512; i++) {
                 payload[i] = rand() % 256;
             }
-            par_filecache_save("big", (par_byte*) payload, 1024, 0, 0);
-            par_byte* received = 0;
+            par_filecache_save("big", (uint8_t*) payload, 1024, 0, 0);
+            uint8_t* received = 0;
             int nbytes = 0;
             int loaded = par_filecache_load("big", &received, &nbytes, 0, 0);
             assert_ok(loaded);
@@ -50,10 +50,10 @@ int main()
             free(payload);
         }
         it("should evict the oldest file when exceeding max size") {
-            par_byte* received = 0;
+            uint8_t* received = 0;
             int nbytes = 0;
             char* payload = calloc(1024, 1);
-            par_filecache_save("second", (par_byte*) payload, 1024, 0, 0);
+            par_filecache_save("second", (uint8_t*) payload, 1024, 0, 0);
             free(payload);
             int loaded = par_filecache_load("big", &received, &nbytes, 0, 0);
             #if ENABLE_LZ4
@@ -64,7 +64,7 @@ int main()
             #endif
         }
         it("returns false when not found") {
-            par_byte* received = 0;
+            uint8_t* received = 0;
             int nbytes = 0;
             int loaded = par_filecache_load("bar", &received, &nbytes, 0, 0);
             assert_equal(loaded, 0);
@@ -72,14 +72,14 @@ int main()
         it("supports a fixed-size header (e.g., version number)") {
             char* payload = "philip";
             char* header = "v0001";
-            par_filecache_save("versioned", (par_byte*) payload,
-                strlen(payload), (par_byte*) header, 5);
+            par_filecache_save("versioned", (uint8_t*) payload,
+                strlen(payload), (uint8_t*) header, 5);
             char* loaded_payload;
             char loaded_header[5] = {0};
             int nbytes = 0;
             int loaded = par_filecache_load("versioned",
-                (par_byte**) &loaded_payload, &nbytes,
-                (par_byte*) loaded_header, 5);
+                (uint8_t**) &loaded_payload, &nbytes,
+                (uint8_t*) loaded_header, 5);
             assert_equal(loaded, 1);
             assert_equal(nbytes, 6);
             assert_ok(!memcmp(loaded_header, header, 5));
@@ -96,8 +96,8 @@ int main()
             char loaded_header[5] = {0};
             int nbytes = 0;
             int loaded = par_filecache_load("versioned",
-                (par_byte**) &loaded_payload, &nbytes,
-                (par_byte*) loaded_header, 5);
+                (uint8_t**) &loaded_payload, &nbytes,
+                (uint8_t*) loaded_header, 5);
             assert_equal(loaded, 0);
         }
         it("is graceful when file content vanishes") {
@@ -109,8 +109,8 @@ int main()
             char loaded_header[5] = {0};
             int nbytes = 0;
             int loaded = par_filecache_load("second",
-                (par_byte**) &loaded_payload, &nbytes,
-                (par_byte*) loaded_header, 5);
+                (uint8_t**) &loaded_payload, &nbytes,
+                (uint8_t*) loaded_header, 5);
             assert_equal(loaded, 0);
         }
     }