Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Macos support #15

Open
wants to merge 7 commits into
base: unity
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions crnlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ OBJECTS = \
crn_utils.o \
crn_value.o \
crn_vector.o \
crn_zeng.o \
crn_texture_comp.o \
crn_texture_conversion.o \
crn_dds_comp.o \
Expand Down Expand Up @@ -80,17 +79,20 @@ OBJECTS = \
all: crunch

%.o: %.cpp
g++ $< -o $@ -c $(COMPILE_OPTIONS)
$(CXX) $< -o $@ -c $(COMPILE_OPTIONS)

crunch.o: ../crunch/crunch.cpp
g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
$(CXX) $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)

corpus_gen.o: ../crunch/corpus_gen.cpp
g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
$(CXX) $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)

corpus_test.o: ../crunch/corpus_test.cpp
g++ $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)
$(CXX) $< -o $@ -c -I../inc -I../crnlib $(COMPILE_OPTIONS)

crunch: $(OBJECTS) crunch.o corpus_gen.o corpus_test.o
g++ $(OBJECTS) crunch.o corpus_gen.o corpus_test.o -o crunch $(LINKER_OPTIONS)
$(CXX) $(OBJECTS) crunch.o corpus_gen.o corpus_test.o -o crunch $(LINKER_OPTIONS)

