Sümer Cip avatar Sümer Cip committed 5931078

TP+ . Few Valgrind uninitialized var warnings.

Comments (0)

Files changed (5)

     conn->listening = 0;
     conn->free = 0;
     conn->in = NULL;
-    conn->out = NULL;
 
     stats.curr_connections++;
 
 
 static void free_request(request *req)
 {
+    if (!req || !req->can_free) {
+        return;
+    }    
 
-    if (!req) {
-        return;
-    }
-
-    if (req->can_free) {
-        LC_DEBUG(("FREEING request data.[%p], sizeof:[%u]\r\n", (void *)req, (unsigned int)sizeof(request *)));
-        li_free(req->rkey);
-        li_free(req->rdata);
-        li_free(req->rextra);
-        req->rkey = NULL;
-        req->rdata = NULL;
-        req->rextra = NULL;
-        li_free(req);
-    }
+    LC_DEBUG(("FREEING request data.[%p], sizeof:[%u]\r\n", (void *)req, (unsigned int)sizeof(request *)));
+    li_free(req->rkey);
+    li_free(req->rdata);
+    li_free(req->rextra);
+    //req->rkey = NULL;
+    //req->rdata = NULL;
+    //req->rextra = NULL;
+    li_free(req);
 }
 
 static void free_response(response *resp)
 {
-
-    if (!resp) {
-        return;
-    }
-
-    if (resp->can_free && resp->sdata) {
-        //LC_DEBUG(("FREEING response data.[%p]\r\n", (void *)resp));
+    if (resp->can_free) {
+        LC_DEBUG(("FREEING response data.[%p]\r\n", (void *)resp));
         li_free(resp->sdata);
     }
-
-    /* can lose reference to mem, otherwise, later freelist usage of the same resp
-     * object may lead to invalid deletion of this data.
-     * */
     resp->sdata = NULL;
-
-    li_free(resp);
 }
 
 
 static int init_resources(conn *conn)
 {
-
-    /* free previous request allocations if we have any */
     free_request(conn->in);
-    free_response(conn->out);
-    conn->in = NULL;
-    conn->out = NULL;    
-
-    /* allocate req/resp resources */
+    free_response(&conn->out);
+    
     conn->in = (request *)li_malloc(sizeof(request));
     if (!conn->in) {
         return 0;
     }
-    conn->out = (response *)li_malloc(sizeof(response));
-    if (!conn->out) {
-        return 0;
-    }
 
-    /* init defaults */
-    conn->in->can_free = 1;
-    conn->out->can_free = 1;
-    conn->in->rbytes = 0;
-    conn->out->sbytes = 0;
+    conn->in->can_free = 1;    
+    conn->in->rbytes = 0;    
     conn->in->rkey = NULL;
     conn->in->rdata = NULL;
     conn->in->rextra = NULL;
-    conn->out->sdata = NULL;
+
+    conn->out.sdata = NULL;
+    conn->out.sbytes = 0;
+    conn->out.can_free = 1;
    
     return 1;
 }
     event_del(conn);
 
     free_request(conn->in);
-    free_response(conn->out);
+    conn->in = NULL;
+    free_response(&conn->out);
 
     conn->free = 1;
     close(conn->fd);
     set_conn_state(conn, CONN_CLOSED);
 }
 
+static void add_response(conn *conn, void *data, size_t data_length, code_t code)
+{
+    conn->out.resp_header.response.data_length = htonl(data_length);
+    conn->out.resp_header.response.opcode = conn->in->req_header.request.opcode;
+    conn->out.resp_header.response.retcode = code;
+    
+    conn->out.sdata = data;
+    conn->out.can_free = 1;
+
+    set_conn_state(conn, SEND_HEADER);    
+} 
+
 static void send_response(conn *conn, code_t code)
 {
-    assert(conn->out != NULL);
-
-    conn->out->resp_header.response.data_length = 0;
-    conn->out->resp_header.response.opcode = conn->in->req_header.request.opcode;
-    conn->out->resp_header.response.retcode = err;
-
-    conn->out->sdata = NULL;
-    conn->out->can_free = 1;
-
-    set_conn_state(conn, SEND_HEADER);
+    add_response(conn, NULL, 0, code);
 }
 
