Add a safe wrapper for write and fix some warnings

1. Add a function writeall and make swrite wrap that function. Use either writeall or swrite, depending on whether we want to exit on errors or not.
2. Fix warnings when compiling with a higher optimisation level.
(CFLAGS ?= -pipe -O3 -march=native -mtune=native -freorder-blocks-and-partition)

Signed-off-by: hwangcc <hwangcc@csie.nctu.edu.tw>
This commit is contained in:
hwangcc
2015-03-24 20:57:06 +08:00
parent 822cd3bf1b
commit 42515308e7
12 changed files with 90 additions and 80 deletions

View File

@ -32,33 +32,11 @@ int ipc_send_message(int sockfd, const uint32_t message_size,
.size = message_size,
.type = message_type};
size_t sent_bytes = 0;
int n = 0;
if (writeall(sockfd, ((void *)&header), sizeof(i3_ipc_header_t)) == -1)
return -1;
/* This first loop is basically unnecessary. No operating system has
* buffers which cannot fit 14 bytes into them, so the write() will only be
* called once. */
while (sent_bytes < sizeof(i3_ipc_header_t)) {
if ((n = write(sockfd, ((void *)&header) + sent_bytes, sizeof(i3_ipc_header_t) - sent_bytes)) == -1) {
if (errno == EAGAIN)
continue;
return -1;
}
sent_bytes += n;
}
sent_bytes = 0;
while (sent_bytes < message_size) {
if ((n = write(sockfd, payload + sent_bytes, message_size - sent_bytes)) == -1) {
if (errno == EAGAIN)
continue;
return -1;
}
sent_bytes += n;
}
if (writeall(sockfd, payload, message_size) == -1)
return -1;
return 0;
}