Minor cleanups

This commit is contained in:
pooler 2012-03-10 23:29:11 +01:00
parent c96b468d1c
commit 40fc3d06f9
3 changed files with 54 additions and 63 deletions

View file

@ -18,7 +18,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#if defined(WIN32) || defined(WIN64) #if defined(WIN32)
#include <windows.h> #include <windows.h>
#elif defined(sun) || defined(__sun) #elif defined(sun) || defined(__sun)
#include <sys/resource.h> #include <sys/resource.h>
@ -175,8 +175,7 @@ static struct option const options[] = {
{ "user", 1, NULL, 'u' }, { "user", 1, NULL, 'u' },
{ "userpass", 1, NULL, 'O' }, { "userpass", 1, NULL, 'O' },
{ "version", 0, NULL, 'V' }, { "version", 0, NULL, 'V' },
{ 0, 0, 0, 0 }
{ }
}; };
struct work { struct work {
@ -906,7 +905,7 @@ int main(int argc, char *argv[])
pthread_mutex_init(&stats_lock, NULL); pthread_mutex_init(&stats_lock, NULL);
pthread_mutex_init(&g_work_lock, NULL); pthread_mutex_init(&g_work_lock, NULL);
#if defined(WIN32) || defined(WIN64) #if defined(WIN32)
SYSTEM_INFO sysinfo; SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo); GetSystemInfo(&sysinfo);
num_processors = sysinfo.dwNumberOfProcessors; num_processors = sysinfo.dwNumberOfProcessors;

49
miner.h
View file

@ -89,12 +89,6 @@ enum {
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif #endif
struct thr_info {
int id;
pthread_t pth;
struct thread_q *q;
};
static inline uint32_t swab32(uint32_t v) static inline uint32_t swab32(uint32_t v)
{ {
#ifdef WANT_BUILTIN_BSWAP #ifdef WANT_BUILTIN_BSWAP
@ -136,43 +130,46 @@ static inline void le32enc(void *pp, uint32_t x)
p[3] = (x >> 24) & 0xff; p[3] = (x >> 24) & 0xff;
} }
extern bool opt_debug;
extern bool opt_protocol;
extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
const char *rpc_req, bool, bool, int *);
extern char *bin2hex(const unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
extern unsigned char *scrypt_buffer_alloc(); extern unsigned char *scrypt_buffer_alloc();
extern int scanhash_scrypt(int thr_id, uint32_t *pdata, extern int scanhash_scrypt(int thr_id, uint32_t *pdata,
unsigned char *scratchbuf, const uint32_t *ptarget, unsigned char *scratchbuf, const uint32_t *ptarget,
uint32_t max_nonce, unsigned long *hashes_done); uint32_t max_nonce, unsigned long *hashes_done);
extern int struct thr_info {
timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y); int id;
pthread_t pth;
extern bool fulltest(const uint32_t *hash, const uint32_t *target); struct thread_q *q;
};
extern int opt_timeout;
extern bool want_longpoll;
extern bool have_longpoll;
extern char *opt_proxy;
extern long opt_proxy_type;
struct thread_q;
struct work_restart { struct work_restart {
volatile unsigned long restart; volatile unsigned long restart;
char padding[128 - sizeof(unsigned long)]; char padding[128 - sizeof(unsigned long)];
}; };
extern pthread_mutex_t time_lock; extern bool opt_debug;
extern bool opt_protocol;
extern int opt_timeout;
extern bool want_longpoll;
extern bool have_longpoll;
extern char *opt_proxy;
extern long opt_proxy_type;
extern bool use_syslog; extern bool use_syslog;
extern pthread_mutex_t time_lock;
extern struct thr_info *thr_info; extern struct thr_info *thr_info;
extern int longpoll_thr_id; extern int longpoll_thr_id;
extern struct work_restart *work_restart; extern struct work_restart *work_restart;
extern void applog(int prio, const char *fmt, ...); extern void applog(int prio, const char *fmt, ...);
extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
const char *rpc_req, bool, bool, int *);
extern char *bin2hex(const unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
extern int timeval_subtract(struct timeval *result, struct timeval *x,
struct timeval *y);
extern bool fulltest(const uint32_t *hash, const uint32_t *target);
struct thread_q;
extern struct thread_q *tq_new(void); extern struct thread_q *tq_new(void);
extern void tq_free(struct thread_q *tq); extern void tq_free(struct thread_q *tq);
extern bool tq_push(struct thread_q *tq, void *data); extern bool tq_push(struct thread_q *tq, void *data);

29
util.c
View file

@ -33,12 +33,6 @@
#include "miner.h" #include "miner.h"
#include "elist.h" #include "elist.h"
#if JANSSON_MAJOR_VERSION >= 2
#define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
#else
#define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
#endif
struct data_buffer { struct data_buffer {
void *buf; void *buf;
size_t len; size_t len;
@ -262,14 +256,14 @@ json_t *json_rpc_call(CURL *curl, const char *url,
{ {
json_t *val, *err_val, *res_val; json_t *val, *err_val, *res_val;
int rc; int rc;
struct data_buffer all_data = { }; struct data_buffer all_data = {0};
struct upload_buffer upload_data; struct upload_buffer upload_data;
json_error_t err = { }; json_error_t err;
struct curl_slist *headers = NULL; struct curl_slist *headers = NULL;
char len_hdr[64]; char len_hdr[64];
char curl_err_str[CURL_ERROR_SIZE]; char curl_err_str[CURL_ERROR_SIZE];
long timeout = opt_timeout; long timeout = opt_timeout;
struct header_info hi = { }; struct header_info hi = {0};
bool lp_scanning = longpoll_scan && !have_longpoll; bool lp_scanning = longpoll_scan && !have_longpoll;
/* it is assumed that 'curl' is freshly [re]initialized at this pt */ /* it is assumed that 'curl' is freshly [re]initialized at this pt */
@ -347,7 +341,11 @@ json_t *json_rpc_call(CURL *curl, const char *url,
goto err_out; goto err_out;
} }
val = JSON_LOADS(all_data.buf, &err); #if JANSSON_VERSION_HEX >= 0x020000
val = json_loads(all_data.buf, 0, &err);
#else
val = json_loads(all_data.buf, &err);
#endif
if (!val) { if (!val) {
applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text); applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
goto err_out; goto err_out;
@ -360,8 +358,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
} }
/* JSON-RPC valid response returns a non-null 'result', /* JSON-RPC valid response returns a non-null 'result',
* and a null 'error'. * and a null 'error'. */
*/
res_val = json_object_get(val, "result"); res_val = json_object_get(val, "result");
err_val = json_object_get(val, "error"); err_val = json_object_get(val, "error");
@ -439,10 +436,8 @@ bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
/* Subtract the `struct timeval' values X and Y, /* Subtract the `struct timeval' values X and Y,
storing the result in RESULT. storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0. */ Return 1 if the difference is negative, otherwise 0. */
int timeval_subtract(struct timeval *result, struct timeval *x,
int struct timeval *y)
timeval_subtract (
struct timeval *result, struct timeval *x, struct timeval *y)
{ {
/* Perform the carry for the later subtraction by updating Y. */ /* Perform the carry for the later subtraction by updating Y. */
if (x->tv_usec < y->tv_usec) { if (x->tv_usec < y->tv_usec) {
@ -457,7 +452,7 @@ timeval_subtract (
} }
/* Compute the time remaining to wait. /* Compute the time remaining to wait.
`tv_usec' is certainly positive. */ * `tv_usec' is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec; result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec; result->tv_usec = x->tv_usec - y->tv_usec;