Skip to content

Commit

Permalink
Removed compile errors from gcc
Browse files Browse the repository at this point in the history
  • Loading branch information
trondn authored and ingenthr committed Jul 19, 2010
1 parent 84ba839 commit d3dd0bb
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 108 deletions.
11 changes: 6 additions & 5 deletions boxmuller.c
Expand Up @@ -10,16 +10,17 @@
*/ */
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include "boxmuller.h"


double box_muller(double m, double s) /* normal random variate generator */ double box_muller(double m, double s) /* normal random variate generator */
{ /* mean m, standard deviation s */ { /* mean m, standard deviation s */
double x1, x2, w, y1; double x1, x2, w, _y1;
static double y2; static double y2;
static int use_last = 0; static int use_last = 0;


if (use_last) /* use value from previous call */ if (use_last) /* use value from previous call */
{ {
y1 = y2; _y1 = y2;
use_last = 0; use_last = 0;
} }
else else
Expand All @@ -31,12 +32,12 @@ double box_muller(double m, double s) /* normal random variate generator */
} while ( w >= 1.0 ); } while ( w >= 1.0 );


w = sqrt( (-2.0 * log( w ) ) / w ); w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w; _y1 = x1 * w;
y2 = x2 * w; y2 = x2 * w;
use_last = 1; use_last = 1;
} }

double result = ( m + y1 * s ); double result = ( m + _y1 * s );


return result; return result;
} }
6 changes: 6 additions & 0 deletions boxmuller.h
@@ -0,0 +1,6 @@
#ifndef BOX_MULLER_H
#define BOX_MULLER_H 1

extern double box_muller(double m, double s);

