now thats a lot of damage!
[vg.git] / src / vg / vg_platform.h
index b71a01d71d9abe345b49c1365c67644d86d861dc..f1c5488e283774daf42e3ee0b1f6e358dcb0cc63 100644 (file)
@@ -29,6 +29,10 @@ 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))
@@ -65,7 +69,7 @@ struct vg_achievement
 
 #endif
 
-static void vg_strncpy( const char *src, char *dst, u32 len )
+VG_STATIC void vg_strncpy( const char *src, char *dst, u32 len )
 {
    for( u32 i=0; i<len; i++ )
    {
@@ -76,57 +80,8 @@ static void vg_strncpy( const char *src, char *dst, u32 len )
    }
 }
 
-#include <stdlib.h>
-
-#define VG_ZERO_NEW_MEM
-
-static void vg_fatal_exit_loop( const char *error );
-static void *vg_alloc( size_t size )
-{
-   if( size == 0 )
-      return NULL;
-
-   void *ptr = malloc( size );
-   
-   if( !ptr )
-   {
-      vg_fatal_exit_loop( "Out of memory" );
-   }
-
-#ifdef VG_ZERO_NEW_MEM
-   u8 *bytes = ptr;
-   for( u32 i=0; i<size; i++ )
-   {
-      bytes[i] = 0x00;
-   }
-#endif
-
-   return ptr;
-}
-
-static void *vg_realloc( void *orig, size_t size )
-{
-   void *ptr = realloc( orig, size );
-
-   if( !ptr )
-   {
-      if( size == 0 )
-         return NULL;
-
-      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 )
+VG_STATIC void vg_fatal_exit_loop( const char *error );
+VG_STATIC void vg_required( void *ptr, const char *path )
 {
    if( !ptr )
    {
@@ -149,15 +104,6 @@ VG_DEPRECATED
 char *strncat(char *restrict dest, const char *restrict src, size_t n);
 #endif
 
-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>
@@ -166,24 +112,24 @@ void free( void *ptr );
 #include <math.h>
 #include <assert.h>
 
-static int     vg_thread_run( void *pfunc, void *data );
-static void    vg_thread_exit(void);
-static void    vg_set_thread_name( const char *name );
-static int     vg_semaphore_init( vg_semaphore *sem, u32 value );
-static int     vg_semaphore_trywait( vg_semaphore *sem );
-static int     vg_semaphore_wait( vg_semaphore *sem );
-static int     vg_semaphore_post( vg_semaphore *sem );
-static void    vg_semaphore_free( vg_semaphore *sem );
-static int     vg_mutex_init( vg_mutex *mutex );
-static int     vg_mutex_lock( vg_mutex *mutex );
-static int     vg_mutex_unlock( vg_mutex *mutex );
-static void    vg_mutex_free( vg_mutex *mutex );
-static void    vg_sleep_ms( long msec );
-static double  vg_time_diff( vg_timespec start, vg_timespec end );
+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
 
-static int vg_thread_run( void *pfunc, void *data )
+VG_STATIC int vg_thread_run( void *pfunc, void *data )
 {
        HANDLE hThread = CreateThread
        (
@@ -210,31 +156,31 @@ static int vg_thread_run( void *pfunc, void *data )
        }
 }
 
-static void vg_thread_exit(void)
+VG_STATIC void vg_thread_exit(void)
 {
    ExitThread(0);
 }
 
-static void vg_set_thread_name( const char *name )
+VG_STATIC void vg_set_thread_name( const char *name )
 {
    /* I believe this is a meaningless concept in windows */
 }
 
-static int     vg_semaphore_init( vg_semaphore *sem, u32 value );
-static int     vg_semaphore_trywait( vg_semaphore *sem );
-static int     vg_semaphore_wait( vg_semaphore *sem );
-static int     vg_semaphore_post( vg_semaphore *sem );
-static void    vg_semaphore_free( vg_semaphore *sem );
-static int     vg_mutex_init( vg_mutex *mutex );
-static int     vg_mutex_lock( vg_mutex *mutex );
-static int     vg_mutex_unlock( vg_mutex *mutex );
-static void    vg_mutex_free( vg_mutex *mutex );
-static void    vg_sleep_ms( long msec );
-static double  vg_time_diff( vg_timespec start, vg_timespec end );
+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
 
-static int vg_thread_run( void *pfunc, void *data )
+VG_STATIC int vg_thread_run( void *pfunc, void *data )
 {
        pthread_t hThread;
        if( pthread_create( &hThread, NULL, pfunc, data ) )
@@ -249,12 +195,12 @@ static int vg_thread_run( void *pfunc, void *data )
 }
 
 
-static void vg_thread_exit(void)
+VG_STATIC void vg_thread_exit(void)
 {
    pthread_exit(NULL);
 }
 
-static void vg_set_thread_name( const char *name )
+VG_STATIC void vg_set_thread_name( const char *name )
 {
    /* not defined but links?? */
 #if 0
@@ -262,38 +208,38 @@ static void vg_set_thread_name( const char *name )
 #endif
 }
 
-static int vg_semaphore_init( vg_semaphore *sem, u32 value )
+VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value )
 {
    return !sem_init( sem, 0, value );
 }
 
-static int vg_semaphore_trywait( vg_semaphore *sem )
+VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem )
 {
    return !sem_trywait( sem );
 }
 
-static int vg_semaphore_wait( vg_semaphore *sem )
+VG_STATIC int vg_semaphore_wait( vg_semaphore *sem )
 {
    return !sem_wait( sem );
 }
 
-static int vg_semaphore_post( vg_semaphore *sem )
+VG_STATIC int vg_semaphore_post( vg_semaphore *sem )
 {
    return !sem_post( sem );
 }
 
-static void vg_semaphore_free( vg_semaphore *sem )
+VG_STATIC void vg_semaphore_free( vg_semaphore *sem )
 {
    sem_destroy( sem );
 }
 
-static int vg_mutex_init( vg_mutex *mutex )
+VG_STATIC int vg_mutex_init( vg_mutex *mutex )
 {
    memset( mutex, 0, sizeof(vg_mutex) );
    return 1;
 }
 
-static int vg_mutex_lock( vg_mutex *mutex )
+VG_STATIC int vg_mutex_lock( vg_mutex *mutex )
 {
    if( !pthread_mutex_lock( mutex ) )
       return 1;
@@ -301,7 +247,7 @@ static int vg_mutex_lock( vg_mutex *mutex )
       return 0;
 }
 
-static int vg_mutex_unlock( vg_mutex *mutex )
+VG_STATIC int vg_mutex_unlock( vg_mutex *mutex )
 {
    if( !pthread_mutex_unlock( mutex ) )
       return 1;
@@ -309,12 +255,12 @@ static int vg_mutex_unlock( vg_mutex *mutex )
       return 0;
 }
 
-static void vg_mutex_free( vg_mutex *mutex )
+VG_STATIC void vg_mutex_free( vg_mutex *mutex )
 {
    
 }
 
-static void vg_sleep_ms( long msec )
+VG_STATIC void vg_sleep_ms( long msec )
 {
     struct timespec ts;
 
@@ -324,7 +270,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);
@@ -337,22 +283,4 @@ static double vg_time_diff( struct timespec start, struct timespec end )
 #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 vg_realloc( buffer, (*cap) * emsize );
-   }
-
-   return buffer;
-}
-
-static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize )
-{
-   *cap = count;
-   return vg_realloc( buffer, (*cap) * emsize );
-}
-
 #endif