changeset 2281:75cb60a7f6f1

Simplify C interface.
author Ziv Scully <ziv@mit.edu>
date Thu, 12 Nov 2015 09:15:50 -0500
parents 985c8016b592
children 19b233bb3176
files include/urweb/types_cpp.h include/urweb/urweb_cpp.h src/c/urweb.c src/lru_cache.sml
diffstat 4 files changed, 16 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb/types_cpp.h	Thu Nov 12 08:46:51 2015 -0500
+++ b/include/urweb/types_cpp.h	Thu Nov 12 09:15:50 2015 -0500
@@ -140,6 +140,7 @@
   struct uw_Sqlcache_Entry *table;
   unsigned long timeInvalid;
   unsigned long timeNow;
+  size_t numKeys;
   UT_hash_handle hh;
 } uw_Sqlcache_Cache;
 
--- a/include/urweb/urweb_cpp.h	Thu Nov 12 08:46:51 2015 -0500
+++ b/include/urweb/urweb_cpp.h	Thu Nov 12 09:15:50 2015 -0500
@@ -406,8 +406,8 @@
 
 // Sqlcache.
 
-uw_Sqlcache_Value *uw_Sqlcache_check(uw_Sqlcache_Cache *, char **, int);
-void *uw_Sqlcache_store(uw_Sqlcache_Cache *, char **, int, uw_Sqlcache_Value *);
-void *uw_Sqlcache_flush(uw_Sqlcache_Cache *, char **, int);
+uw_Sqlcache_Value *uw_Sqlcache_check(uw_Sqlcache_Cache *, char **);
+void *uw_Sqlcache_store(uw_Sqlcache_Cache *, char **, uw_Sqlcache_Value *);
+void *uw_Sqlcache_flush(uw_Sqlcache_Cache *, char **);
 
 #endif
--- a/src/c/urweb.c	Thu Nov 12 08:46:51 2015 -0500
+++ b/src/c/urweb.c	Thu Nov 12 09:15:50 2015 -0500
@@ -488,7 +488,7 @@
   char *output_buffer;
   size_t output_buffer_size;
 
-  // For caching.
+  // Sqlcache.
   int numRecording;
   int recordingOffset;
 
@@ -4616,7 +4616,8 @@
 // The NUL-terminated prefix of [key] below always looks something like "_k1_k2_k3..._kn".
 // TODO: strlen(key) = buf - key?
 
-uw_Sqlcache_Value *uw_Sqlcache_check(uw_Sqlcache_Cache *cache, char **keys, int numKeys) {
+uw_Sqlcache_Value *uw_Sqlcache_check(uw_Sqlcache_Cache *cache, char **keys) {
+  size_t numKeys = cache->numKeys;
   char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
   char *buf = key;
   time_t timeInvalid = cache->timeInvalid;
@@ -4636,7 +4637,8 @@
   return value && value->timeValid > timeInvalid ? value : NULL;
 }
 
-void uw_Sqlcache_store(uw_Sqlcache_Cache *cache, char **keys, int numKeys, uw_Sqlcache_Value *value) {
+void uw_Sqlcache_store(uw_Sqlcache_Cache *cache, char **keys, uw_Sqlcache_Value *value) {
+  size_t numKeys = cache->numKeys;
   char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
   char *buf = key;
   time_t timeNow = uw_Sqlcache_getTimeNow(cache);
@@ -4659,7 +4661,8 @@
   entry->value->timeValid = timeNow;
 }
 
-void uw_Sqlcache_flush(uw_Sqlcache_Cache *cache, char **keys, int numKeys) {
+void uw_Sqlcache_flush(uw_Sqlcache_Cache *cache, char **keys) {
+  size_t numKeys = cache->numKeys;
   char *key = uw_Sqlcache_allocKeyBuffer(keys, numKeys);
   char *buf = key;
   time_t timeNow = uw_Sqlcache_getTimeNow(cache);
--- a/src/lru_cache.sml	Thu Nov 12 08:46:51 2015 -0500
+++ b/src/lru_cache.sml	Thu Nov 12 09:15:50 2015 -0500
@@ -62,14 +62,14 @@
 
         val revArgs = paramRepeatRev (fn p => "p" ^ p) ", "
 
-        val numArgs = Int.toString params
-
     in
         Print.box
             [string ("static uw_Sqlcache_Cache cacheStruct" ^ i ^ " = {"),
              newline,
              string "  .table = NULL,",
              newline,
+             string ("  .numKeys = " ^ Int.toString params ^ ","),
+             newline,
              string "  .timeInvalid = 0,",
              newline,
              string "  .timeNow = 0};",
@@ -83,8 +83,7 @@
              newline,
              string ("  char *ks[] = {" ^ revArgs ^ "};"),
              newline,
-             string "  uw_Sqlcache_Value *v = ",
-             string ("uw_Sqlcache_check(cache" ^ i ^ ", ks, " ^ numArgs ^ ");"),
+             string ("  uw_Sqlcache_Value *v = uw_Sqlcache_check(cache" ^ i ^ ", ks);"),
              newline,
              (* If the output is null, it means we had too much recursion, so it's a miss. *)
              string "  if (v && v->output != NULL) {",
@@ -122,7 +121,7 @@
              newline,
              string ("  puts(\"SQLCACHE: stored " ^ i ^ ".\");"),
              newline,
-             string ("  uw_Sqlcache_store(cache" ^ i ^ ", ks, " ^ numArgs ^ ", v);"),
+             string ("  uw_Sqlcache_store(cache" ^ i ^ ", ks, v);"),
              newline,
              string "  return uw_unit_v;",
              newline,
@@ -135,7 +134,7 @@
              newline,
              string ("  char *ks[] = {" ^ revArgs ^ "};"),
              newline,
-             string ("  uw_Sqlcache_flush(cache" ^ i ^ ", ks, " ^ numArgs ^ ");"),
+             string ("  uw_Sqlcache_flush(cache" ^ i ^ ", ks);"),
              newline,
              string "  return uw_unit_v;",
              newline,