From 9a27b6ef6acbc96e869fbfe073080514fb14823d Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Wed, 8 May 2013 16:35:32 -0400 Subject: [PATCH] Make rocket code suitable for 32-bit architectures --- nilmdb/server/rocket.c | 79 ++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/nilmdb/server/rocket.c b/nilmdb/server/rocket.c index f1aec61..850b88a 100644 --- a/nilmdb/server/rocket.c +++ b/nilmdb/server/rocket.c @@ -5,6 +5,9 @@ #include #include +#define __STDC_FORMAT_MACROS +#include + /* Values missing from stdint.h */ #define UINT8_MIN 0 #define UINT16_MIN 0 @@ -19,13 +22,6 @@ typedef int64_t timestamp_t; -/* This code probably needs to be double-checked for the case where - sizeof(long) != 8, so enforce that here with something that will - fail at build time. We assume that the python integer type can - hold an int64_t. */ -const static char __long_ok[1 - 2*!(sizeof(int64_t) == - sizeof(long int))] = { 0 }; - /* Somewhat arbitrary, just so we can use fixed sizes for strings etc. */ static const int MAX_LAYOUT_COUNT = 1024; @@ -58,7 +54,7 @@ static PyObject *raise_str(int line, int col, int code, const char *string) static PyObject *raise_int(int line, int col, int code, int64_t num) { PyObject *o; - o = Py_BuildValue("(iiil)", line, col, code, num); + o = Py_BuildValue("(iiiL)", line, col, code, (long long)num); if (o != NULL) { PyErr_SetObject(ParseError, o); Py_DECREF(o); @@ -249,11 +245,11 @@ static PyObject *Rocket_get_file_size(Rocket *self) /**** * Append from string */ -static inline long int strtol10(const char *nptr, char **endptr) { - return strtol(nptr, endptr, 10); +static inline long int strtoll10(const char *nptr, char **endptr) { + return strtoll(nptr, endptr, 10); } -static inline long int strtoul10(const char *nptr, char **endptr) { - return strtoul(nptr, endptr, 10); +static inline long int strtoull10(const char *nptr, char **endptr) { + return strtoull(nptr, endptr, 10); } /* .append_string(count, data, offset, linenum, start, end, last_timestamp) */ @@ -264,6 +260,7 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args) int offset; const char *linestart; int linenum; + long long ll1, ll2, ll3; timestamp_t start; timestamp_t end; timestamp_t last_timestamp; @@ -280,10 +277,13 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args) but we need the null termination for strto*. If we had strnto* that took a length, we could use t# and not require a copy. */ - if (!PyArg_ParseTuple(args, "isiilll:append_string", &count, + if (!PyArg_ParseTuple(args, "isiiLLL:append_string", &count, &data, &offset, &linenum, - &start, &end, &last_timestamp)) + &ll1, &ll2, &ll3)) return NULL; + start = ll1; + end = ll2; + last_timestamp = ll3; /* Skip spaces, but don't skip over a newline. */ #define SKIP_BLANK(buf) do { \ @@ -372,14 +372,14 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args) goto extra_data_on_line; \ break - CS(INT8, strtol10, t64.i, t8.i, t8.u, , 1); - CS(UINT8, strtoul10, t64.u, t8.u, t8.u, , 1); - CS(INT16, strtol10, t64.i, t16.i, t16.u, le16toh, 2); - CS(UINT16, strtoul10, t64.u, t16.u, t16.u, le16toh, 2); - CS(INT32, strtol10, t64.i, t32.i, t32.u, le32toh, 4); - CS(UINT32, strtoul10, t64.u, t32.u, t32.u, le32toh, 4); - CS(INT64, strtol10, t64.i, t64.i, t64.u, le64toh, 8); - CS(UINT64, strtoul10, t64.u, t64.u, t64.u, le64toh, 8); + CS(INT8, strtoll10, t64.i, t8.i, t8.u, , 1); + CS(UINT8, strtoull10, t64.u, t8.u, t8.u, , 1); + CS(INT16, strtoll10, t64.i, t16.i, t16.u, le16toh, 2); + CS(UINT16, strtoull10, t64.u, t16.u, t16.u, le16toh, 2); + CS(INT32, strtoll10, t64.i, t32.i, t32.u, le32toh, 4); + CS(UINT32, strtoull10, t64.u, t32.u, t32.u, le32toh, 4); + CS(INT64, strtoll10, t64.i, t64.i, t64.u, le64toh, 8); + CS(UINT64, strtoull10, t64.u, t64.u, t64.u, le64toh, 8); CS(FLOAT32, strtod, t64.d, t32.f, t32.u, le32toh, 4); CS(FLOAT64, strtod, t64.d, t64.d, t64.u, le64toh, 8); #undef CS @@ -397,7 +397,8 @@ static PyObject *Rocket_append_string(Rocket *self, PyObject *args) /* Build return value and return */ offset = buf - data; PyObject *o; - o = Py_BuildValue("(iili)", written, offset, last_timestamp, linenum); + o = Py_BuildValue("(iiLi)", written, offset, + (long long)last_timestamp, linenum); return o; err: PyErr_SetFromErrno(PyExc_OSError); @@ -431,14 +432,18 @@ static PyObject *Rocket_append_binary(Rocket *self, PyObject *args) int data_len; int linenum; int offset; + long long ll1, ll2, ll3; timestamp_t start; timestamp_t end; timestamp_t last_timestamp; - if (!PyArg_ParseTuple(args, "it#iilll:append_binary", + if (!PyArg_ParseTuple(args, "it#iiLLL:append_binary", &count, &data, &data_len, &offset, - &linenum, &start, &end, &last_timestamp)) + &linenum, &ll1, &ll2, &ll3)) return NULL; + start = ll1; + end = ll2; + last_timestamp = ll3; /* Advance to offset */ if (offset > data_len) @@ -476,8 +481,8 @@ static PyObject *Rocket_append_binary(Rocket *self, PyObject *args) /* Build return value and return */ PyObject *o; - o = Py_BuildValue("(iili)", rows, offset + rows * self->binary_size, - last_timestamp, linenum); + o = Py_BuildValue("(iiLi)", rows, offset + rows * self->binary_size, + (long long)last_timestamp, linenum); return o; } @@ -534,7 +539,7 @@ static PyObject *Rocket_extract_string(Rocket *self, PyObject *args) if (fread(&t64.u, 8, 1, self->file) != 1) goto err; t64.u = le64toh(t64.u); - ret = sprintf(&str[len], "%ld", t64.i); + ret = sprintf(&str[len], "%" PRId64, t64.i); if (ret <= 0) goto err; len += ret; @@ -556,14 +561,14 @@ static PyObject *Rocket_extract_string(Rocket *self, PyObject *args) len += ret; \ } \ break - CASE(INT8, "%hhd", t8.i, t8.u, , 1); - CASE(UINT8, "%hhu", t8.u, t8.u, , 1); - CASE(INT16, "%hd", t16.i, t16.u, le16toh, 2); - CASE(UINT16, "%hu", t16.u, t16.u, le16toh, 2); - CASE(INT32, "%d", t32.i, t32.u, le32toh, 4); - CASE(UINT32, "%u", t32.u, t32.u, le32toh, 4); - CASE(INT64, "%ld", t64.i, t64.u, le64toh, 8); - CASE(UINT64, "%lu", t64.u, t64.u, le64toh, 8); + CASE(INT8, "%" PRId8, t8.i, t8.u, , 1); + CASE(UINT8, "%" PRIu8, t8.u, t8.u, , 1); + CASE(INT16, "%" PRId16, t16.i, t16.u, le16toh, 2); + CASE(UINT16, "%" PRIu16, t16.u, t16.u, le16toh, 2); + CASE(INT32, "%" PRId32, t32.i, t32.u, le32toh, 4); + CASE(UINT32, "%" PRIu32, t32.u, t32.u, le32toh, 4); + CASE(INT64, "%" PRId64, t64.i, t64.u, le64toh, 8); + CASE(UINT64, "%" PRIu64, t64.u, t64.u, le64toh, 8); /* These next two are a bit debatable. floats are 6-9 significant figures, so we print 7. Doubles are 15-19, so we print 17. This is @@ -653,7 +658,7 @@ static PyObject *Rocket_extract_timestamp(Rocket *self, PyObject *args) /* Convert and return */ t64.u = le64toh(t64.u); - return Py_BuildValue("l", t64.i); + return Py_BuildValue("L", (long long)t64.i); } /****