clean:
rm -f *.o
rm -f ../crunch/*.o
5 changes: 3 additions & 2 deletions crnlib/crn_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#ifdef WIN32
#include <tchar.h>
#include <conio.h>
#elif defined(__GNUC__)
#include <termios.h>
#include <unistd.h>
#endif
namespace crnlib {
class dynamic_string;
Expand Down Expand Up @@ -99,8 +102,6 @@ inline int crn_getch() {
return _getch();
}
#elif defined(__GNUC__)
#include <termios.h>
#include <unistd.h>
inline int crn_getch() {
struct termios oldt, newt;
int ch;
Expand Down
4 changes: 4 additions & 0 deletions crnlib/crn_jpge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

#include <stdlib.h>
#include <string.h>
#if !defined(__APPLE__)
#include <malloc.h>
#else
#include <malloc/malloc.h>
#endif

#include "crn_core.h"

Expand Down
8 changes: 7 additions & 1 deletion crnlib/crn_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
#include "crn_core.h"
#include "crn_console.h"
#include "../inc/crnlib.h"
#if !defined(__APPLE__)
#include <malloc.h>
#else
#include <malloc/malloc.h>
#endif
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif

#define CRNLIB_MEM_STATS 0

#if !CRNLIB_USE_WIN32_API
#if defined(__APPLE__)
#define _msize malloc_size
#elif !CRNLIB_USE_WIN32_API
#define _msize malloc_usable_size
#endif

Expand Down
2 changes: 1 addition & 1 deletion crnlib/crn_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const bool c_crnlib_little_endian_platform = false;

const bool c_crnlib_big_endian_platform = !c_crnlib_little_endian_platform;

#ifdef __GNUC__
#if defined(__GNUC__) && !defined(__APPLE__)
#define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen64(f, m)
#define crn_fseek fseeko64
#define crn_ftell ftello64
Expand Down
73 changes: 60 additions & 13 deletions crnlib/crn_threading_pthreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
#include "crn_winhdr.h"
#endif

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

#ifdef WIN32
#include <process.h>
#endif

#if defined(__APPLE__)
#include <sys/sysctl.h>
#endif

namespace crnlib {
uint g_number_of_processors = 1;

Expand All @@ -27,6 +31,8 @@ void crn_threading_init() {
SYSTEM_INFO g_system_info;
GetSystemInfo(&g_system_info);
g_number_of_processors = math::maximum<uint>(1U, g_system_info.dwNumberOfProcessors);
#elif defined(__APPLE__)
g_number_of_processors = math::maximum<int>(1, sysconf(_SC_NPROCESSORS_ONLN));
#elif defined(__GNUC__)
g_number_of_processors = math::maximum<int>(1, get_nprocs());
#else
Expand All @@ -36,7 +42,7 @@ void crn_threading_init() {

crn_thread_id_t crn_get_current_thread_id() {
// FIXME: Not portable
return static_cast<crn_thread_id_t>(pthread_self());
return (crn_thread_id_t)(pthread_self());
}

void crn_sleep(unsigned int milliseconds) {
Expand Down Expand Up @@ -95,15 +101,35 @@ void mutex::set_spin_count(unsigned int count) {
}

semaphore::semaphore(long initialCount, long maximumCount, const char* pName) {
maximumCount, pName;
CRNLIB_ASSERT(maximumCount >= initialCount);
if (sem_init(&m_sem, 0, initialCount)) {
#if !defined(__APPLE__)
maximumCount, pName;
m_sem = new sem_t();
if (sem_init(m_sem, 0, initialCount)) {
CRNLIB_FAIL("semaphore: sem_init() failed");
}
#else
m_name = pName ? pName : "semaphore";
for(int i = 0; i < 256; i++) {
m_sem = sem_open(m_name, O_CREAT | O_EXCL, 0644, initialCount);
if (m_sem != SEM_FAILED)
{
break;
}
sem_unlink(m_name);
}
if (m_sem == SEM_FAILED)
{
CRNLIB_FAIL("semaphore: sem_open() failed");
}
#endif
}

semaphore::~semaphore() {
sem_destroy(&m_sem);
sem_destroy(m_sem);
#if defined(__APPLE__)
sem_unlink(m_name);
#endif
}

void semaphore::release(long releaseCount) {
Expand All @@ -112,12 +138,12 @@ void semaphore::release(long releaseCount) {
int status = 0;
#ifdef WIN32
if (1 == releaseCount)
status = sem_post(&m_sem);
status = sem_post(m_sem);
else
status = sem_post_multiple(&m_sem, releaseCount);
status = sem_post_multiple(m_sem, releaseCount);
#else
while (releaseCount > 0) {
status = sem_post(&m_sem);
status = sem_post(m_sem);
if (status)
break;
releaseCount--;
Expand All @@ -134,12 +160,12 @@ void semaphore::try_release(long releaseCount) {

#ifdef WIN32
if (1 == releaseCount)
sem_post(&m_sem);
sem_post(m_sem);
else
sem_post_multiple(&m_sem, releaseCount);
sem_post_multiple(m_sem, releaseCount);
#else
while (releaseCount > 0) {
sem_post(&m_sem);
sem_post(m_sem);
releaseCount--;
}
#endif
Expand All @@ -148,12 +174,16 @@ void semaphore::try_release(long releaseCount) {
bool semaphore::wait(uint32 milliseconds) {
int status;
if (milliseconds == cUINT32_MAX) {
status = sem_wait(&m_sem);
status = sem_wait(m_sem);
} else {
#if !defined(__APPLE__)
struct timespec interval;
interval.tv_sec = milliseconds / 1000;
interval.tv_nsec = (milliseconds % 1000) * 1000000L;
status = sem_timedwait(&m_sem, &interval);
status = sem_timedwait(m_sem, &interval);
#else
status = sem_wait(m_sem);
#endif
}

if (status) {
Expand All @@ -167,25 +197,42 @@ bool semaphore::wait(uint32 milliseconds) {
}

spinlock::spinlock() {
#if !defined(__APPLE__)
if (pthread_spin_init(&m_spinlock, 0)) {
CRNLIB_FAIL("spinlock: pthread_spin_init() failed");
}
#else
m_lock = new os_unfair_lock();
*m_lock = OS_UNFAIR_LOCK_INIT;
#endif
}

spinlock::~spinlock() {
#if !defined(__APPLE__)
pthread_spin_destroy(&m_spinlock);
#else
delete m_lock;
#endif
}

void spinlock::lock() {
#if !defined(__APPLE__)
if (pthread_spin_lock(&m_spinlock)) {
CRNLIB_FAIL("spinlock: pthread_spin_lock() failed");
}
#else
os_unfair_lock_lock(m_lock);
#endif
}

void spinlock::unlock() {
#if !defined(__APPLE__)
if (pthread_spin_unlock(&m_spinlock)) {
CRNLIB_FAIL("spinlock: pthread_spin_unlock() failed");
}
#else
os_unfair_lock_unlock(m_lock);
#endif
}

task_pool::task_pool()
Expand Down
13 changes: 12 additions & 1 deletion crnlib/crn_threading_pthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <semaphore.h>
#include <unistd.h>

#if defined(__APPLE__)
#include <os/lock.h>
#endif

namespace crnlib {
// g_number_of_processors defaults to 1. Will be higher on multicore machines.
extern uint g_number_of_processors;
Expand Down Expand Up @@ -71,7 +75,10 @@ class semaphore {
bool wait(uint32 milliseconds = cUINT32_MAX);

private:
sem_t m_sem;
sem_t* m_sem;
#if defined(__APPLE__)
const char* m_name;
#endif
};

class spinlock {
Expand All @@ -83,7 +90,11 @@ class spinlock {
void unlock();

private:
#if !defined(__APPLE__)
pthread_spinlock_t m_spinlock;
#else
os_unfair_lock_t m_lock;
#endif
};

class scoped_spinlock {
Expand Down
4 changes: 2 additions & 2 deletions crnlib/crn_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ bool elemental_vector::increase_capacity(uint min_new_capacity, bool grow_hint,
return true;

size_t new_capacity = min_new_capacity;
if ((grow_hint) && (!math::is_power_of_2(new_capacity)))
new_capacity = math::next_pow2(new_capacity);
if ((grow_hint) && (!math::is_power_of_2((uint64)new_capacity)))
new_capacity = math::next_pow2((uint64)new_capacity);

CRNLIB_ASSERT(new_capacity && (new_capacity > m_capacity));

Expand Down
8 changes: 8 additions & 0 deletions inc/crn_decomp.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <stdio.h>
#ifdef WIN32
#include <memory.h>
#elif defined(__APPLE__)
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
Expand Down Expand Up @@ -1925,6 +1927,8 @@ static void* crnd_default_realloc(void* p, size_t size, size_t* pActual_size, bo
if (pActual_size) {
#ifdef WIN32
*pActual_size = p_new ? ::_msize(p_new) : 0;
#elif defined(__APPLE__)
*pActual_size = p_new ? ::malloc_size(p_new) : 0;
#else
*pActual_size = p_new ? malloc_usable_size(p_new) : 0;
#endif
Expand Down Expand Up @@ -1955,6 +1959,8 @@ static void* crnd_default_realloc(void* p, size_t size, size_t* pActual_size, bo
if (pActual_size) {
#ifdef WIN32
*pActual_size = ::_msize(p_final_block);
#elif defined(__APPLE__)
*pActual_size = ::malloc_size(p_final_block);
#else
*pActual_size = ::malloc_usable_size(p_final_block);
#endif
Expand All @@ -1968,6 +1974,8 @@ static size_t crnd_default_msize(void* p, void* pUser_data) {
pUser_data;
#ifdef WIN32
return p ? _msize(p) : 0;
#elif defined(__APPLE__)
return p ? malloc_size(p) : 0;
#else
return p ? malloc_usable_size(p) : 0;
#endif
Expand Down