medium sized dollop
[vg.git] / src / vg / vg_platform.h
index be9a225bf8fc56f97d3481f7f1a5bf5f39ff529a..d7c0a0cff6ee13d0a08ff5ef1e09bcbef45ba449 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef VG_PLATFORM_H
 #define VG_PLATFORM_H
 
+#include "vg.h"
+
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
 typedef unsigned int uint;
@@ -29,36 +31,74 @@ struct vg_achievement
 #define vg_static_assert _Static_assert
 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
 
-/* Pthred emulation for windows */
 #ifdef _WIN32
        #include <windows.h>
-       #define MUTEX_TYPE             HANDLE
-       #define MUTEX_INITIALIZER      NULL
-       #define MUTEX_SETUP(x)         (x) = CreateMutex(NULL, FALSE, NULL)
-
-   /* TODO: Why is this defined but never used? */
-       #define MUTEX_CLEANUP(x)       (CloseHandle(x)) 
-       #define MUTEX_LOCK(x)          emulate_pthread_mutex_lock(&(x))
-       #define MUTEX_UNLOCK(x)        (ReleaseMutex(x))
-
-       int emulate_pthread_mutex_lock( volatile MUTEX_TYPE *mx )
-       { 
-               if( *mx == NULL ) /* static initializer? */
-               { 
-                       HANDLE p = CreateMutex( NULL, FALSE, NULL );
-                       if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL ) 
-               != NULL )
-                               CloseHandle(p);
-               }
-               
-               return WaitForSingleObject( *mx, INFINITE ) == WAIT_FAILED;
-       }
+
+/* TODO */
+
 #else
        #include <pthread.h>
-       #define MUTEX_LOCK(x)                   pthread_mutex_lock(&(x))
-       #define MUTEX_UNLOCK(x)                 pthread_mutex_unlock(&(x))
-       #define MUTEX_TYPE                              pthread_mutex_t
-       #define MUTEX_INITIALIZER               {0}
+   #include <semaphore.h>
+
+   typedef sem_t                 vg_semaphore;
+   typedef pthread_mutex_t       vg_mutex;
+
+static int vg_semaphore_init( vg_semaphore *sem, u32 value )
+{
+   if( !sem_init( sem, 0, value ) )
+      return 1;
+   else
+      return 0;
+}
+
+static int vg_semaphore_wait( vg_semaphore *sem )
+{
+   if( !sem_wait( sem ) )
+      return 1;
+   else
+      return 0;
+}
+
+static int vg_semaphore_post( vg_semaphore *sem )
+{
+   if( !sem_post( sem ) )
+      return 1;
+   else
+      return 0;
+}
+
+static void vg_semaphore_free( vg_semaphore *sem )
+{
+   sem_destroy( sem );
+}
+
+static int vg_mutex_init( vg_mutex *mutex )
+{
+   memset( mutex, 0, sizeof(vg_mutex) );
+   return 1;
+}
+
+static int vg_mutex_lock( vg_mutex *mutex )
+{
+   if( !pthread_mutex_lock( mutex ) )
+      return 1;
+   else
+      return 0;
+}
+
+static int vg_mutex_unlock( vg_mutex *mutex )
+{
+   if( !pthread_mutex_unlock( mutex ) )
+      return 1;
+   else
+      return 0;
+}
+
+static void vg_mutex_free( vg_mutex *mutex )
+{
+
+}
+
 #endif
 
 
@@ -120,4 +160,26 @@ static double vg_time_diff( struct timespec start, struct timespec end )
    return elapsed;
 }
 
+#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