Skip to content

Commit ea52a32

Browse files
authored
Merge pull request #23 from i2p/macos-build-fixes
Macos build fixes
2 parents 2226ef0 + 03954db commit ea52a32

File tree

4 files changed

+76
-20
lines changed

4 files changed

+76
-20
lines changed

src/libsam3/libsam3.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,31 @@
4848
#endif
4949
#endif
5050

51-
#ifdef __unix__
51+
#if defined(__unix__) || defined(__APPLE__)
5252
#include <arpa/inet.h>
5353
#include <netdb.h>
5454
#include <netinet/in.h>
5555
#include <sys/socket.h>
56-
#include <sys/sysinfo.h>
5756
#include <sys/types.h>
57+
#include <sys/ioctl.h>
58+
#endif
59+
60+
#if defined(__unix__) && !defined(__APPLE__)
61+
#include <sys/sysinfo.h>
62+
#endif
63+
64+
#if defined(__APPLE__)
65+
#ifndef MSG_NOSIGNAL
66+
#define MSG_NOSIGNAL 0
67+
#endif
68+
#include <mach/mach_time.h>
69+
uint32_t TickCount() {
70+
uint64_t mat = mach_absolute_time();
71+
uint32_t mul = 0x80d9594e;
72+
return ((((0xffffffff & mat) * mul) >> 32) + (mat >> 32) * mul) >> 23;
73+
}
5874
#endif
75+
5976
////////////////////////////////////////////////////////////////////////////////
6077
int libsam3_debug = 0;
6178

@@ -625,19 +642,27 @@ static inline uint32_t hashint(uint32_t a) {
625642
}
626643

627644
static uint32_t genSeed(void) {
645+
volatile uint32_t seed = 1;
628646
uint32_t res;
629647
#ifndef WIN32
630-
struct sysinfo sy;
631-
pid_t pid = getpid();
632-
//
633-
sysinfo(&sy);
634-
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
635-
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
636-
hashint((uint32_t)sy.uptime);
648+
#ifndef __APPLE__
649+
struct sysinfo sy;
650+
pid_t pid = getpid();
651+
//
652+
sysinfo(&sy);
653+
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
654+
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
655+
hashint((uint32_t)sy.uptime);
656+
#else
657+
res = hashint((uint32_t)getpid()) ^
658+
hashint((uint32_t)TickCount());
659+
#endif
637660
#else
638661
res = hashint((uint32_t)GetCurrentProcessId()) ^
639662
hashint((uint32_t)GetTickCount());
640663
#endif
664+
res += __sync_fetch_and_add(&seed, 1);
665+
//
641666
return hashint(res);
642667
}
643668

src/libsam3/libsam3.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdbool.h>
2929
#include <stdint.h>
3030
#include <stdlib.h>
31+
#include <sys/types.h>
3132

3233
#ifndef _SSIZE_T_DEFINED
3334
#define _SSIZE_T_DEFINED

src/libsam3a/libsam3a.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,51 @@
4848
#endif
4949
#endif
5050

51-
#ifdef __unix__
51+
#if defined(__unix__) && !defined(__APPLE__)
52+
#include <sys/sysinfo.h>
53+
#endif
54+
55+
#if defined(__unix__) || defined(__APPLE__)
5256
#include <arpa/inet.h>
5357
#include <netdb.h>
5458
#include <netinet/in.h>
5559
#include <sys/ioctl.h>
5660
#include <sys/socket.h>
57-
#include <sys/sysinfo.h>
5861
#include <sys/types.h>
5962
#endif
63+
64+
#if defined(__APPLE__)
65+
#include <mach/mach_time.h>
66+
#include <netinet/tcp.h>
67+
#ifndef SOCK_CLOEXEC
68+
#define SOCK_CLOEXEC 0
69+
#endif
70+
#ifndef SOCK_NONBLOCK
71+
#include <fcntl.h>
72+
#define SOCK_NONBLOCK O_NONBLOCK
73+
#endif
74+
#ifndef MSG_NOSIGNAL
75+
#define MSG_NOSIGNAL 0
76+
#endif
77+
uint32_t TickCount() {
78+
uint64_t mat = mach_absolute_time();
79+
uint32_t mul = 0x80d9594e;
80+
return ((((0xffffffff & mat) * mul) >> 32) + (mat >> 32) * mul) >> 23;
81+
}
82+
#endif
83+
6084
////////////////////////////////////////////////////////////////////////////////
6185
int libsam3a_debug = 0;
6286

6387
#define DEFAULT_TCP_PORT (7656)
6488
#define DEFAULT_UDP_PORT (7655)
6589

6690
////////////////////////////////////////////////////////////////////////////////
67-
uint64_t sam3atimeval2ms(const struct timeval *tv) {
91+
extern uint64_t sam3atimeval2ms(const struct timeval *tv) {
6892
return ((uint64_t)tv->tv_sec) * 1000 + ((uint64_t)tv->tv_usec) / 1000;
6993
}
7094

71-
void sam3ams2timeval(struct timeval *tv, uint64_t ms) {
95+
extern void sam3ams2timeval(struct timeval *tv, uint64_t ms) {
7296
tv->tv_sec = ms / 1000;
7397
tv->tv_usec = (ms % 1000) * 1000;
7498
}
@@ -665,13 +689,18 @@ static uint32_t genSeed(void) {
665689
volatile uint32_t seed = 1;
666690
uint32_t res;
667691
#ifndef WIN32
668-
struct sysinfo sy;
669-
pid_t pid = getpid();
670-
//
671-
sysinfo(&sy);
672-
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
673-
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
674-
hashint((uint32_t)sy.uptime);
692+
#ifndef __APPLE__
693+
struct sysinfo sy;
694+
pid_t pid = getpid();
695+
//
696+
sysinfo(&sy);
697+
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
698+
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
699+
hashint((uint32_t)sy.uptime);
700+
#else
701+
res = hashint((uint32_t)getpid()) ^
702+
hashint((uint32_t)TickCount());
703+
#endif
675704
#else
676705
res = hashint((uint32_t)GetCurrentProcessId()) ^
677706
hashint((uint32_t)GetTickCount());

src/libsam3a/libsam3a.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <time.h>
3232

3333
#include <sys/types.h>
34+
#include <sys/time.h>
3435

3536
#ifdef __MINGW32__
3637
//#include <winsock.h>

0 commit comments

Comments
 (0)