Fixed Windows (updated gb.h) | Need to test on MacOS and Linux now!

This commit is contained in:
Zachary Pierson
2017-02-11 23:52:56 -06:00
parent c50aabd916
commit 7829421085
2 changed files with 43 additions and 41 deletions
+3 -3
View File
@@ -1,3 +1,3 @@
#include "os_windows.odin" when ODIN_OS == "windows";
#include "os_x.odin" when ODIN_OS == "osx";
#include "os_linux.odin" when ODIN_OS == "linux";
#load "os_windows.odin" when ODIN_OS == "windows";
#load "os_x.odin" when ODIN_OS == "osx";
#load "os_linux.odin" when ODIN_OS == "linux";
+40 -38
View File
@@ -1,4 +1,4 @@
/* gb.h - v0.26d - Ginger Bill's C Helper Library - public domain
/* gb.h - v0.27 - Ginger Bill's C Helper Library - public domain
- no warranty implied; use at your own risk
This is a single header file with a bunch of useful stuff
@@ -277,6 +277,8 @@ extern "C" {
#include <stdarg.h>
#include <stddef.h>
#if defined(GB_SYSTEM_WINDOWS)
#if !defined(GB_NO_WINDOWS_H)
#define NOMINMAX 1
@@ -297,6 +299,9 @@ extern "C" {
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#ifndef _IOSC11_SOURCE
#define _IOSC11_SOURCE
#endif
#include <stdlib.h> // NOTE(bill): malloc on linux
#include <sys/mman.h>
#if !defined(GB_SYSTEM_OSX)
@@ -310,18 +315,18 @@ extern "C" {
#endif
#if defined(GB_SYSTEM_OSX)
#include <mach/mach.h>
#include <mach/mach_init.h>
#include <mach/mach_time.h>
#include <mach/thread_act.h>
#include <mach/thread_policy.h>
#include <sys/sysctl.h>
#include <copyfile.h>
#include <mach/clock.h>
#include <mach/mach.h>
#include <mach/mach_init.h>
#include <mach/mach_time.h>
#include <mach/thread_act.h>
#include <mach/thread_policy.h>
#include <sys/sysctl.h>
#include <copyfile.h>
#include <mach/clock.h>
#endif
#if defined(GB_SYSTEM_UNIX)
#include <semaphore.h>
#include <semaphore.h>
#endif
@@ -413,21 +418,20 @@ typedef i32 Rune; // NOTE(bill): Unicode codepoint
#define GB_RUNE_EOF cast(Rune)(-1)
// NOTE(bill): I think C99 and C++ `bool` is stupid for numerous reasons but there are too many
// to write in this small comment.
typedef i8 b8;
typedef i16 b16;
typedef i32 b32; // NOTE(bill): Prefer this!!!
// NOTE(bill): Get true and false
#if !defined(__cplusplus)
#if (defined(_MSC_VER) && _MSC_VER <= 1800) || !defined(__STDC_VERSION__)
#if (defined(_MSC_VER) && _MSC_VER <= 1800) || (!defined(_MSC_VER) && !defined(__STDC_VERSION__))
#ifndef true
#define true (0 == 0)
#endif
#ifndef false
#define false (0 != 0)
#endif
typedef b8 bool;
#else
#include <stdbool.h>
#endif
@@ -616,7 +620,7 @@ extern "C++" {
//
// NOTE: C++11 (and above) only!
//
#if defined(__cplusplus) && ((defined(_MSC_VER) && _MSC_VER >= 1400) || (__cplusplus >= 201103L))
#if !defined(GB_NO_DEFER) && defined(__cplusplus) && ((defined(_MSC_VER) && _MSC_VER >= 1400) || (__cplusplus >= 201103L))
extern "C++" {
// NOTE(bill): Stupid fucking templates
template <typename T> struct gbRemoveReference { typedef T Type; };
@@ -3628,15 +3632,7 @@ gb_inline void *gb_memcopy(void *dest, void const *source, isize n) {
#if defined(_MSC_VER)
// TODO(bill): Is this good enough?
__movsb(cast(u8 *)dest, cast(u8 *)source, n);
#elif defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_UNIX)
// NOTE(zangent): I assume there's a reason this isn't being used elsewhere,
// but casting pointers as arguments to an __asm__ call is considered an
// error on MacOS and (I think) Linux
// TODO(zangent): Figure out how to refactor the asm code so it works on MacOS,
// since this is probably not the way the author intended this to work.
memcpy(dest, source, n);
#elif defined(GB_CPU_X86)
__asm__ __volatile__("rep movsb" : "+D"(cast(u8 *)dest), "+S"(cast(u8 *)source), "+c"(n) : : "memory");
#else
u8 *d = cast(u8 *)dest;
@@ -4127,7 +4123,7 @@ gb_inline i64 gb_atomic64_fetch_and(gbAtomic64 volatile *a, i64 operand) {
gb_inline i64 gb_atomic64_fetch_or(gbAtomic64 volatile *a, i64 operand) {
#if defined(GB_ARCH_64_BIT)
return _InterlockedAnd64(cast(i64 volatile *)a, operand);
return _InterlockedOr64(cast(i64 volatile *)a, operand);
#elif GB_CPU_X86
i64 expected = a->value;
for (;;) {
@@ -4680,8 +4676,7 @@ gb_inline u32 gb_thread_current_id(void) {
#else
thread_id = GetCurrentThreadId();
#endif
#elif defined(GB_SYSTEM_LINUX)
thread_id = pthread_self();
#elif defined(GB_SYSTEM_OSX) && defined(GB_ARCH_64_BIT)
thread_id = pthread_mach_thread_np(pthread_self());
#elif defined(GB_ARCH_32_BIT) && defined(GB_CPU_X86)
@@ -4835,18 +4830,15 @@ GB_ALLOCATOR_PROC(gb_heap_allocator_proc) {
#else
// TODO(bill): *nix version that's decent
case gbAllocation_Alloc: {
gbAllocationHeader *header;
isize total_size = size + alignment + gb_size_of(gbAllocationHeader);
ptr = malloc(total_size);
header = cast(gbAllocationHeader *)ptr;
ptr = gb_align_forward(header+1, alignment);
gb_allocation_header_fill(header, ptr, size);
if (flags & gbAllocatorFlag_ClearToZero)
posix_memalign(&ptr, alignment, size);
if (flags & gbAllocatorFlag_ClearToZero) {
gb_zero_size(ptr, size);
}
} break;
case gbAllocation_Free: {
free(gb_allocation_header(old_memory));
free(old_memory);
} break;
case gbAllocation_Resize: {
@@ -4943,7 +4935,7 @@ isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
void gb_affinity_init(gbAffinity *a) {
usize count, count_size = gb_size_of(count);
a->is_accurate = false;
a->is_accurate = false;
a->thread_count = 1;
a->core_count = 1;
a->threads_per_core = 1;
@@ -4974,7 +4966,6 @@ void gb_affinity_destroy(gbAffinity *a) {
b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
isize index;
thread_t thread;
GB_ASSERT(thread < a->thread_count);
thread_affinity_policy_data_t info;
kern_return_t result;
@@ -5049,6 +5040,7 @@ void gb_affinity_init(gbAffinity *a) {
}
#undef AF__CHECK
}
fclose(cpu_info);
}
@@ -5059,7 +5051,6 @@ void gb_affinity_init(gbAffinity *a) {
a->threads_per_core = threads;
a->thread_count = a->threads_per_core * a->core_count;
a->is_accurate = accurate;
}
@@ -5073,7 +5064,6 @@ b32 gb_affinity_set(gbAffinity *a, isize core, isize thread_index) {
}
isize gb_affinity_thread_count_for_core(gbAffinity *a, isize core) {
GB_ASSERT(0 <= core && core < a->core_count);
return a->threads_per_core;
}
@@ -7857,7 +7847,18 @@ char *gb_path_get_full_name(gbAllocator a, char const *path) {
return gb_alloc_str_len(a, buf, len+1);
#else
// TODO(bill): Make work on *nix, etc.
return gb_alloc_str_len(a, path, gb_strlen(path));
char* p = realpath(path, 0);
GB_ASSERT(p && "file does not exist");
isize len = gb_strlen(p);
// bill... gb_alloc_str_len refused to work for this...
char* ret = gb_alloc(a, sizeof(char) * len + 1);
gb_memmove(ret, p, len);
ret[len] = 0;
free(p);
return ret;
#endif
}
@@ -10471,3 +10472,4 @@ GB_COMPARE_PROC(gb_video_mode_dsc_cmp) {
#endif
#endif // GB_IMPLEMENTATION