You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ra...@apache.org on 2012/01/08 05:08:51 UTC

[2/2] git commit: avoid unneccesary calls to strlen

avoid unneccesary calls to strlen


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/74d4c91f
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/74d4c91f
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/74d4c91f

Branch: refs/heads/master
Commit: 74d4c91f0dcbfbb9772f7f8b71df9628ad242f53
Parents: 2e60492
Author: Randall Leeds <ra...@apache.org>
Authored: Wed Nov 30 02:20:27 2011 -0800
Committer: Randall Leeds <ra...@apache.org>
Committed: Sat Jan 7 19:42:56 2012 -0800

----------------------------------------------------------------------
 src/couchdb/priv/couch_js/util.c |   18 +++++++++---------
 1 files changed, 9 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/74d4c91f/src/couchdb/priv/couch_js/util.c
----------------------------------------------------------------------
diff --git a/src/couchdb/priv/couch_js/util.c b/src/couchdb/priv/couch_js/util.c
index 3076856..f45ee38 100644
--- a/src/couchdb/priv/couch_js/util.c
+++ b/src/couchdb/priv/couch_js/util.c
@@ -20,11 +20,12 @@
 #include "utf8.h"
 
 
-char*
-slurp_file(char* buf, const char* file)
+size_t
+slurp_file(const char* file, char** outbuf_p)
 {
     FILE* fp;
     char fbuf[16384];
+    char *buf = NULL;
     char* tmp;
     size_t nread = 0;
     size_t buflen = 0;
@@ -41,16 +42,13 @@ slurp_file(char* buf, const char* file)
 
     while((nread = fread(fbuf, 1, 16384, fp)) > 0) {
         if(buf == NULL) {
-            buflen = nread;
             buf = (char*) malloc(nread + 1);
             if(buf == NULL) {
                 fprintf(stderr, "Out of memory.\n");
                 exit(3);
             }
-            memcpy(buf, fbuf, buflen);
-            buf[buflen] = '\0';
+            memcpy(buf, fbuf, nread);
         } else {
-            buflen = strlen(buf);
             tmp = (char*) malloc(buflen + nread + 1);
             if(tmp == NULL) {
                 fprintf(stderr, "Out of memory.\n");
@@ -58,12 +56,14 @@ slurp_file(char* buf, const char* file)
             }
             memcpy(tmp, buf, buflen);
             memcpy(tmp+buflen, fbuf, nread);
-            tmp[buflen+nread] = '\0';
             free(buf);
             buf = tmp;
         }
+        buflen += nread;
+        buf[buflen] = '\0';
     }
-    return buf;
+    *outbuf_p = buf;
+    return buflen + 1;
 }
 
 couch_args*
@@ -104,7 +104,7 @@ couch_parse_args(int argc, const char* argv[])
     }
 
     while(i < argc) {
-        args->script = slurp_file(args->script, argv[i]);
+        slurp_file(argv[i], &args->script);
         if(args->script_name == NULL) {
             if(strcmp(argv[i], "-") == 0) {
                 args->script_name = "<stdin>";