simplify gitignore
[vg.git] / src / vg / vg_platform.h
index d7c0a0cff6ee13d0a08ff5ef1e09bcbef45ba449..c81d3474600e048f2117c73c406471ef0ba678ba 100644 (file)
@@ -1,7 +1,8 @@
 #ifndef VG_PLATFORM_H
 #define VG_PLATFORM_H
 
-#include "vg.h"
+//#include "vg.h"
+#include "vg_stdint.h"
 
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
@@ -28,83 +29,99 @@ struct vg_achievement
        const char *name;
 };
 
+#ifndef VG_STATIC
+#define VG_STATIC static
+#endif
+
 #define vg_static_assert _Static_assert
 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
+#define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
 
-#ifdef _WIN32
+#ifdef _WIN32_NO
        #include <windows.h>
 
-/* TODO */
+#ifdef I_THINK_THIS_IS_WHAT_MSCV_WANTS_BUT_HAVNT_TESTED_IT_YET
 
-#else
-       #include <pthread.h>
-   #include <semaphore.h>
+   #define VG_DEPRECATED         __declspec(deprecated)
+   #define VG_THREAD_LOCAL       __declspec( thread )
 
-   typedef sem_t                 vg_semaphore;
-   typedef pthread_mutex_t       vg_mutex;
+#else /* MINGW-64 */
 
-static int vg_semaphore_init( vg_semaphore *sem, u32 value )
-{
-   if( !sem_init( sem, 0, value ) )
-      return 1;
-   else
-      return 0;
-}
+   #define VG_THREAD_LOCAL       __thread
+   #define VG_DEPRECATED         __attribute__((deprecated))
 
-static int vg_semaphore_wait( vg_semaphore *sem )
-{
-   if( !sem_wait( sem ) )
-      return 1;
-   else
-      return 0;
-}
+#endif
 
-static int vg_semaphore_post( vg_semaphore *sem )
-{
-   if( !sem_post( sem ) )
-      return 1;
-   else
-      return 0;
-}
+   typedef HANDLE                vg_semaphore;
+   typedef HANDLE                vg_mutex;
+   typedef u64                   vg_timespec;
 
-static void vg_semaphore_free( vg_semaphore *sem )
-{
-   sem_destroy( sem );
-}
+#else
+   #include <pthread.h>
+   #include <semaphore.h>
 
-static int vg_mutex_init( vg_mutex *mutex )
-{
-   memset( mutex, 0, sizeof(vg_mutex) );
-   return 1;
-}
+   #define VG_DEPRECATED         __attribute__((deprecated))
+   #define VG_THREAD_LOCAL       __thread
 
-static int vg_mutex_lock( vg_mutex *mutex )
-{
-   if( !pthread_mutex_lock( mutex ) )
-      return 1;
-   else
-      return 0;
-}
+   typedef sem_t                 vg_semaphore;
+   typedef pthread_mutex_t       vg_mutex;
+   typedef struct timespec       vg_timespec;
 
-static int vg_mutex_unlock( vg_mutex *mutex )
-{
-   if( !pthread_mutex_unlock( mutex ) )
-      return 1;
-   else
-      return 0;
-}
+#endif
 
-static void vg_mutex_free( vg_mutex *mutex )
+VG_STATIC void vg_strncpy( const char *src, char *dst, u32 len )
 {
+   for( u32 i=0; i<len; i++ )
+   {
+      dst[i] = src[i];
 
+      if( !src[i] )
+         break;
+   }
 }
 
