verify: Clarify assertion message.
[libfirm] / ir / lpp / lpp_comm.c
index 21ce281..8d307e8 100644 (file)
@@ -1,14 +1,29 @@
-/**
- * @file   lpp_comm.c
- * @date   21.07.2005
- * @author Sebastian Hack
+/*
+ * Copyright (C) 2005-2011 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
  *
- * Protocol stuff for lpp server
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
  *
- * Copyright (C) 2005 Universitaet Karlsruhe
- * Released under the GPL
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
+/**
+ * @file
+ * @brief   Protocol stuff for lpp server
+ * @author  Sebastian Hack
+ */
+#include "config.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
@@ -18,9 +33,8 @@
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #include <winsock2.h>
-
-#define vsnprintf _vsnprintf
-
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
 #else
 #include <unistd.h>
 #include <sys/types.h>
 #include <arpa/inet.h>
 #endif
 
-#include "irtools.h"
+#include "xmalloc.h"
+#include "util.h"
 #include "debug.h"
 
 #include "lpp_comm.h"
 
-/* undef to disable debugging */
-#undef  ENABLE_DEBUGGING
-
 struct _lpp_comm_t {
        int fd;
        size_t buf_size;
@@ -46,6 +58,7 @@ struct _lpp_comm_t {
        char *r_buf;
 };
 
+#ifdef DEBUG_libfirm
 static inline firm_dbg_module_t *get_dbg_module(void)
 {
        static firm_dbg_module_t *dbg = NULL;
@@ -55,25 +68,25 @@ static inline firm_dbg_module_t *get_dbg_module(void)
 
        return dbg;
 }
-
 #define dbg get_dbg_module()
+#endif
 
 /**
  * Try to read some bytes but block until a certain amount is read.
  * @param fd The file descriptor.
  * @param buf The buffer to read into.
- * @param try The amount of bytes to try to read.
+ * @param try_amount The amount of bytes to try to read.
  * @param at_least block until this many bytes are read.
  * @return The number of bytes read or -1 on error.
  */
-static ssize_t secure_recv(int fd, void *buf, size_t try, size_t at_least)
+static ssize_t secure_recv(int fd, void *buf, size_t try_amount, size_t at_least)
 {
        ssize_t res;
        size_t bytes_read = 0;
-       char *data = buf;
+       char *data = (char*)buf;
 
        do {
-               res = recv(fd, &data[bytes_read], try - bytes_read, 0);
+               res = recv(fd, &data[bytes_read], try_amount - bytes_read, 0);
                if(res <= 0) {
                        if(res == 0 || errno != EAGAIN)
                                return -1;
@@ -91,7 +104,7 @@ static ssize_t secure_send(int fd, const void *buf, size_t n)
 {
        ssize_t res;
        size_t bytes_written = 0;
-       const char *data = buf;
+       const char *data = (const char*)buf;
 
        do {
                res = send(fd, &data[bytes_written], n - bytes_written, 0);
@@ -108,7 +121,7 @@ static ssize_t secure_send(int fd, const void *buf, size_t n)
        return n;
 }
 
-ssize_t lpp_flush(lpp_comm_t *comm)
+static ssize_t lpp_flush_(lpp_comm_t *comm)
 {
        ssize_t res = 0;
        if(comm->w_pos - comm->w_buf > 0) {
@@ -122,6 +135,11 @@ ssize_t lpp_flush(lpp_comm_t *comm)
        return res;
 }
 
+void lpp_flush(lpp_comm_t *comm)
+{
+       lpp_flush_(comm);
+}
+
 static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
 {
        assert(comm->w_pos - comm->w_buf >= 0);
@@ -131,7 +149,7 @@ static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
                size_t free = (comm->w_buf + comm->buf_size) - comm->w_pos;
                size_t copy = MIN(free, len);
                size_t rest = len - copy;
-               const char *pos   = buf;
+               const char *pos = (const char*)buf;
 
                DBG((dbg, LEVEL_1, "\tfree = %d, copy = %d, rest = %d\n", free, copy, rest));
                if(copy > 0) {
@@ -149,7 +167,7 @@ static ssize_t lpp_write(lpp_comm_t *comm, const void *buf, size_t len)
                        size_t n_direct = rest / comm->buf_size;
                        size_t last_rest;
 
-                       if(lpp_flush(comm) < 0)
+                       if(lpp_flush_(comm) < 0)
                                return -1;
 
                        for(i = 0; i < n_direct; ++i) {
@@ -180,7 +198,7 @@ static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
                size_t left = comm->r_max - comm->r_pos;
                size_t copy = MIN(left, len);
                size_t rest = len - copy;
-               char *pos   = buf;
+               char *pos = (char*)buf;
 
                DBG((dbg, LEVEL_1, "\tleft = %d, copy = %d, rest = %d\n", left, copy, rest));
                if(copy > 0) {
@@ -234,12 +252,12 @@ static ssize_t lpp_read(lpp_comm_t *comm, void *buf, size_t len)
 
 lpp_comm_t *lpp_comm_new(int fd, size_t buf_size)
 {
-       lpp_comm_t *res = malloc(sizeof(res[0]));
+       lpp_comm_t *res = XMALLOCZ(lpp_comm_t);
 
        res->fd       = fd;
-       res->w_buf    = malloc(buf_size);
+       res->w_buf    = XMALLOCN(char, buf_size);
        res->w_pos    = res->w_buf;
-       res->r_buf    = malloc(buf_size);
+       res->r_buf    = XMALLOCN(char, buf_size);
        res->r_pos    = res->r_buf;
        res->r_max    = res->r_buf;
        res->buf_size = buf_size;
@@ -323,7 +341,7 @@ double lpp_readd(lpp_comm_t *comm)
 char *lpp_reads(lpp_comm_t *comm)
 {
        size_t len = lpp_readl(comm);
-       char *res = malloc(sizeof(char) * (len + 1));
+       char *res = XMALLOCN(char, len+1);
 
        ERRNO_CHECK(lpp_read(comm, res, len), !=, (ssize_t) len);
        res[len] = '\0';