Fix major overstep with last commit
[vg.git] / src / vg / vg_platform.h
index d7c0a0cff6ee13d0a08ff5ef1e09bcbef45ba449..ab9c927b79a1582fb383235cf648ee552aa6754b 100644 (file)
@@ -2,6 +2,7 @@
 #define VG_PLATFORM_H
 
 #include "vg.h"
+#include "vg_stdint.h"
 
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
@@ -30,41 +31,122 @@ struct vg_achievement
 
 #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
        #include <windows.h>
 
 /* TODO */
+#define VG_DEPRECATED __declspec(deprecated)
 
 #else
        #include <pthread.h>
    #include <semaphore.h>
 
+#define VG_DEPRECATED __attribute__((deprecated))
+
+   #define VG_THREAD_LOCAL       __thread
+
    typedef sem_t                 vg_semaphore;
    typedef pthread_mutex_t       vg_mutex;
 
+#endif
+
+#include <stdlib.h>
+
+/* TODO: If there is no graphics, we dont need to do an exit loop */
+
+static void vg_fatal_exit_loop( const char *error );
+static void *vg_alloc( size_t size )
+{
+   void *ptr = malloc( size );
+   
+   if( !ptr )
+      vg_fatal_exit_loop( "Out of memory" );
+
+   return ptr;
+}
+
+static void *vg_realloc( void *orig, size_t size )
+{
+   void *ptr = realloc( orig, size );
+
+   if( !ptr )
+      vg_fatal_exit_loop( "Out of memory" );
+
+   return ptr;
+}
+
+/* seems to be a GCC bug when inlining this, its low priority anyway */
+__attribute__ ((noinline))
+static void vg_free( void *ptr )
+{
+   free( ptr );
+}
+
+static void vg_required( void *ptr, const char *path )
+{
+   if( !ptr )
+   {
+      vg_fatal_exit_loop( path );
+   }
+}
+
+#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 "'" );
+
+VG_DEPRECATED
+void *malloc( size_t size );
+
+VG_DEPRECATED
+void *realloc( void *orig, size_t size );
+
+VG_DEPRECATED
+void free( void *ptr );
+
+#include <stdio.h>
+#include <dirent.h>
+#include <string.h>
+#include <stdarg.h>
+#include <ctype.h>
+#include <math.h>
+#include <assert.h>
+
+#ifdef _WIN32
+#else
+
+static void vg_thread_exit(void)
+{
+   pthread_exit(NULL);
+}
+
+static void vg_set_thread_name( const char *name )
+{
+   /* not defined but links?? */
+#if 0
+   pthread_setname_np(pthread_self());
+#endif
+}
+
 static int vg_semaphore_init( vg_semaphore *sem, u32 value )
 {
-   if( !sem_init( sem, 0, value ) )
-      return 1;
-   else
-      return 0;
+   return !sem_init( sem, 0, value );
+}
+
+static int vg_semaphore_trywait( vg_semaphore *sem )
+{
+   return !sem_trywait( sem );
 }
 
 static int vg_semaphore_wait( vg_semaphore *sem )
 {
-   if( !sem_wait( sem ) )
-      return 1;
-   else
-      return 0;
+   return !sem_wait( sem );
 }
 
 static int vg_semaphore_post( vg_semaphore *sem )
 {
-   if( !sem_post( sem ) )
-      return 1;
-   else
-      return 0;
+   return !sem_post( sem );
 }
 
 static void vg_semaphore_free( vg_semaphore *sem )
@@ -96,7 +178,7 @@ static int vg_mutex_unlock( vg_mutex *mutex )
 
 static void vg_mutex_free( vg_mutex *mutex )
 {
-
+   
 }
 
 #endif
@@ -169,8 +251,7 @@ static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
    if( count+amount > *cap )
    {
       *cap = VG_MAX( (*cap)*2, (*cap)+amount );
-      
-      return realloc( buffer, (*cap) * emsize );
+      return vg_realloc( buffer, (*cap) * emsize );
    }
 
    return buffer;
@@ -179,7 +260,7 @@ static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount,
 static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize )
 {
    *cap = count;
-   return realloc( buffer, (*cap) * emsize );
+   return vg_realloc( buffer, (*cap) * emsize );
 }
 
 #endif