Skip to content

Commit

Permalink
Merge pull request #23 from i2p/macos-build-fixes
Browse files Browse the repository at this point in the history
Macos build fixes
  • Loading branch information
eyedeekay authored Feb 25, 2024
2 parents 2226ef0 + 03954db commit ea52a32
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 20 deletions.
43 changes: 34 additions & 9 deletions src/libsam3/libsam3.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,31 @@
#endif
#endif

#ifdef __unix__
#if defined(__unix__) || defined(__APPLE__)
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#endif

#if defined(__unix__) && !defined(__APPLE__)
#include <sys/sysinfo.h>
#endif

#if defined(__APPLE__)
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#include <mach/mach_time.h>
uint32_t TickCount() {
uint64_t mat = mach_absolute_time();
uint32_t mul = 0x80d9594e;
return ((((0xffffffff & mat) * mul) >> 32) + (mat >> 32) * mul) >> 23;
}
#endif

////////////////////////////////////////////////////////////////////////////////
int libsam3_debug = 0;

Expand Down Expand Up @@ -625,19 +642,27 @@ static inline uint32_t hashint(uint32_t a) {
}

static uint32_t genSeed(void) {
volatile uint32_t seed = 1;
uint32_t res;
#ifndef WIN32
struct sysinfo sy;
pid_t pid = getpid();
//
sysinfo(&sy);
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
hashint((uint32_t)sy.uptime);
#ifndef __APPLE__
struct sysinfo sy;
pid_t pid = getpid();
//
sysinfo(&sy);
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
hashint((uint32_t)sy.uptime);
#else
res = hashint((uint32_t)getpid()) ^
hashint((uint32_t)TickCount());
#endif
#else
res = hashint((uint32_t)GetCurrentProcessId()) ^
hashint((uint32_t)GetTickCount());
#endif
res += __sync_fetch_and_add(&seed, 1);
//
return hashint(res);
}

Expand Down
1 change: 1 addition & 0 deletions src/libsam3/libsam3.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>

#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
Expand Down
51 changes: 40 additions & 11 deletions src/libsam3a/libsam3a.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,51 @@
#endif
#endif

#ifdef __unix__
#if defined(__unix__) && !defined(__APPLE__)
#include <sys/sysinfo.h>
#endif

#if defined(__unix__) || defined(__APPLE__)
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#endif

#if defined(__APPLE__)
#include <mach/mach_time.h>
#include <netinet/tcp.h>
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC 0
#endif
#ifndef SOCK_NONBLOCK
#include <fcntl.h>
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
uint32_t TickCount() {
uint64_t mat = mach_absolute_time();
uint32_t mul = 0x80d9594e;
return ((((0xffffffff & mat) * mul) >> 32) + (mat >> 32) * mul) >> 23;
}
#endif

////////////////////////////////////////////////////////////////////////////////
int libsam3a_debug = 0;

#define DEFAULT_TCP_PORT (7656)
#define DEFAULT_UDP_PORT (7655)

////////////////////////////////////////////////////////////////////////////////
uint64_t sam3atimeval2ms(const struct timeval *tv) {
extern uint64_t sam3atimeval2ms(const struct timeval *tv) {
return ((uint64_t)tv->tv_sec) * 1000 + ((uint64_t)tv->tv_usec) / 1000;
}

void sam3ams2timeval(struct timeval *tv, uint64_t ms) {
extern void sam3ams2timeval(struct timeval *tv, uint64_t ms) {
tv->tv_sec = ms / 1000;
tv->tv_usec = (ms % 1000) * 1000;
}
Expand Down Expand Up @@ -665,13 +689,18 @@ static uint32_t genSeed(void) {
volatile uint32_t seed = 1;
uint32_t res;
#ifndef WIN32
struct sysinfo sy;
pid_t pid = getpid();
//
sysinfo(&sy);
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
hashint((uint32_t)sy.uptime);
#ifndef __APPLE__
struct sysinfo sy;
pid_t pid = getpid();
//
sysinfo(&sy);
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
hashint((uint32_t)sy.uptime);
#else
res = hashint((uint32_t)getpid()) ^
hashint((uint32_t)TickCount());
#endif
#else
res = hashint((uint32_t)GetCurrentProcessId()) ^
hashint((uint32_t)GetTickCount());
Expand Down
1 change: 1 addition & 0 deletions src/libsam3a/libsam3a.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <time.h>

#include <sys/types.h>
#include <sys/time.h>

#ifdef __MINGW32__
//#include <winsock.h>
Expand Down

0 comments on commit ea52a32

Please sign in to comment.