+#define VG_REQUIRED_ASSET( TYPE, DECL, FN, PATH, ... )                         \
+   TYPE DECL = FN( PATH,##__VA_ARGS__ );                                       \
+   vg_required( DECL, "Resource is required but failed to load: '" PATH "'" );
+
+#if 0
+VG_DEPRECATED
+char *strcpy(char* destination, const char* source);
+VG_DEPRECATED
+char *strncpy(char *restrict dest, const char *restrict src, size_t n);
+VG_DEPRECATED
+char *strcat(char *restrict dest, const char *restrict src);
+VG_DEPRECATED
+char *strncat(char *restrict dest, const char *restrict src, size_t n);
 #endif
 
-
-int vg_thread_run( void *pfunc, void *data )
+#include <stdio.h>
+#include <dirent.h>
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <math.h>
+#include <assert.h>
+
+VG_STATIC int     vg_thread_run( void *pfunc, void *data );
+VG_STATIC void    vg_thread_exit(void);
+VG_STATIC void    vg_set_thread_name( const char *name );
+VG_STATIC int     vg_semaphore_init( vg_semaphore *sem, u32 value );
+VG_STATIC int     vg_semaphore_trywait( vg_semaphore *sem );
+VG_STATIC int     vg_semaphore_wait( vg_semaphore *sem );
+VG_STATIC int     vg_semaphore_post( vg_semaphore *sem );
+VG_STATIC void    vg_semaphore_free( vg_semaphore *sem );
+VG_STATIC int     vg_mutex_init( vg_mutex *mutex );
+VG_STATIC int     vg_mutex_lock( vg_mutex *mutex );
+VG_STATIC int     vg_mutex_unlock( vg_mutex *mutex );
+VG_STATIC void    vg_mutex_free( vg_mutex *mutex );
+VG_STATIC void    vg_sleep_ms( long msec );
+VG_STATIC double  vg_time_diff( vg_timespec start, vg_timespec end );
+
+#ifdef _WIN32_NO
+
+VG_STATIC int vg_thread_run( void *pfunc, void *data )
 {
-#ifdef _WIN32
        HANDLE hThread = CreateThread
        (
                NULL,    /* Thread attributes */
@@ -128,7 +145,34 @@ int vg_thread_run( void *pfunc, void *data )
                CloseHandle( hThread );
                return 0;
        }
+}
+
+VG_STATIC void vg_thread_exit(void)
+{
+   ExitThread(0);
+}
+
+VG_STATIC void vg_set_thread_name( const char *name )
+{
+   /* I believe this is a meaningless concept in windows */
+}
+
+VG_STATIC int     vg_semaphore_init( vg_semaphore *sem, u32 value );
+VG_STATIC int     vg_semaphore_trywait( vg_semaphore *sem );
+VG_STATIC int     vg_semaphore_wait( vg_semaphore *sem );
+VG_STATIC int     vg_semaphore_post( vg_semaphore *sem );
+VG_STATIC void    vg_semaphore_free( vg_semaphore *sem );
+VG_STATIC int     vg_mutex_init( vg_mutex *mutex );
+VG_STATIC int     vg_mutex_lock( vg_mutex *mutex );
+VG_STATIC int     vg_mutex_unlock( vg_mutex *mutex );
+VG_STATIC void    vg_mutex_free( vg_mutex *mutex );
+VG_STATIC void    vg_sleep_ms( long msec );
+VG_STATIC double  vg_time_diff( vg_timespec start, vg_timespec end );
+
 #else
+
+VG_STATIC int vg_thread_run( void *pfunc, void *data )
+{
        pthread_t hThread;
        if( pthread_create( &hThread, NULL, pfunc, data ) )
        {
@@ -139,10 +183,75 @@ int vg_thread_run( void *pfunc, void *data )
                pthread_detach( hThread );
                return 0;
        }
+}
+
+
+VG_STATIC void vg_thread_exit(void)
+{
+   pthread_exit(NULL);
+}
+
+VG_STATIC void vg_set_thread_name( const char *name )
+{
+   /* not defined but links?? */
+#if 0
+   pthread_setname_np(pthread_self());
 #endif
 }
 
-static void vg_sleep_ms( long msec )
+VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value )
+{
+   return !sem_init( sem, 0, value );
+}
+
+VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem )
+{
+   return !sem_trywait( sem );
+}
+
+VG_STATIC int vg_semaphore_wait( vg_semaphore *sem )
+{
+   return !sem_wait( sem );
+}
+
+VG_STATIC int vg_semaphore_post( vg_semaphore *sem )
+{
+   return !sem_post( sem );
+}
+
+VG_STATIC void vg_semaphore_free( vg_semaphore *sem )
+{
+   sem_destroy( sem );
+}
+
+VG_STATIC int vg_mutex_init( vg_mutex *mutex )
+{
+   memset( mutex, 0, sizeof(vg_mutex) );
+   return 1;
+}
+
+VG_STATIC int vg_mutex_lock( vg_mutex *mutex )
+{
+   if( !pthread_mutex_lock( mutex ) )
+      return 1;
+   else
+      return 0;
+}
+
+VG_STATIC int vg_mutex_unlock( vg_mutex *mutex )
+{
+   if( !pthread_mutex_unlock( mutex ) )
+      return 1;
+   else
+      return 0;
+}
+
+VG_STATIC void vg_mutex_free( vg_mutex *mutex )
+{
+   
+}
+
+VG_STATIC void vg_sleep_ms( long msec )
 {
     struct timespec ts;
 
@@ -152,7 +261,7 @@ static void vg_sleep_ms( long msec )
 }
 
 /* diff two timespecs in MS */
-static double vg_time_diff( struct timespec start, struct timespec end )
+VG_STATIC double vg_time_diff( struct timespec start, struct timespec end )
 {
    double elapsed = 1000.0*end.tv_sec + 1e-6*end.tv_nsec
                        - (1000.0*start.tv_sec + 1e-6*start.tv_nsec);
@@ -160,26 +269,9 @@ static double vg_time_diff( struct timespec start, struct timespec end )
    return elapsed;
 }
 
+#endif
+
 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
 
-static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, 
-      size_t emsize )
-{
-   if( count+amount > *cap )
-   {
-      *cap = VG_MAX( (*cap)*2, (*cap)+amount );
-      
-      return realloc( buffer, (*cap) * emsize );
-   }
-
-   return buffer;
-}
-
-static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize )
-{
-   *cap = count;
-   return realloc( buffer, (*cap) * emsize );
-}
-
 #endif