From a33bc47dd8971631a88907c3a4c21e1333527104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:34:16 +0200 Subject: [PATCH 1/7] Remove missing file from Makefile --- crnlib/Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/crnlib/Makefile b/crnlib/Makefile index 9a9de187..8d2dfcec 100644 --- a/crnlib/Makefile +++ b/crnlib/Makefile @@ -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 \ From 301eef5a52f3d787a2d1b58711182c27fe2ecd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:34:39 +0200 Subject: [PATCH 2/7] Use C++ compiler from the environment in Makefile --- crnlib/Makefile | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crnlib/Makefile b/crnlib/Makefile index 8d2dfcec..3e43e4bb 100644 --- a/crnlib/Makefile +++ b/crnlib/Makefile @@ -79,17 +79,16 @@ 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) From 3d7dd353a9f0b6d647da0f92626e6002f61fc2d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:34:52 +0200 Subject: [PATCH 3/7] Add clean step to Makefile --- crnlib/Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crnlib/Makefile b/crnlib/Makefile index 3e43e4bb..f0dffa58 100644 --- a/crnlib/Makefile +++ b/crnlib/Makefile @@ -92,3 +92,7 @@ corpus_test.o: ../crunch/corpus_test.cpp crunch: $(OBJECTS) crunch.o corpus_gen.o corpus_test.o $(CXX) $(OBJECTS) crunch.o corpus_gen.o corpus_test.o -o crunch $(LINKER_OPTIONS) + +clean: + rm -f *.o + rm -f ../crunch/*.o \ No newline at end of file From e912e06f244927e3602adf90c02b6018e9edfe52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:30:17 +0200 Subject: [PATCH 4/7] Fix compile ambiguity --- crnlib/crn_vector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crnlib/crn_vector.cpp b/crnlib/crn_vector.cpp index 82a7202b..38b22559 100644 --- a/crnlib/crn_vector.cpp +++ b/crnlib/crn_vector.cpp @@ -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)); From e8ef34b4dad5bb6761b72e4af7271e0a588b2b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:31:04 +0200 Subject: [PATCH 5/7] Include outside of namespace --- crnlib/crn_console.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crnlib/crn_console.h b/crnlib/crn_console.h index b6e7ab18..3580c1a3 100644 --- a/crnlib/crn_console.h +++ b/crnlib/crn_console.h @@ -6,6 +6,9 @@ #ifdef WIN32 #include #include +#elif defined(__GNUC__) +#include +#include #endif namespace crnlib { class dynamic_string; @@ -99,8 +102,6 @@ inline int crn_getch() { return _getch(); } #elif defined(__GNUC__) -#include -#include inline int crn_getch() { struct termios oldt, newt; int ch; From e4a212a68f6c190d0fe4b443d1ea4947f2854d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:33:40 +0200 Subject: [PATCH 6/7] Use macos malloc and file related functions --- crnlib/crn_jpge.cpp | 4 ++++ crnlib/crn_mem.cpp | 8 +++++++- crnlib/crn_platform.h | 2 +- inc/crn_decomp.h | 8 ++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crnlib/crn_jpge.cpp b/crnlib/crn_jpge.cpp index 2a9cb185..888b5c8d 100644 --- a/crnlib/crn_jpge.cpp +++ b/crnlib/crn_jpge.cpp @@ -12,7 +12,11 @@ #include #include +#if !defined(__APPLE__) #include +#else +#include +#endif #include "crn_core.h" diff --git a/crnlib/crn_mem.cpp b/crnlib/crn_mem.cpp index d32ea08b..75a9ffe4 100644 --- a/crnlib/crn_mem.cpp +++ b/crnlib/crn_mem.cpp @@ -3,14 +3,20 @@ #include "crn_core.h" #include "crn_console.h" #include "../inc/crnlib.h" +#if !defined(__APPLE__) #include +#else +#include +#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 diff --git a/crnlib/crn_platform.h b/crnlib/crn_platform.h index 0db51929..cec101e2 100644 --- a/crnlib/crn_platform.h +++ b/crnlib/crn_platform.h @@ -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 diff --git a/inc/crn_decomp.h b/inc/crn_decomp.h index 5775745b..d803e217 100644 --- a/inc/crn_decomp.h +++ b/inc/crn_decomp.h @@ -19,6 +19,8 @@ #include #ifdef WIN32 #include +#elif defined(__APPLE__) +#include #else #include #endif @@ -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 @@ -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 @@ -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 From 96c3db70ed422d325a5ef90e8b608b211bef24e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Mon, 25 Oct 2021 11:35:19 +0200 Subject: [PATCH 7/7] Make pthreads work on macos Instead of `pthread_spinlock_t` use `os_unfair_lock_t` because macos is missing the pthread spinlock. `sem_init` is deprecated on macos and always returns `-1`. Use `sem_open` together with `sem_unlink` to open an exclusive sempahore in global namespace. Use `sem_wait` because `sem_timedWait` is missing on macos. --- crnlib/crn_threading_pthreads.cpp | 73 +++++++++++++++++++++++++------ crnlib/crn_threading_pthreads.h | 13 +++++- 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/crnlib/crn_threading_pthreads.cpp b/crnlib/crn_threading_pthreads.cpp index 9b1b8128..144079a7 100644 --- a/crnlib/crn_threading_pthreads.cpp +++ b/crnlib/crn_threading_pthreads.cpp @@ -11,7 +11,7 @@ #include "crn_winhdr.h" #endif -#ifdef __GNUC__ +#if defined(__GNUC__) && !defined(__APPLE__) #include #endif @@ -19,6 +19,10 @@ #include #endif +#if defined(__APPLE__) +#include +#endif + namespace crnlib { uint g_number_of_processors = 1; @@ -27,6 +31,8 @@ void crn_threading_init() { SYSTEM_INFO g_system_info; GetSystemInfo(&g_system_info); g_number_of_processors = math::maximum(1U, g_system_info.dwNumberOfProcessors); +#elif defined(__APPLE__) + g_number_of_processors = math::maximum(1, sysconf(_SC_NPROCESSORS_ONLN)); #elif defined(__GNUC__) g_number_of_processors = math::maximum(1, get_nprocs()); #else @@ -36,7 +42,7 @@ void crn_threading_init() { crn_thread_id_t crn_get_current_thread_id() { // FIXME: Not portable - return static_cast(pthread_self()); + return (crn_thread_id_t)(pthread_self()); } void crn_sleep(unsigned int milliseconds) { @@ -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) { @@ -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--; @@ -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 @@ -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) { @@ -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() diff --git a/crnlib/crn_threading_pthreads.h b/crnlib/crn_threading_pthreads.h index 53855774..6e37ed93 100644 --- a/crnlib/crn_threading_pthreads.h +++ b/crnlib/crn_threading_pthreads.h @@ -14,6 +14,10 @@ #include #include +#if defined(__APPLE__) +#include +#endif + namespace crnlib { // g_number_of_processors defaults to 1. Will be higher on multicore machines. extern uint g_number_of_processors; @@ -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 { @@ -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 {