-static int prepare_response(conn *conn, size_t data_length, int alloc_mem)
-{
-    assert(conn->out != NULL);
-
-    if (alloc_mem) {
-        conn->out->sdata = (char *)li_malloc(data_length);
-        if (!conn->out->sdata) {
-            return 0;
-        }
-    }
-    conn->out->resp_header.response.data_length = htonl(data_length);
-    conn->out->resp_header.response.opcode = conn->in->req_header.request.opcode;
-    conn->out->resp_header.response.errcode = SUCCESS;
-
-    return 1;
-}
 
 void set_conn_state(struct conn* conn, conn_states state)
 {
     case CMD_SENT:
         break;
     case SEND_HEADER:
-        conn->out->sbytes = 0;
+        conn->out.sbytes = 0;
         event_set(conn, EVENT_WRITE);
         break;
     default:
 static void execute_cmd(struct conn* conn)
 {
     int r;
-    int quiet;
     hresult ret;
     uint8_t cmd;
     uint64_t val;
     request *cached_req;
     _hitem *tab_item;
+    char *sval;
+    uint64_t *ival;
 
     assert(conn->state == CMD_RECEIVED);
 
     /* here, the complete request is received from the connection */
     conn->in->received = CURRENT_TIME;
     cmd = conn->in->req_header.request.opcode;
-    quiet = 0;
-
+    
     /* No need for the validation of conn->in->rkey as it is mandatory for the
        protocol. */
     switch(cmd) {
-    case CMD_GETQ:
-    	quiet = 1;
     case CMD_GET:
 
-        LC_DEBUG(("CMD_GET (Q:%d) [%s]\r\n", quiet, conn->in->rkey));
+        LC_DEBUG(("CMD_GET [%s]\r\n", conn->in->rkey));
 
         stats.cmd_get++;
 
 
         stats.get_hits++;
 
-        prepare_response(conn, cached_req->req_header.request.data_length, 0); // do not alloc mem
-
-        conn->out->sdata = cached_req->rdata;
-        conn->out->can_free = 0;
-
-        set_conn_state(conn, SEND_HEADER);
+        add_response(conn, cached_req->rdata, cached_req->req_header.request.data_length, SUCCESS);
+        conn->out.can_free = 0;
         break;
-    case CMD_SETQ:
-        quiet = 1;
     case CMD_SET:
 
-        LC_DEBUG(("CMD_SET (Q:%d) \r\n", quiet));
+        LC_DEBUG(("CMD_SET \r\n"));
 
         stats.cmd_set++;
 
 
         /* validate params */
         if (strcmp(conn->in->rkey, "idle_conn_timeout") == 0) {
-            if (!prepare_response(conn, sizeof(uint64_t), 1)) {
+            ival = li_malloc(sizeof(uint64_t));
+            if (!ival) {
                 send_response(conn, OUT_OF_MEMORY);
                 return;
             }
-            *(uint64_t *)conn->out->sdata = htonll(settings.idle_conn_timeout);
-            set_conn_state(conn, SEND_HEADER);
-        } else if (strcmp(conn->in->rkey, "mem_avail") == 0) {
-            if (!prepare_response(conn, sizeof(uint64_t), 1)) {
-                send_response(conn, OUT_OF_MEMORY);
-                return;
-            }
-            *(uint64_t *)conn->out->sdata = htonll(settings.mem_avail / 1024 / 1024);
-            set_conn_state(conn, SEND_HEADER);
+            *ival = htonll(settings.idle_conn_timeout);
+            add_response(conn, ival, sizeof(uint64_t), SUCCESS);
         } else {
             LC_DEBUG(("Invalid setting received :%s\r\n", conn->in->rkey));
             send_response(conn, INVALID_PARAM);
     case CMD_GET_STATS:
 
         LC_DEBUG(("GET_STATS\r\n"));
-
-        if (!prepare_response(conn, LIGHTCACHE_STATS_SIZE, 1)) {
+        
+        sval = li_malloc(LIGHTCACHE_STATS_SIZE);
+        if (!sval) {
             send_response(conn, OUT_OF_MEMORY);
             return;
         }
-        sprintf(conn->out->sdata,
+        sprintf(sval,
                 "mem_used:%llu\r\nmem_avail:%llu\r\nuptime:%lu\r\nversion: %0.1f Build.%d\r\n"
                 "pid:%d\r\ntime:%lu\r\ncurr_items:%d\r\ncurr_connections:%llu\r\n"
                 "cmd_get:%llu\r\ncmd_set:%llu\r\nget_misses:%llu\r\nget_hits:%llu\r\n"
                 (long long unsigned int)stats.get_misses,
                 (long long unsigned int)stats.get_hits,
                 (long long unsigned int)stats.bytes_read,
-                (long long unsigned int)stats.bytes_written);
-        set_conn_state(conn, SEND_HEADER);
+                (long long unsigned int)stats.bytes_written);        
+        add_response(conn, sval, LIGHTCACHE_STATS_SIZE, SUCCESS);
         break;
     default:
         LC_DEBUG(("Unrecognized command.[%d]\r\n", cmd));
 {
     int needed, nbytes;
 
-    //LC_DEBUG(("send_nbytes called.[left:%ld, fd:%d]\r\n", total - conn->out->sbytes, conn->fd));
+    //LC_DEBUG(("send_nbytes called.[left:%ld, fd:%d]\r\n", total - conn->out.sbytes, conn->fd));
 
-    needed = total - conn->out->sbytes;
-    nbytes = write(conn->fd, &bytes[conn->out->sbytes], 1); //needed
+    needed = total - conn->out.sbytes;
+    nbytes = write(conn->fd, &bytes[conn->out.sbytes], 1); //needed
     if (nbytes == -1) {
         if (errno == EWOULDBLOCK || errno == EAGAIN) {
             return NEED_MORE;
         }
-        LC_DEBUG(("%s (%s)", "socket write error.", strerror(errno)));
-        syslog(LOG_ERR, "%s (%s)", "socket write error.", strerror(errno));
+        LC_DEBUG(("%s (%s)", "socket write error.\r\n", strerror(errno)));
+        syslog(LOG_ERR, "%s (%s)", "socket write error.\r\n", strerror(errno));
         return SEND_ERR;
     }
 
     stats.bytes_written += nbytes;
 
-    conn->out->sbytes += nbytes;
-    if (conn->out->sbytes == total) {
-        conn->out->sbytes = 0;
+    conn->out.sbytes += nbytes;
+    if (conn->out.sbytes == total) {
+        conn->out.sbytes = 0;
         return SEND_COMPLETED;
     }
 
     switch(conn->state) {
 
     case SEND_HEADER:
-        ret = send_nbytes(conn, (char *)conn->out->resp_header.bytes, sizeof(resp_header));
+        ret = send_nbytes(conn, (char *)conn->out.resp_header.bytes, sizeof(resp_header));
         if (ret == SEND_COMPLETED) {
-            if (ntohl(conn->out->resp_header.response.data_length) != 0) {
+            if (ntohl(conn->out.resp_header.response.data_length) != 0) {
                 set_conn_state(conn, SEND_DATA);
             } else {
                 set_conn_state(conn, CMD_SENT);
         }
         break;
     case SEND_DATA:
-        ret = send_nbytes(conn, conn->out->sdata, ntohl(conn->out->resp_header.response.data_length));
+        ret = send_nbytes(conn, conn->out.sdata, ntohl(conn->out.resp_header.response.data_length));
         if (ret == SEND_COMPLETED) {
             set_conn_state(conn, CMD_SENT);
             set_conn_state(conn, READ_HEADER);// wait for new commands
 #define LIGHTCACHE_PORT 13131
 #define LIGHTCACHE_LISTEN_BACKLOG 100
 #define LIGHTCACHE_GARBAGE_COLLECT_RATIO_THRESHOLD 75 /*the ratio threshold that garbage collect functions will start demanding memory.*/
-#define LIGHTCACHE_STATS_SIZE 250
+#define LIGHTCACHE_STATS_SIZE 512
 #define SLAB_SIZE_FACTOR 1.25
 
 #endif
     }
     
     if (!settings.use_sys_malloc) {
-        p = scmalloc(size);
+        p = malloc(size);
     } else {
         if (size + mem_used > (settings.mem_avail)) {
             syslog(LOG_ERR, "No memory available![%llu MB]", (long long unsigned int)settings.mem_avail);
 //#endif
     
     if (!settings.use_sys_malloc) {
-        scfree(ptr);
+        free(ptr);
     } else {
         ptr = (char *)ptr - sizeof(size_t);
         size = *(size_t *)ptr;
         mem_used -= size;
         free(ptr);
     }
-}
+}
     CMD_GET_STATS = 0x04,
     CMD_DELETE = 0x05,
     CMD_FLUSH_ALL = 0x06,
-    CMD_GETQ = 0x07,
-    CMD_SETQ = 0x08,
 } protocol_commands;
 
 typedef enum {
     SUCCESS = 0x04,
     INVALID_COMMAND = 0x05,
     OUT_OF_MEMORY = 0x06,
-} errors;
+} code_t;
 
 typedef struct conn conn;
 struct conn {
     int active;					    /* conn have active events on the I/O interface */
     time_t last_heard; 				/* last time we heard from the client */
     conn_states state; 				/* state of the connection READ_KEY, READ_HEADER.etc...*/
-    request *in;					/* request instance */
-    response *out;					/* response instance */
+    request *in;					/* request */
+    response out;					/* response */
     int free; 						/* recycle connection structure */
     conn *next;						/* next connection in the linked-list of the connections */
 };

test/test_protocol.py

     def test_get_setting(self):
         self.client.chg_setting("idle_conn_timeout", 5)
         self.assertEqual(self.client.get_setting("idle_conn_timeout"), 5)
-        self.client.chg_setting("mem_avail", 65)
-        self.assertEqual(self.client.get_setting("mem_avail"), 65)
         
     def test_get_setting_invalid(self):
         self.client.get_setting("invalid_setting")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.