#endif
79 changes: 32 additions & 47 deletions libmemc.c
@@ -1,3 +1,4 @@
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* /*
* CDDL HEADER START * CDDL HEADER START
* *
Expand Down Expand Up @@ -25,7 +26,7 @@


#include "libmemc.h" #include "libmemc.h"


#if HAVE_PROTOCOL_BINARY #ifdef HAVE_MEMCACHED_PROTOCOL_BINARY_H
#include <memcached/protocol_binary.h> #include <memcached/protocol_binary.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
Expand All @@ -47,7 +48,7 @@ struct Server {
const char *errmsg; const char *errmsg;
const char *peername; const char *peername;
char *buffer; char *buffer;
int buffersize; size_t buffersize;
}; };


enum StoreCommand {add, set, replace}; enum StoreCommand {add, set, replace};
Expand Down Expand Up @@ -148,15 +149,14 @@ int libmemc_get(struct Memcache *handle, struct Item *item) {
static struct addrinfo *lookuphost(const char *hostname, in_port_t port) static struct addrinfo *lookuphost(const char *hostname, in_port_t port)
{ {
struct addrinfo *ai = 0; struct addrinfo *ai = 0;
struct addrinfo hints = {0}; struct addrinfo hints = {
.ai_flags = AI_PASSIVE|AI_ADDRCONFIG,
.ai_family = AF_UNSPEC,
.ai_protocol = IPPROTO_TCP,
.ai_socktype = SOCK_STREAM };
char service[NI_MAXSERV]; char service[NI_MAXSERV];
int error; int error;


hints.ai_flags = AI_PASSIVE|AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_socktype = SOCK_STREAM;

(void)snprintf(service, NI_MAXSERV, "%d", port); (void)snprintf(service, NI_MAXSERV, "%d", port);
if ((error = getaddrinfo(hostname, service, &hints, &ai)) != 0) { if ((error = getaddrinfo(hostname, service, &hints, &ai)) != 0) {
if (error != EAI_SYSTEM) { if (error != EAI_SYSTEM) {
Expand Down Expand Up @@ -238,8 +238,6 @@ static int libmemc_store(struct Memcache* handle, enum StoreCommand cmd,


static size_t server_receive(struct Server* server, char* data, size_t size, int line); static size_t server_receive(struct Server* server, char* data, size_t size, int line);
static int server_sendv(struct Server* server, struct iovec *iov, int iovcnt); static int server_sendv(struct Server* server, struct iovec *iov, int iovcnt);
static int server_send(struct Server* server, const void *data, size_t size);
static int server_connect(struct Server *server);
static void server_disconnect(struct Server *server); static void server_disconnect(struct Server *server);


void server_destroy(struct Server *server) { void server_destroy(struct Server *server) {
Expand Down Expand Up @@ -314,26 +312,6 @@ static int server_connect(struct Server *server)
return 0; return 0;
} }


static int server_send(struct Server* server, const void *data, size_t size) {
size_t offset = 0;
do {
ssize_t sent = send(server->sock, ((const char*)data) + offset, size - offset, 0);
if (sent == -1) {
if (errno != EINTR) {
char errmsg[1024];
sprintf(errmsg, "Failed to send data to server: %s", strerror(errno));
server->errmsg = strdup(errmsg);
server_disconnect(server);
return -1;
}
} else {
offset += sent;
}
} while (offset < size);

return 0;
}

static int server_sendv(struct Server* server, struct iovec *iov, int iovcnt) { static int server_sendv(struct Server* server, struct iovec *iov, int iovcnt) {
#ifdef WIN32 #ifdef WIN32
// @todo I might have a scattered IO function on windows... // @todo I might have a scattered IO function on windows...
Expand Down Expand Up @@ -361,12 +339,12 @@ static int server_sendv(struct Server* server, struct iovec *iov, int iovcnt) {
return -1; return -1;
} }
} else { } else {
if (sent == size) { if ((size_t)sent == size) {
return 0; return 0;
} }


for (int ii = 0; ii < iovcnt && sent > 0; ++ii) { for (int ii = 0; ii < iovcnt && sent > 0; ++ii) {
if (iov[ii].iov_len < sent) { if (iov[ii].iov_len < (size_t)sent) {
size -= iov[ii].iov_len; size -= iov[ii].iov_len;
sent -= iov[ii].iov_len; sent -= iov[ii].iov_len;
iov[ii].iov_len = 0; iov[ii].iov_len = 0;
Expand Down Expand Up @@ -415,7 +393,7 @@ static size_t server_receive(struct Server* server, char* data, size_t size, int
return offset; return offset;
} }



#ifdef HAVE_MEMCACHED_PROTOCOL_BINARY_H
/* Byte swap a 64-bit number */ /* Byte swap a 64-bit number */
static int64_t swap64(int64_t in) { static int64_t swap64(int64_t in) {
#ifndef __sparc #ifndef __sparc
Expand All @@ -433,14 +411,18 @@ static int64_t swap64(int64_t in) {
return in; return in;
#endif #endif
} }

#endif


/** /**
* Implementation of the Binary protocol * Implementation of the Binary protocol
*/ */
static int binary_get(struct Server* server, struct Item* item) static int binary_get(struct Server* server, struct Item* item)
{ {
#if HAVE_PROTOCOL_BINARY #ifndef HAVE_MEMCACHED_PROTOCOL_BINARY_H
(void)server;
(void)item;
return -1;
#else
uint16_t keylen = item->keylen; uint16_t keylen = item->keylen;
uint32_t bodylen = keylen; uint32_t bodylen = keylen;


Expand Down Expand Up @@ -490,23 +472,23 @@ static int binary_get(struct Server* server, struct Item* item)
if (response.message.header.response.extlen != 0) { if (response.message.header.response.extlen != 0) {
assert(response.message.header.response.extlen == 4); assert(response.message.header.response.extlen == 4);
uint32_t flags; uint32_t flags;
struct iovec iovec[2]; struct iovec iov[2];
iovec[0].iov_base = (void*)&flags; iov[0].iov_base = (void*)&flags;
iovec[0].iov_len = sizeof(flags); iov[0].iov_len = sizeof(flags);
iovec[1].iov_base = item->data; iov[1].iov_base = item->data;
iovec[1].iov_len = item->size; iov[1].iov_len = item->size;


ssize_t nread = readv(server->sock, iovec, 2); nread = readv(server->sock, iovec, 2);
if (nread < bodylen) { if (nread < bodylen) {
// partial read.. read the rest! // partial read.. read the rest!
nread -= 4; nread -= 4;
size_t left = item->size - nread; size_t left = item->size - nread;
if (server_receive(server, item->data + nread, left, 0) != left) { if (server_receive(server, (char*)(item->data) + nread, left, 0) != left) {
abort(); abort();
} }
} }
} else { } else {
size_t nread = server_receive(server, item->data, item->size, 0); nread = server_receive(server, item->data, item->size, 0);
assert(nread == item->size); assert(nread == item->size);
} }


Expand All @@ -527,14 +509,18 @@ static int binary_get(struct Server* server, struct Item* item)


return 0; return 0;
#endif #endif
return -1;
} }


static int binary_store(struct Server* server, static int binary_store(struct Server* server,
enum StoreCommand cmd, enum StoreCommand cmd,
const struct Item *item) const struct Item *item)
{ {
#if HAVE_PROTOCOL_BINARY #ifndef HAVE_MEMCACHED_PROTOCOL_BINARY_H
(void)server;
(void)cmd;
(void)item;
return -1;
#else
protocol_binary_request_set request = { .bytes = {0} }; protocol_binary_request_set request = { .bytes = {0} };
request.message.header.request.magic = PROTOCOL_BINARY_REQ; request.message.header.request.magic = PROTOCOL_BINARY_REQ;


Expand Down Expand Up @@ -593,13 +579,12 @@ static int binary_store(struct Server* server,
return -1; return -1;
} }


size_t nread = server_receive(server, buffer, len, 0); nread = server_receive(server, buffer, len, 0);
free(buffer); free(buffer);
} }


return (response.message.header.response.status == 0) ? 0 : -1; return (response.message.header.response.status == 0) ? 0 : -1;
#endif #endif
return -1;
} }


/** /**
Expand Down

0 comments on commit d3dd0bb

Please sign in to comment.