Skip to content

Xcode compatibility #19

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

Open
wants to merge 4 commits into
base: master
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
4 changes: 4 additions & 0 deletions crnlib/crn_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@

#define CRNLIB_RESTRICT

#ifdef __APPLE__
#define CRNLIB_FORCE_INLINE inline __attribute__((__always_inline__))
#else
#define CRNLIB_FORCE_INLINE inline __attribute__((__always_inline__,__gnu_inline__))
#endif

#define CRNLIB_INT64_FORMAT_SPECIFIER "%lli"
#define CRNLIB_UINT64_FORMAT_SPECIFIER "%llu"
Expand Down
6 changes: 3 additions & 3 deletions crnlib/crn_dynamic_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,10 @@ namespace crnlib
return *this;
}

#ifdef CRNLIB_BUILD_DEBUG
void dynamic_string::check() const
{
if (!m_pStr)
#ifdef CRNLIB_BUILD_DEBUG
if (!m_pStr)
{
CRNLIB_ASSERT(!m_buf_size && !m_len);
}
Expand All @@ -550,8 +550,8 @@ namespace crnlib
CRNLIB_ASSERT(strlen(m_pStr) == m_len);
#endif
}
}
#endif
}

bool dynamic_string::ensure_buf(uint len, bool preserve_contents)
{
Expand Down
4 changes: 0 additions & 4 deletions crnlib/crn_dynamic_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ namespace crnlib
uint16 m_len;
char* m_pStr;

#ifdef CRNLIB_BUILD_DEBUG
void check() const;
#else
inline void check() const { }
#endif

bool expand_buf(uint new_buf_size, bool preserve_contents);

Expand Down
5 changes: 5 additions & 0 deletions crnlib/crn_jpge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

#include <stdlib.h>
#include <string.h>

#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif

#include "crn_core.h"

Expand Down
8 changes: 8 additions & 0 deletions crnlib/crn_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
#include "crn_core.h"
#include "crn_console.h"
#include "../inc/crnlib.h"
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif

#define CRNLIB_MEM_STATS 0

#if !CRNLIB_USE_WIN32_API
#ifdef __APPLE__
#define _msize malloc_size
#else
#define _msize malloc_usable_size
#endif
#endif

namespace crnlib
{
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 @@ void crnlib_fail(const char* pExp, const char* pFile, unsigned line);

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
65 changes: 64 additions & 1 deletion crnlib/crn_threading_pthreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
#endif

#ifdef __GNUC__
#ifdef __APPLE__
#include <unistd.h>
#else
#include <sys/sysinfo.h>
#endif
#endif

#ifdef WIN32
#include <process.h>
Expand All @@ -29,6 +33,8 @@ namespace crnlib
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 @@ -38,8 +44,14 @@ namespace crnlib

crn_thread_id_t crn_get_current_thread_id()
{
#ifdef __APPLE__
__uint64_t thread_id = 0;
pthread_threadid_np(pthread_self(), &thread_id);
return thread_id;
#else
// FIXME: Not portable
return static_cast<crn_thread_id_t>(pthread_self());
#endif
}

void crn_sleep(unsigned int milliseconds)
Expand Down Expand Up @@ -106,17 +118,31 @@ namespace crnlib

semaphore::semaphore(long initialCount, long maximumCount, const char* pName)
{
maximumCount, pName;
CRNLIB_ASSERT(maximumCount >= initialCount);
#ifdef __APPLE__
m_name = (pName != NULL ? pName : "crnlib");
m_sem = sem_open(m_name.c_str(), O_CREAT, S_IRWXU, initialCount);

if(m_sem == SEM_FAILED)
{
CRNLIB_FAIL("semaphore: sem_open() failed");
}
#else
maximumCount, pName;
if (sem_init(&m_sem, 0, initialCount))
{
CRNLIB_FAIL("semaphore: sem_init() failed");
}
#endif
}

semaphore::~semaphore()
{
#ifdef __APPLE__
sem_unlink(m_name.c_str());
#else
sem_destroy(&m_sem);
#endif
}

void semaphore::release(long releaseCount)
Expand All @@ -132,7 +158,11 @@ namespace crnlib
#else
while (releaseCount > 0)
{
#ifdef __APPLE__
status = sem_post(m_sem);
#else
status = sem_post(&m_sem);
#endif
if (status)
break;
releaseCount--;
Expand All @@ -157,7 +187,11 @@ namespace crnlib
#else
while (releaseCount > 0)
{
#ifdef __APPLE__
sem_post(m_sem);
#else
sem_post(&m_sem);
#endif
releaseCount--;
}
#endif
Expand All @@ -168,22 +202,37 @@ namespace crnlib
int status;
if (milliseconds == cUINT32_MAX)
{
#ifdef __APPLE__
status = sem_wait(m_sem);
#else
status = sem_wait(&m_sem);
#endif
}
else
{
#ifdef __APPLE__
status = sem_trywait(m_sem);
#else
struct timespec interval;
interval.tv_sec = milliseconds / 1000;
interval.tv_nsec = (milliseconds % 1000) * 1000000L;
status = sem_timedwait(&m_sem, &interval);
#endif
}

if (status)
{
#ifdef __APPLE__
if(errno != EAGAIN)
{
CRNLIB_FAIL("semaphore: sem_wait() or sem_trywait() failed");
}
#else
if (errno != ETIMEDOUT)
{
CRNLIB_FAIL("semaphore: sem_wait() or sem_timedwait() failed");
}
#endif
return false;
}

Expand All @@ -192,31 +241,45 @@ namespace crnlib

spinlock::spinlock()
{
#ifdef __APPLE__
m_spinlock = OS_SPINLOCK_INIT;
#else
if (pthread_spin_init(&m_spinlock, 0))
{
CRNLIB_FAIL("spinlock: pthread_spin_init() failed");
}
#endif
}

spinlock::~spinlock()
{
#ifndef __APPLE__
pthread_spin_destroy(&m_spinlock);
#endif
}

void spinlock::lock()
{
#ifdef __APPLE__
OSSpinLockLock(&m_spinlock);
#else
if (pthread_spin_lock(&m_spinlock))
{
CRNLIB_FAIL("spinlock: pthread_spin_lock() failed");
}
#endif
}

void spinlock::unlock()
{
#ifdef __APPLE__
OSSpinLockUnlock(&m_spinlock);
#else
if (pthread_spin_unlock(&m_spinlock))
{
CRNLIB_FAIL("spinlock: pthread_spin_unlock() failed");
}
#endif
}

task_pool::task_pool() :
Expand Down
13 changes: 13 additions & 0 deletions 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>

#ifdef __APPLE__
#include <libkern/OSAtomic.h>
#endif

namespace crnlib
{
// g_number_of_processors defaults to 1. Will be higher on multicore machines.
Expand Down Expand Up @@ -74,7 +78,12 @@ namespace crnlib
bool wait(uint32 milliseconds = cUINT32_MAX);

private:
#ifdef __APPLE__
std::string m_name;
sem_t *m_sem;
#else
sem_t m_sem;
#endif
};

class spinlock
Expand All @@ -87,7 +96,11 @@ namespace crnlib
void unlock();

private:
#ifdef __APPLE__
OSSpinLock m_spinlock;
#else
pthread_spinlock_t m_spinlock;
#endif
};

class scoped_spinlock
Expand Down
4 changes: 4 additions & 0 deletions crnlib/crn_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ namespace crnlib
QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(pTicks));
}
#elif defined(__GNUC__)
#ifdef __APPLE__
#include <sys/time.h>
#else
#include <sys/timex.h>
#endif
inline void query_counter(timer_ticks *pTicks)
{
struct timeval cur_time;
Expand Down
4 changes: 2 additions & 2 deletions crnlib/crn_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace crnlib
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
28 changes: 28 additions & 0 deletions crnlib/crnlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@

#include "crn_rg_etc1.h"


inline bool crn_comp_params::check() const
{
if ( (m_file_type > cCRNFileTypeDDS) ||
(((int)m_quality_level < (int)cCRNMinQualityLevel) || ((int)m_quality_level > (int)cCRNMaxQualityLevel)) ||
(m_dxt1a_alpha_threshold > 255) ||
((m_faces != 1) && (m_faces != 6)) ||
((m_width < 1) || (m_width > cCRNMaxLevelResolution)) ||
((m_height < 1) || (m_height > cCRNMaxLevelResolution)) ||
((m_levels < 1) || (m_levels > cCRNMaxLevels)) ||
((m_format < cCRNFmtDXT1) || (m_format >= cCRNFmtTotal)) ||
((m_crn_color_endpoint_palette_size) && ((m_crn_color_endpoint_palette_size < cCRNMinPaletteSize) || (m_crn_color_endpoint_palette_size > cCRNMaxPaletteSize))) ||
((m_crn_color_selector_palette_size) && ((m_crn_color_selector_palette_size < cCRNMinPaletteSize) || (m_crn_color_selector_palette_size > cCRNMaxPaletteSize))) ||
((m_crn_alpha_endpoint_palette_size) && ((m_crn_alpha_endpoint_palette_size < cCRNMinPaletteSize) || (m_crn_alpha_endpoint_palette_size > cCRNMaxPaletteSize))) ||
((m_crn_alpha_selector_palette_size) && ((m_crn_alpha_selector_palette_size < cCRNMinPaletteSize) || (m_crn_alpha_selector_palette_size > cCRNMaxPaletteSize))) ||
(m_alpha_component > 3) ||
(m_num_helper_threads > cCRNMaxHelperThreads) ||
(m_dxt_quality > cCRNDXTQualityUber) ||
(m_dxt_compressor_type >= cCRNTotalDXTCompressors) )
{
return false;
}
return true;
}

inline bool crn_mipmap_params::check() const { return true; }


namespace crnlib
{
static void* realloc_func(void* p, size_t size, size_t* pActual_size, bool movable, void* pUser_data)
Expand Down
Loading