ab9c927b79a1582fb383235cf648ee552aa6754b
7 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
9 typedef unsigned int uint
;
24 typedef struct vg_tex2d vg_tex2d
;
32 #define vg_static_assert _Static_assert
33 #define vg_list_size( A ) (sizeof(A)/sizeof(A[0]))
34 #define VG_MUST_USE_RESULT __attribute__((warn_unused_result))
40 #define VG_DEPRECATED __declspec(deprecated)
44 #include <semaphore.h>
46 #define VG_DEPRECATED __attribute__((deprecated))
48 #define VG_THREAD_LOCAL __thread
50 typedef sem_t vg_semaphore
;
51 typedef pthread_mutex_t vg_mutex
;
57 /* TODO: If there is no graphics, we dont need to do an exit loop */
59 static void vg_fatal_exit_loop( const char *error
);
60 static void *vg_alloc( size_t size
)
62 void *ptr
= malloc( size
);
65 vg_fatal_exit_loop( "Out of memory" );
70 static void *vg_realloc( void *orig
, size_t size
)
72 void *ptr
= realloc( orig
, size
);
75 vg_fatal_exit_loop( "Out of memory" );
80 /* seems to be a GCC bug when inlining this, its low priority anyway */
81 __attribute__ ((noinline
))
82 static void vg_free( void *ptr
)
87 static void vg_required( void *ptr
, const char *path
)
91 vg_fatal_exit_loop( path
);
95 #define VG_REQUIRED_ASSET( TYPE, DECL, FN, PATH, ... ) \
96 TYPE DECL = FN( PATH,##__VA_ARGS__ ); \
97 vg_required( DECL, "Resource is required but failed to load: '" PATH "'" );
100 void *malloc( size_t size
);
103 void *realloc( void *orig
, size_t size
);
106 void free( void *ptr
);
119 static void vg_thread_exit(void)
124 static void vg_set_thread_name( const char *name
)
126 /* not defined but links?? */
128 pthread_setname_np(pthread_self());
132 static int vg_semaphore_init( vg_semaphore
*sem
, u32 value
)
134 return !sem_init( sem
, 0, value
);
137 static int vg_semaphore_trywait( vg_semaphore
*sem
)
139 return !sem_trywait( sem
);
142 static int vg_semaphore_wait( vg_semaphore
*sem
)
144 return !sem_wait( sem
);
147 static int vg_semaphore_post( vg_semaphore
*sem
)
149 return !sem_post( sem
);
152 static void vg_semaphore_free( vg_semaphore
*sem
)
157 static int vg_mutex_init( vg_mutex
*mutex
)
159 memset( mutex
, 0, sizeof(vg_mutex
) );
163 static int vg_mutex_lock( vg_mutex
*mutex
)
165 if( !pthread_mutex_lock( mutex
) )
171 static int vg_mutex_unlock( vg_mutex
*mutex
)
173 if( !pthread_mutex_unlock( mutex
) )
179 static void vg_mutex_free( vg_mutex
*mutex
)
187 int vg_thread_run( void *pfunc
, void *data
)
190 HANDLE hThread
= CreateThread
192 NULL
, /* Thread attributes */
193 0, /* Stack size (0 = use default) */
194 pfunc
, /* Thread start address */
195 data
, /* Parameter to pass to the thread */
196 0, /* Creation flags */
200 if ( hThread
== NULL
)
203 * Thread creation failed.
204 * More details can be retrieved by calling GetLastError()
210 CloseHandle( hThread
);
215 if( pthread_create( &hThread
, NULL
, pfunc
, data
) )
221 pthread_detach( hThread
);
227 static void vg_sleep_ms( long msec
)
231 ts
.tv_sec
= msec
/ 1000;
232 ts
.tv_nsec
= (msec
% 1000) * 1000000;
233 nanosleep( &ts
, &ts
);
236 /* diff two timespecs in MS */
237 static double vg_time_diff( struct timespec start
, struct timespec end
)
239 double elapsed
= 1000.0*end
.tv_sec
+ 1e-6*end
.tv_nsec
240 - (1000.0*start
.tv_sec
+ 1e-6*start
.tv_nsec
);
245 #define VG_MIN( A, B ) ((A)<(B)?(A):(B))
246 #define VG_MAX( A, B ) ((A)>(B)?(A):(B))
248 static void *buffer_reserve( void *buffer
, u32 count
, u32
*cap
, u32 amount
,
251 if( count
+amount
> *cap
)
253 *cap
= VG_MAX( (*cap
)*2, (*cap
)+amount
);
254 return vg_realloc( buffer
, (*cap
) * emsize
);
260 static void *buffer_fix( void *buffer
, u32 count
, u32
*cap
, size_t emsize
)
263 return vg_realloc( buffer
, (*cap
) * emsize
);