diff --git a/app.h b/app.h index d493e4e..303ecaf 100644 --- a/app.h +++ b/app.h @@ -747,6 +747,9 @@ details. #define APP_GL_ARRAY_BUFFER 0x8892 #define APP_GL_TEXTURE_2D 0x0de1 #define APP_GL_TEXTURE0 0x84c0 + #define APP_GL_CLAMP 0x2900 + #define APP_GL_TEXTURE_WRAP_S 0x2802 + #define APP_GL_TEXTURE_WRAP_T 0x2803 #define APP_GL_TEXTURE_MIN_FILTER 0x2801 #define APP_GL_TEXTURE_MAG_FILTER 0x2800 #define APP_GL_NEAREST 0x2600 @@ -782,6 +785,9 @@ details. #define APP_GL_ARRAY_BUFFER GL_ARRAY_BUFFER #define APP_GL_TEXTURE_2D GL_TEXTURE_2D #define APP_GL_TEXTURE0 GL_TEXTURE0 + #define APP_GL_CLAMP GL_CLAMP + #define APP_GL_TEXTURE_WRAP_S GL_TEXTURE_WRAP_S + #define APP_GL_TEXTURE_WRAP_T GL_TEXTURE_WRAP_T #define APP_GL_TEXTURE_MIN_FILTER GL_TEXTURE_MIN_FILTER #define APP_GL_TEXTURE_MAG_FILTER GL_TEXTURE_MAG_FILTER #define APP_GL_NEAREST GL_NEAREST @@ -975,6 +981,8 @@ static int app_internal_opengl_init( app_t* app, struct app_internal_opengl_t* g gl->BindTexture( APP_GL_TEXTURE_2D, gl->texture ); gl->TexParameteri( APP_GL_TEXTURE_2D, APP_GL_TEXTURE_MIN_FILTER, APP_GL_NEAREST ); gl->TexParameteri( APP_GL_TEXTURE_2D, APP_GL_TEXTURE_MAG_FILTER, APP_GL_NEAREST ); + gl->TexParameteri( APP_GL_TEXTURE_2D, APP_GL_TEXTURE_WRAP_S, APP_GL_CLAMP ); + gl->TexParameteri( APP_GL_TEXTURE_2D, APP_GL_TEXTURE_WRAP_T, APP_GL_CLAMP ); return 1; } diff --git a/assetsys.h b/assetsys.h index 5fa3e5a..1ac8509 100644 --- a/assetsys.h +++ b/assetsys.h @@ -3,7 +3,7 @@ Licensing information can be found at the end of the file. ------------------------------------------------------------------------------ -assetsys.h - v1.1 - File system abstraction to read from zip-files, for C/C++. +assetsys.h - v1.2 - File system abstraction to read from zip-files, for C/C++. Do this: #define ASSETSYS_IMPLEMENTATION @@ -384,7 +384,7 @@ path. If the path is invalid or index is out of range, `assetsys_subdir_path` re #endif /* ASSETSYS_NO_MINIZ */ #ifdef ASSETSYS_ASSERT - #define MZ_ASSERT ASSETSYS_ASSERT + #define MZ_ASSERT( x ) ASSETSYS_ASSERT( x, "Miniz assert" ) #endif /* ASSETSYS_ASSERT */ #pragma warning( push ) @@ -5353,7 +5353,7 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char #define _CRT_NONSTDC_NO_DEPRECATE #define _CRT_SECURE_NO_WARNINGS #include - #define ASSETSYS_ASSERT( x ) assert( x ) + #define ASSETSYS_ASSERT( expression, message ) assert( ( expression ) && ( message ) ) #endif #ifndef ASSETSYS_MALLOC @@ -5369,13 +5369,16 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char #define _CRT_NONSTDC_NO_DEPRECATE #define _CRT_SECURE_NO_WARNINGS + #if !defined( _WIN32_WINNT ) || _WIN32_WINNT < 0x0501 #undef _WIN32_WINNT #define _WIN32_WINNT 0x0501 // requires Windows XP minimum + #endif // 0x0400=Windows NT 4.0, 0x0500=Windows 2000, 0x0501=Windows XP, 0x0502=Windows Server 2003, 0x0600=Windows Vista, // 0x0601=Windows 7, 0x0602=Windows 8, 0x0603=Windows 8.1, 0x0A00=Windows 10, #define _WINSOCKAPI_ #pragma warning( push ) #pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives' + #pragma warning( disable: 4768 ) // __declspec attributes before linkage specification are ignored #pragma warning( disable: 4255 ) // 'function' : no function prototype given: converting '()' to '(void)' #include #pragma warning( pop ) @@ -5389,21 +5392,18 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char struct assetsys_internal_dir_t { - void* memctx; HANDLE handle; WIN32_FIND_DATAA data; struct assetsys_internal_dir_entry_t entry; }; - static struct assetsys_internal_dir_t* assetsys_internal_dir_open( char const* path, void* memctx ) + static void assetsys_internal_dir_open( struct assetsys_internal_dir_t* dir, char const* path ) { - if( !path ) return 0; - size_t path_len = strlen( path ); BOOL trailing_path_separator = path[ path_len - 1 ] == '\\' || path[ path_len - 1 ] == '/'; const char* string_to_append = "*.*"; - if( path_len + strlen( string_to_append ) + ( trailing_path_separator ? 0 : 1 ) >= MAX_PATH ) return NULL; + if( path_len + strlen( string_to_append ) + ( trailing_path_separator ? 0 : 1 ) >= MAX_PATH ) return; char search_pattern[ MAX_PATH ]; strcpy( search_pattern, path ); if( !trailing_path_separator ) strcat( search_pattern, "\\" ); @@ -5411,30 +5411,21 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char WIN32_FIND_DATAA data; HANDLE handle = FindFirstFileA( search_pattern, &data ); - if( handle == INVALID_HANDLE_VALUE ) return NULL; + if( handle == INVALID_HANDLE_VALUE ) return; - struct assetsys_internal_dir_t* dir = (struct assetsys_internal_dir_t*) ASSETSYS_MALLOC( memctx, - sizeof( struct assetsys_internal_dir_t ) ); - dir->memctx = memctx; dir->handle = handle; dir->data = data; - - return dir; } static void assetsys_internal_dir_close( struct assetsys_internal_dir_t* dir ) { - if( !dir ) return; - if( dir->handle != INVALID_HANDLE_VALUE ) FindClose( dir->handle ); - ASSETSYS_FREE( dir->memctx, dir ); } static struct assetsys_internal_dir_entry_t* assetsys_internal_dir_read( struct assetsys_internal_dir_t* dir ) { - if( !dir ) return NULL; if( dir->handle == INVALID_HANDLE_VALUE ) return NULL; strcpy( dir->entry.name, dir->data.cFileName ); @@ -5453,21 +5444,18 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char static char const* assetsys_internal_dir_name( struct assetsys_internal_dir_entry_t* entry ) { - if( !entry ) return NULL; return entry->name; } static int assetsys_internal_dir_is_file( struct assetsys_internal_dir_entry_t* entry ) { - if( !entry ) return 0; return entry->is_folder == FALSE; } static int assetsys_internal_dir_is_folder( struct assetsys_internal_dir_entry_t* entry ) { - if( !entry ) return 0; return entry->is_folder == TRUE; } @@ -5476,48 +5464,47 @@ void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char #include - typedef struct assetsys_internal_dir_t assetsys_internal_dir_t; + struct assetsys_internal_dir_t + { + DIR* dir + }; + + typedef struct assetsys_internal_dir_entry_t assetsys_internal_dir_entry_t; - static assetsys_internal_dir_t* assetsys_internal_dir_open( char const* path, void* memctx ) + + static void assetsys_internal_dir_open( struct assetsys_internal_dir_t* dir, char const* path ) { - (void) memctx; - DIR* dir = opendir( path ); - return (assetsys_internal_dir_t*) dir; + dir->dir = opendir( path ); } static void assetsys_internal_dir_close( assetsys_internal_dir_t* dir ) { - if( !dir ) return; - closedir( (DIR*) dir ); + closedir( dir->dir ); } static assetsys_internal_dir_entry_t* assetsys_internal_dir_read( assetsys_internal_dir_t* dir ) { - if( !dir ) return NULL; - return (assetsys_internal_dir_entry_t*)readdir( (DIR*) dir ); + return (assetsys_internal_dir_entry_t*)readdir( dir->dir ); } static char const* assetsys_internal_dir_name( assetsys_internal_dir_entry_t* entry ) { - if( !entry ) return NULL; return ( (struct dirent*)entry )->d_name; } static int assetsys_internal_dir_is_file( assetsys_internal_dir_entry_t* entry ) { - if( !entry ) return 0; return ( (struct dirent*)entry )->d_type == DT_REG; } static int assetsys_internal_dir_is_folder( assetsys_internal_dir_entry_t* entry ) { - if( !entry ) return 0; return ( (struct dirent*)entry )->d_type == DT_DIR; } @@ -5536,6 +5523,7 @@ static void* assetsys_internal_mz_alloc( void* memctx, size_t items, size_t size static void assetsys_internal_mz_free( void* memctx, void* ptr ) { (void) memctx; (void) ptr; + if( !ptr ) return; ASSETSYS_U64* p = ( (ASSETSYS_U64*) ptr ) - 1; ASSETSYS_FREE( memctx, p ); } @@ -5681,7 +5669,7 @@ static int assetsys_internal_register_collated( assetsys_t* sys, char const* con { if( sys->collated[ i ].ref_count > 0 && sys->collated[ i ].path == handle ) { - ASSETSYS_ASSERT( is_file == sys->collated[ i ].is_file ); + ASSETSYS_ASSERT( is_file == sys->collated[ i ].is_file, "Entry type mismatch" ); ++sys->collated[ i ].ref_count; strpool_discard( &sys->strpool, handle ); return i; @@ -5771,10 +5759,11 @@ static void assetsys_internal_recurse_directories( assetsys_t* sys, int const co strcat( sys->temp, ( *path == '\0' || *sys->temp == '\0' ) ? "" : "/" ); strcat( sys->temp, path ); - struct assetsys_internal_dir_t* dir = assetsys_internal_dir_open( *sys->temp == '\0' ? "." : sys->temp, sys->memctx ); + struct assetsys_internal_dir_t dir; + assetsys_internal_dir_open( &dir, *sys->temp == '\0' ? "." : sys->temp ); struct assetsys_internal_dir_entry_t* dirent; - for( dirent = assetsys_internal_dir_read( dir ); dirent != NULL; dirent = assetsys_internal_dir_read( dir ) ) + for( dirent = assetsys_internal_dir_read( &dir ); dirent != NULL; dirent = assetsys_internal_dir_read( &dir ) ) { char const* name = assetsys_internal_dir_name( dirent ); if( !name || *name == '\0' || strcmp( name, "." ) == 0 || strcmp( name, ".." ) == 0 ) continue; @@ -5853,7 +5842,7 @@ static void assetsys_internal_recurse_directories( assetsys_t* sys, int const co } } } - assetsys_internal_dir_close( dir ); + assetsys_internal_dir_close( &dir ); } @@ -6029,7 +6018,7 @@ assetsys_error_t assetsys_mount( assetsys_t* sys, char const* path, char const* static void assetsys_internal_remove_collated( assetsys_t* sys, int const index ) { struct assetsys_internal_collated_t* coll = &sys->collated[ index ]; - ASSETSYS_ASSERT( coll->ref_count > 0 ); + ASSETSYS_ASSERT( coll->ref_count > 0, "Invalid ref count" ); --coll->ref_count; if( coll->ref_count == 0 ) { @@ -6320,6 +6309,7 @@ contributors: Randy Gaul (hotloading support) revision history: + 1.2 asserts with message, eliminated a frequent small allocation 1.1 changes to support loading assets being re-saved during execution 1.0 first released version diff --git a/hashtable.h b/hashtable.h index 4cbe61b..5016551 100644 --- a/hashtable.h +++ b/hashtable.h @@ -3,7 +3,7 @@ Licensing information can be found at the end of the file. ------------------------------------------------------------------------------ -hashtable.h - v1.1 - Cache efficient hash table implementation for C/C++. +hashtable.h - v2.0 - Cache efficient hash table implementation for C/C++. Do this: #define HASHTABLE_IMPLEMENTATION @@ -13,30 +13,102 @@ before you include this file in *one* C/C++ file to create the implementation. #ifndef hashtable_h #define hashtable_h -#ifndef HASHTABLE_U64 - #define HASHTABLE_U64 unsigned long long +#ifndef HASHTABLE_U32 + #define HASHTABLE_U32 unsigned int #endif typedef struct hashtable_t hashtable_t; -void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx ); +void hashtable_init( hashtable_t* table, int key_size, int item_size, int initial_capacity, void* memctx ); void hashtable_term( hashtable_t* table ); -void hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item ); -void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key ); +void hashtable_insert( hashtable_t* table, HASHTABLE_U32 hash, void const* key, void const* item ); +void hashtable_remove( hashtable_t* table, HASHTABLE_U32 hash, void const* key ); void hashtable_clear( hashtable_t* table ); -void* hashtable_find( hashtable_t const* table, HASHTABLE_U64 key ); +void* hashtable_find( hashtable_t const* table, HASHTABLE_U32 hash, void const* key ); int hashtable_count( hashtable_t const* table ); void* hashtable_items( hashtable_t const* table ); -HASHTABLE_U64 const* hashtable_keys( hashtable_t const* table ); +void const* hashtable_keys( hashtable_t const* table ); void hashtable_swap( hashtable_t* table, int index_a, int index_b ); #endif /* hashtable_h */ +/* + +uint32_t hash_u32( uint32_t key ) { + key = ~key + ( key << 15 ); + key = key ^ ( key >> 12 ); + key = key + ( key << 2 ); + key = key ^ ( key >> 4 ); + key = (key + ( key << 3 ) ) + ( key << 11 ); + key = key ^ ( key >> 16); + return key; +} + + +uint32_t hash_u64( uint64_t key ) { + key = ( ~key ) + ( key << 18 ); + key = key ^ ( key >> 31 ); + key = ( key + ( key << 2 ) ) + ( key << 4 ); + key = key ^ ( key >> 11 ); + key = key + ( key << 6 ); + key = key ^ ( key >> 22 ); + return (uint32_t) key; +} + + +uint32_t hash_str( char const* key ) { + uint32_t hash = 5381u; + for( char const* s = key; *s != ’\0’; ++s ) { + hash = ( ( hash << 5u ) + hash ) ^ (*s); + } + return hash; +} + + +uint32_t murmur_hash( const void * key, int len, uint32_t seed ) { + uint32_t const m = 0x5bd1e995; + int const r = 24; + uint32_t h = seed ^ len; + + uint8_t const* data = (uint8_t const*) key; + while( len >= 4 ) { + #ifdef PLATFORM_BIG_ENDIAN + uint32_t k = ( data[0] ) + ( data[1] << 8 ) + ( data[2] << 16 ) + ( data[3] << 24 ); + #else + uint32_t k = *(uint32_t *)data; + #endif + + k *= m; + k ^= k >> r; + k *= m; + + h *= m; + h ^= k; + + data += 4; + len -= 4; + } + + switch(len) { + case 3: h ^= data[2] << 16; + case 2: h ^= data[1] << 8; + case 1: h ^= data[0]; + h *= m; + }; + + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; +} + +*/ /** @@ -61,7 +133,7 @@ Example char id[ 64 ]; float x, y, z; int n[ 250 ]; - } value_t; + } value_t; // create a couple of sample keys // (don't bother to fill in the fields for this sample) @@ -97,13 +169,13 @@ Example for( int i = 0; i < count; ++i ) { printf( " 0x%X : %s\n", (int) keys[ i ], items[ i ].id ); } - + // cleanup hashtable_term( &table ); free( key_b ); free( key_a ); return 0; - } + } API Documentation @@ -310,10 +382,6 @@ retrieved by calling `hashtable_items` and `hashtable_keys`, while keeping the h #ifndef hashtable_t_h #define hashtable_t_h -#ifndef HASHTABLE_U32 - #define HASHTABLE_U32 unsigned int -#endif - struct hashtable_internal_slot_t { HASHTABLE_U32 key_hash; @@ -325,12 +393,14 @@ struct hashtable_t { void* memctx; int count; + int key_size; int item_size; struct hashtable_internal_slot_t* slots; int slot_capacity; + int prime_index; - HASHTABLE_U64* items_key; + void* items_key; int* items_slot; void* items_data; int item_capacity; @@ -362,15 +432,6 @@ struct hashtable_t #define HASHTABLE_ASSERT( x ) assert( x ) #endif -#ifndef HASHTABLE_MEMSET - #undef _CRT_NONSTDC_NO_DEPRECATE - #define _CRT_NONSTDC_NO_DEPRECATE - #undef _CRT_SECURE_NO_WARNINGS - #define _CRT_SECURE_NO_WARNINGS - #include - #define HASHTABLE_MEMSET( ptr, val, cnt ) ( memset( ptr, val, cnt ) ) -#endif - #ifndef HASHTABLE_MEMCPY #undef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE @@ -380,6 +441,33 @@ struct hashtable_t #define HASHTABLE_MEMCPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) ) #endif +#ifndef HASHTABLE_KEYCOPY + #undef _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_NONSTDC_NO_DEPRECATE + #undef _CRT_SECURE_NO_WARNINGS + #define _CRT_SECURE_NO_WARNINGS + #include + #define HASHTABLE_KEYCOPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) ) +#endif + +#ifndef HASHTABLE_ITEMCOPY + #undef _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_NONSTDC_NO_DEPRECATE + #undef _CRT_SECURE_NO_WARNINGS + #define _CRT_SECURE_NO_WARNINGS + #include + #define HASHTABLE_ITEMCOPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) ) +#endif + +#ifndef HASHTABLE_KEYCMP + #undef _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_NONSTDC_NO_DEPRECATE + #undef _CRT_SECURE_NO_WARNINGS + #define _CRT_SECURE_NO_WARNINGS + #include + #define HASHTABLE_KEYCMP( a, b, len ) ( memcmp( a, b, len ) == 0 ) +#endif + #ifndef HASHTABLE_MALLOC #undef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE @@ -405,22 +493,47 @@ static HASHTABLE_U32 hashtable_internal_pow2ceil( HASHTABLE_U32 v ) } -void hashtable_init( hashtable_t* table, int item_size, int initial_capacity, void* memctx ) +static int hashtable_internal_primes[] = { 31, 67, 127, 257, 509, 1021, 2053, 4099, 8191, 16381, 32771, 65537, 131071, + 262147, 524287, 1048573, 2097143, 4194301, 8388617, 16777213, 33554467, 67108859, 134217757, 268435459, 536870909, + 1073741827, 2147483647 }; + + +void hashtable_init( hashtable_t* table, int key_size, int item_size, int initial_capacity, void* memctx ) { initial_capacity = (int)hashtable_internal_pow2ceil( initial_capacity >=0 ? (HASHTABLE_U32) initial_capacity : 32U ); + int prime_index = 0; + while( hashtable_internal_primes[ prime_index ] < initial_capacity + initial_capacity / 2 ) ++prime_index; + table->memctx = memctx; table->count = 0; + table->key_size = key_size; table->item_size = item_size; - table->slot_capacity = (int) hashtable_internal_pow2ceil( (HASHTABLE_U32) ( initial_capacity + initial_capacity / 2 ) ); - int slots_size = (int)( table->slot_capacity * sizeof( *table->slots ) ); - table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) slots_size ); - HASHTABLE_ASSERT( table->slots ); - HASHTABLE_MEMSET( table->slots, 0, (HASHTABLE_SIZE_T) slots_size ); - table->item_capacity = (int) hashtable_internal_pow2ceil( (HASHTABLE_U32) initial_capacity ); - table->items_key = (HASHTABLE_U64*) HASHTABLE_MALLOC( table->memctx, - table->item_capacity * ( sizeof( *table->items_key ) + sizeof( *table->items_slot ) + table->item_size ) + table->item_size ); + table->slot_capacity = hashtable_internal_primes[ prime_index ]; + table->prime_index = prime_index; + + if( key_size > 0 ) + { + int slots_size = (int)( table->slot_capacity * sizeof( *table->slots ) ); + table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) slots_size ); + HASHTABLE_ASSERT( table->slots ); + + for( int i = 0; i < table->slot_capacity; ++i ) + { + table->slots[ i ].base_count = 0; + table->slots[ i ].item_index = -1; + } + } + else + { + table->slots = 0; + } + + table->item_capacity = initial_capacity; + table->items_key = HASHTABLE_MALLOC( table->memctx, + table->item_capacity * ( table->key_size + sizeof( *table->items_slot ) + table->item_size ) + + ( table->key_size > table->item_size ? table->key_size : table->item_size ) ); HASHTABLE_ASSERT( table->items_key ); - table->items_slot = (int*)( table->items_key + table->item_capacity ); + table->items_slot = (int*)( ( (uintptr_t) table->items_key ) + table->key_size * table->item_capacity ); table->items_data = (void*)( table->items_slot + table->item_capacity ); table->swap_temp = (void*)( ( (uintptr_t) table->items_data ) + table->item_size * table->item_capacity ); } @@ -433,44 +546,33 @@ void hashtable_term( hashtable_t* table ) } -// from https://gist.github.com/badboy/6267743 -static HASHTABLE_U32 hashtable_internal_calculate_hash( HASHTABLE_U64 key ) + +static int hashtable_internal_find_slot( hashtable_t const* table, HASHTABLE_U32 hash, void const* key ) { - key = ( ~key ) + ( key << 18 ); - key = key ^ ( key >> 31 ); - key = key * 21; - key = key ^ ( key >> 11 ); - key = key + ( key << 6 ); - key = key ^ ( key >> 22 ); - HASHTABLE_ASSERT( key ); - return (HASHTABLE_U32) key; - } + HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity; + int const base_slot = (int)( hash % slot_capacity ); - -static int hashtable_internal_find_slot( hashtable_t const* table, HASHTABLE_U64 key ) - { - int const slot_mask = table->slot_capacity - 1; - HASHTABLE_U32 const hash = hashtable_internal_calculate_hash( key ); - - int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask ); int base_count = table->slots[ base_slot ].base_count; int slot = base_slot; - while( base_count > 0 ) { - HASHTABLE_U32 slot_hash = table->slots[ slot ].key_hash; - if( slot_hash ) + if( table->slots[ slot ].item_index >= 0 ) { - int slot_base = (int)( slot_hash & (HASHTABLE_U32)slot_mask ); + HASHTABLE_U32 slot_hash = table->slots[ slot ].key_hash; + int slot_base = (int)( slot_hash % slot_capacity ); if( slot_base == base_slot ) { HASHTABLE_ASSERT( base_count > 0 ); + if( slot_hash == hash ) + { + void const* slot_key = (void const*)( ( (uintptr_t) table->items_key ) + table->key_size * table->slots[ slot ].item_index ); + if( HASHTABLE_KEYCMP( slot_key, key, table->key_size ) ) + return slot; + } --base_count; - if( slot_hash == hash && table->items_key[ table->slots[ slot ].item_index ] == key ) - return slot; } } - slot = ( slot + 1 ) & slot_mask; + slot = (int)( ( slot + 1 ) % slot_capacity ); } return -1; @@ -479,26 +581,32 @@ static int hashtable_internal_find_slot( hashtable_t const* table, HASHTABLE_U64 static void hashtable_internal_expand_slots( hashtable_t* table ) { + if( !table->slots ) return; + int const old_capacity = table->slot_capacity; struct hashtable_internal_slot_t* old_slots = table->slots; - table->slot_capacity *= 2; - int const slot_mask = table->slot_capacity - 1; + table->slot_capacity = hashtable_internal_primes[ ++table->prime_index ]; + HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity; int const size = (int)( table->slot_capacity * sizeof( *table->slots ) ); table->slots = (struct hashtable_internal_slot_t*) HASHTABLE_MALLOC( table->memctx, (HASHTABLE_SIZE_T) size ); HASHTABLE_ASSERT( table->slots ); - HASHTABLE_MEMSET( table->slots, 0, (HASHTABLE_SIZE_T) size ); + for( int i = 0; i < table->slot_capacity; ++i ) + { + table->slots[ i ].base_count = 0; + table->slots[ i ].item_index = -1; + } for( int i = 0; i < old_capacity; ++i ) { - HASHTABLE_U32 const hash = old_slots[ i ].key_hash; - if( hash ) + if( old_slots[ i ].item_index >= 0 ) { - int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask ); + HASHTABLE_U32 const hash = old_slots[ i ].key_hash; + int const base_slot = (int)( hash % slot_capacity ); int slot = base_slot; - while( table->slots[ slot ].key_hash ) - slot = ( slot + 1 ) & slot_mask; + while( table->slots[ slot ].item_index >= 0 ) + slot = (int)( ( slot + 1 ) % slot_capacity ); table->slots[ slot ].key_hash = hash; int item_index = old_slots[ i ].item_index; table->slots[ slot ].item_index = item_index; @@ -514,16 +622,18 @@ static void hashtable_internal_expand_slots( hashtable_t* table ) static void hashtable_internal_expand_items( hashtable_t* table ) { table->item_capacity *= 2; - HASHTABLE_U64* const new_items_key = (HASHTABLE_U64*) HASHTABLE_MALLOC( table->memctx, - table->item_capacity * ( sizeof( *table->items_key ) + sizeof( *table->items_slot ) + table->item_size ) + table->item_size); + + void* const new_items_key = HASHTABLE_MALLOC( table->memctx, + table->item_capacity * ( table->key_size + sizeof( *table->items_slot ) + table->item_size ) + + ( table->key_size > table->item_size ? table->key_size : table->item_size ) ); HASHTABLE_ASSERT( new_items_key ); - int* const new_items_slot = (int*)( new_items_key + table->item_capacity ); + int* const new_items_slot = (int*)( ( (uintptr_t) new_items_key ) + table->key_size * table->item_capacity ); void* const new_items_data = (void*)( new_items_slot + table->item_capacity ); void* const new_swap_temp = (void*)( ( (uintptr_t) new_items_data ) + table->item_size * table->item_capacity ); - HASHTABLE_MEMCPY( new_items_key, table->items_key, table->count * sizeof( *table->items_key ) ); - HASHTABLE_MEMCPY( new_items_slot, table->items_slot, table->count * sizeof( *table->items_key ) ); + HASHTABLE_MEMCPY( new_items_key, table->items_key, table->count * table->key_size ); + HASHTABLE_MEMCPY( new_items_slot, table->items_slot, table->count * sizeof( *table->items_slot ) ); HASHTABLE_MEMCPY( new_items_data, table->items_data, (HASHTABLE_SIZE_T) table->count * table->item_size ); HASHTABLE_FREE( table->memctx, table->items_key ); @@ -535,74 +645,87 @@ static void hashtable_internal_expand_items( hashtable_t* table ) } -void hashtable_insert( hashtable_t* table, HASHTABLE_U64 key, void const* item ) +void hashtable_insert( hashtable_t* table, HASHTABLE_U32 hash, void const* key, void const* item ) { - HASHTABLE_ASSERT( hashtable_internal_find_slot( table, key ) < 0 ); + if( !table->slots ) + { + if( table->count >= table->item_capacity ) + hashtable_internal_expand_items( table ); + + void* dest_item = (void*)( ( (uintptr_t) table->items_data ) + table->count * table->item_size ); + HASHTABLE_ITEMCOPY( dest_item, item, (HASHTABLE_SIZE_T) table->item_size ); + ++table->count; + return; + } + + HASHTABLE_ASSERT( hashtable_internal_find_slot( table, hash, key ) < 0 ); if( table->count >= ( table->slot_capacity - table->slot_capacity / 3 ) ) hashtable_internal_expand_slots( table ); - int const slot_mask = table->slot_capacity - 1; - HASHTABLE_U32 const hash = hashtable_internal_calculate_hash( key ); - - int const base_slot = (int)( hash & (HASHTABLE_U32)slot_mask ); + HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity; + int const base_slot = (int)( hash % slot_capacity ); int base_count = table->slots[ base_slot ].base_count; int slot = base_slot; int first_free = slot; while( base_count ) { + if( table->slots[ slot ].item_index < 0 && table->slots[ first_free ].item_index >= 0 ) first_free = slot; HASHTABLE_U32 const slot_hash = table->slots[ slot ].key_hash; - if( slot_hash == 0 && table->slots[ first_free ].key_hash != 0 ) first_free = slot; - int slot_base = (int)( slot_hash & (HASHTABLE_U32)slot_mask ); + int slot_base = (int)( slot_hash % slot_capacity ); if( slot_base == base_slot ) --base_count; - slot = ( slot + 1 ) & slot_mask; + slot = (int)( ( slot + 1 ) % slot_capacity ); } slot = first_free; - while( table->slots[ slot ].key_hash ) - slot = ( slot + 1 ) & slot_mask; + while( table->slots[ slot ].item_index >= 0 ) + slot = (int)( ( slot + 1 ) % slot_capacity ); if( table->count >= table->item_capacity ) hashtable_internal_expand_items( table ); - HASHTABLE_ASSERT( !table->slots[ slot ].key_hash && ( hash & (HASHTABLE_U32) slot_mask ) == (HASHTABLE_U32) base_slot ); - HASHTABLE_ASSERT( hash ); + HASHTABLE_ASSERT( table->slots[ slot ].item_index < 0 && (int)( hash % slot_capacity ) == (HASHTABLE_U32) base_slot ); table->slots[ slot ].key_hash = hash; table->slots[ slot ].item_index = table->count; ++table->slots[ base_slot ].base_count; void* dest_item = (void*)( ( (uintptr_t) table->items_data ) + table->count * table->item_size ); - memcpy( dest_item, item, (HASHTABLE_SIZE_T) table->item_size ); - table->items_key[ table->count ] = key; + HASHTABLE_ITEMCOPY( dest_item, item, (HASHTABLE_SIZE_T) table->item_size ); + void* dest_key = (void*)( ( (uintptr_t) table->items_key ) + table->count * table->key_size ); + HASHTABLE_KEYCOPY( dest_key, key, (HASHTABLE_SIZE_T) table->key_size ); table->items_slot[ table->count ] = slot; ++table->count; } -void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key ) +void hashtable_remove( hashtable_t* table, HASHTABLE_U32 hash, void const* key ) { - int const slot = hashtable_internal_find_slot( table, key ); - HASHTABLE_ASSERT( slot >= 0 ); - - int const slot_mask = table->slot_capacity - 1; - HASHTABLE_U32 const hash = table->slots[ slot ].key_hash; - int const base_slot = (int)( hash & (HASHTABLE_U32) slot_mask ); - HASHTABLE_ASSERT( hash ); - --table->slots[ base_slot ].base_count; - table->slots[ slot ].key_hash = 0; - - int index = table->slots[ slot ].item_index; - int last_index = table->count - 1; - if( index != last_index ) + if( table->slots ) { - table->items_key[ index ] = table->items_key[ last_index ]; - table->items_slot[ index ] = table->items_slot[ last_index ]; - void* dst_item = (void*)( ( (uintptr_t) table->items_data ) + index * table->item_size ); - void* src_item = (void*)( ( (uintptr_t) table->items_data ) + last_index * table->item_size ); - HASHTABLE_MEMCPY( dst_item, src_item, (HASHTABLE_SIZE_T) table->item_size ); - table->slots[ table->items_slot[ last_index ] ].item_index = index; + int const slot = hashtable_internal_find_slot( table, hash, key ); + HASHTABLE_ASSERT( slot >= 0 ); + + HASHTABLE_U32 slot_capacity = (HASHTABLE_U32) table->slot_capacity; + int const base_slot = (int)( hash % slot_capacity ); + HASHTABLE_ASSERT( hash ); + --table->slots[ base_slot ].base_count; + table->slots[ slot ].item_index = -1; + + int index = table->slots[ slot ].item_index; + int last_index = table->count - 1; + if( index != last_index ) + { + void* dst_key = (void*)( ( (uintptr_t) table->items_key ) + index * table->key_size ); + void* src_key = (void*)( ( (uintptr_t) table->items_key ) + last_index * table->key_size ); + HASHTABLE_KEYCOPY( dst_key, src_key, (HASHTABLE_SIZE_T) table->key_size ); + table->items_slot[ index ] = table->items_slot[ last_index ]; + void* dst_item = (void*)( ( (uintptr_t) table->items_data ) + index * table->item_size ); + void* src_item = (void*)( ( (uintptr_t) table->items_data ) + last_index * table->item_size ); + HASHTABLE_ITEMCOPY( dst_item, src_item, (HASHTABLE_SIZE_T) table->item_size ); + table->slots[ table->items_slot[ last_index ] ].item_index = index; + } } --table->count; } @@ -611,13 +734,20 @@ void hashtable_remove( hashtable_t* table, HASHTABLE_U64 key ) void hashtable_clear( hashtable_t* table ) { table->count = 0; - HASHTABLE_MEMSET( table->slots, 0, table->slot_capacity * sizeof( *table->slots ) ); + if( table->slots ) + { + for( int i = 0; i < table->slot_capacity; ++i ) + { + table->slots[ i ].base_count = 0; + table->slots[ i ].item_index = -1; + } + } } -void* hashtable_find( hashtable_t const* table, HASHTABLE_U64 key ) +void* hashtable_find( hashtable_t const* table, HASHTABLE_U32 hash, void const* key ) { - int const slot = hashtable_internal_find_slot( table, key ); + int const slot = table->slots ? hashtable_internal_find_slot( table, hash, key ) : -1; if( slot < 0 ) return 0; int const index = table->slots[ slot ].item_index; @@ -638,7 +768,7 @@ void* hashtable_items( hashtable_t const* table ) } -HASHTABLE_U64 const* hashtable_keys( hashtable_t const* table ) +void const* hashtable_keys( hashtable_t const* table ) { return table->items_key; } @@ -654,18 +784,23 @@ void hashtable_swap( hashtable_t* table, int index_a, int index_b ) table->items_slot[ index_a ] = slot_b; table->items_slot[ index_b ] = slot_a; - HASHTABLE_U64 temp_key = table->items_key[ index_a ]; - table->items_key[ index_a ] = table->items_key[ index_b ]; - table->items_key[ index_b ] = temp_key; + void* key_a = (void*)( ( (uintptr_t) table->items_key ) + index_a * table->key_size ); + void* key_b = (void*)( ( (uintptr_t) table->items_key ) + index_b * table->key_size ); + HASHTABLE_KEYCOPY( table->swap_temp, key_a, table->key_size ); + HASHTABLE_KEYCOPY( key_a, key_b, table->key_size ); + HASHTABLE_KEYCOPY( key_b, table->swap_temp, table->key_size ); void* item_a = (void*)( ( (uintptr_t) table->items_data ) + index_a * table->item_size ); void* item_b = (void*)( ( (uintptr_t) table->items_data ) + index_b * table->item_size ); - HASHTABLE_MEMCPY( table->swap_temp, item_a, table->item_size ); - HASHTABLE_MEMCPY( item_a, item_b, table->item_size ); - HASHTABLE_MEMCPY( item_b, table->swap_temp, table->item_size ); + HASHTABLE_ITEMCOPY( table->swap_temp, item_a, table->item_size ); + HASHTABLE_ITEMCOPY( item_a, item_b, table->item_size ); + HASHTABLE_ITEMCOPY( item_b, table->swap_temp, table->item_size ); - table->slots[ slot_a ].item_index = index_b; - table->slots[ slot_b ].item_index = index_a; + if( table->slots ) + { + table->slots[ slot_a ].item_index = index_b; + table->slots[ slot_b ].item_index = index_a; + } } @@ -677,6 +812,7 @@ contributors: Randy Gaul (hashtable_clear, hashtable_swap ) revision history: + 2.0 variable key size, custom hashing 1.1 added hashtable_clear, hashtable_swap 1.0 first released version diff --git a/ini.h b/ini.h index d895de0..ebd6994 100644 --- a/ini.h +++ b/ini.h @@ -739,7 +739,7 @@ int ini_find_section( ini_t const* ini, char const* name, int name_length ) { char const* const other = ini->sections[ i ].name_large ? ini->sections[ i ].name_large : ini->sections[ i ].name; - if( INI_STRNICMP( name, other, name_length ) == 0 ) + if( INI_STRNICMP( name, other, (size_t)name_length ) == 0 ) return i; } } @@ -763,7 +763,7 @@ int ini_find_property( ini_t const* ini, int section, char const* name, int name { char const* const other = ini->properties[ i ].name_large ? ini->properties[ i ].name_large : ini->properties[ i ].name; - if( INI_STRNICMP( name, other, name_length ) == 0 ) + if( INI_STRNICMP( name, other, (size_t) name_length ) == 0 ) return c; ++c; } diff --git a/rnd.h b/rnd.h index 74c25e3..ad870b0 100644 --- a/rnd.h +++ b/rnd.h @@ -8,9 +8,6 @@ rnd.h - v1.0 - Pseudo-random number generators for C/C++. Do this: #define RND_IMPLEMENTATION before you include this file in *one* C/C++ file to create the implementation. - -Additional Contributors - Jonatan Hedborg: unsigned int to normalized float conversion */ #ifndef rnd_h @@ -520,6 +517,10 @@ int rnd_xorshift_range( rnd_xorshift_t* xorshift, int min, int max ) #endif /* RND_IMPLEMENTATION */ /* + +contributors: + Jonatan Hedborg (unsigned int to normalized float conversion) + revision history: 1.0 first publicly released version */ diff --git a/sysfont.h b/sysfont.h index 4c68ef4..b70127f 100644 --- a/sysfont.h +++ b/sysfont.h @@ -98,30 +98,30 @@ Examples: if( !text || !pixels ) return; \ char const* str = text; \ SYSFONT_U32* const data = sysfont_data; \ - int xp = x; \ + int px = x; \ while( *str ) \ { \ SYSFONT_U8 c = (SYSFONT_U8) *str++; \ int sx = ( c % M ) * W; \ int sy = ( c / M ) * H + O; \ - int dx = xp; \ + int dx = px; \ int dy = y; \ - for( int y = 0; y < H; ++y ) \ + for( int iy = 0; iy < H; ++iy ) \ { \ - for( int x = 0; x < W; ++x ) \ + for( int ix = 0; ix < W; ++ix ) \ { \ - int v = ( sx + x ) / 32; \ - int u = ( sx + x ) - ( v * 32 ); \ - SYSFONT_U32 b = data[ v + ( sy + y ) * 8 ]; \ + int v = ( sx + ix ) / 32; \ + int u = ( sx + ix ) - ( v * 32 ); \ + SYSFONT_U32 b = data[ v + ( sy + iy ) * 8 ]; \ if( b & ( 1 << u ) ) \ { \ - int xp = dx + x; \ - int yp = dy + y; \ + int xp = dx + ix; \ + int yp = dy + iy; \ if( xp >= 0 && xp < width && yp >= 0 && yp < height ) pixels[ xp + yp * width ] = color; \ } \ } \ } \ - xp += W; \ + px += W; \ } @@ -129,10 +129,10 @@ Examples: SYSFONT_DATA \ SYSFONT_U32* src = sysfont_data; \ T* row = texture; \ - for( int y = 0; y < 224; ++y ) \ + for( int iy = 0; iy < 224; ++iy ) \ { \ T* dst = row; \ - for( int x = 0; x < 256 / 32; ++x ) \ + for( int ix = 0; ix < 256 / 32; ++ix ) \ { \ SYSFONT_U32 b = *src++; \ for( int i = 0; i < 32; ++i ) \ @@ -140,10 +140,10 @@ Examples: } \ row = (T*) ( ( (SYSFONT_U8*) row ) + pitch_in_bytes ); \ } \ - for( int y = 224; y < 256; ++y ) \ + for( int iy = 224; iy < 256; ++iy ) \ { \ T* dst = row; \ - for( int x = 0; x < 256; ++x ) \ + for( int ix = 0; ix < 256; ++ix ) \ *dst++ = bgcolor; \ row = (T*) ( ( (SYSFONT_U8*) row ) + pitch_in_bytes ); \ } @@ -185,19 +185,19 @@ void sysfont_8x8_u32( SYSFONT_U32* pixels, int width, int height, int x, int y, } -void sysfont_texture_u8( SYSFONT_U8* const texture, int const pitch_in_bytes, SYSFONT_U8 const color, SYSFONT_U8 const bgcolor ) +void sysfont_texture_u8( SYSFONT_U8* texture, int pitch_in_bytes, SYSFONT_U8 color, SYSFONT_U8 bgcolor ) { SYSFONT_TEX(SYSFONT_U8); } -void sysfont_texture_u16( SYSFONT_U16* const texture, int const pitch_in_bytes, SYSFONT_U16 const color, SYSFONT_U16 const bgcolor ) +void sysfont_texture_u16( SYSFONT_U16* texture, int pitch_in_bytes, SYSFONT_U16 color, SYSFONT_U16 bgcolor ) { SYSFONT_TEX(SYSFONT_U16); } -void sysfont_texture_u32( SYSFONT_U32* const texture, int const pitch_in_bytes, SYSFONT_U32 const color, SYSFONT_U32 const bgcolor ) +void sysfont_texture_u32( SYSFONT_U32* texture, int pitch_in_bytes, SYSFONT_U32 color, SYSFONT_U32 bgcolor ) { SYSFONT_TEX(SYSFONT_U32); } diff --git a/testfw.h b/testfw.h index 0315a35..f4e7f06 100644 --- a/testfw.h +++ b/testfw.h @@ -3,7 +3,7 @@ Licensing information can be found at the end of the file. ------------------------------------------------------------------------------ -testfw.h - v1.0 - Basic test framwework for C/C++. +testfw.h - v1.1 - Basic test framwework for C/C++. Do this: #define TESTFW_IMPLEMENTATION @@ -15,13 +15,18 @@ before you include this file in *one* C/C++ file to create the implementation. #define TESTFW_INIT() testfw_init() #define TESTFW_SUMMARY() testfw_summary( __FILE__, __func__, __LINE__ ) -#define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ ); -#define TESTFW_TEST_END() testfw_test_end( __FILE__, __func__, __LINE__ ) +#if defined( _WIN32 ) && !defined( TESTFW_NO_SEH ) + #define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ ); __try { + #define TESTFW_TEST_END() } __except( EXCEPTION_EXECUTE_HANDLER ) { testfw_exception( GetExceptionCode() ); } \ + testfw_test_end( __FILE__, __func__, __LINE__ ) +#else + #define TESTFW_TEST_BEGIN( desc ) testfw_test_begin( desc, __FILE__, __func__, __LINE__ ) + #define TESTFW_TEST_END() testfw_test_end( __FILE__, __func__, __LINE__ ) +#endif #define TESTFW_EXPECTED( expression ) testfw_expected( (expression) ? 1 : 0, #expression, __FILE__, __func__, __LINE__ ) - void testfw_init(); -void testfw_summary( char const* filename, char const* funcname, int line ); +int testfw_summary( char const* filename, char const* funcname, int line ); void testfw_test_begin( char const* desc, char const* filename, char const* funcname, int line ); void testfw_test_end( char const* filename, char const* funcname, int line ); void testfw_expected( int expression, char const* expression_str, char const* filename, char const* funcname, int line ); @@ -29,6 +34,9 @@ void testfw_print_test_desc(); void testfw_print_failure( char const* filename, int line ); void testfw_assertion_count_inc(); void testfw_current_test_assertion_failed(); +#if defined( _WIN32 ) && !defined( TESTFW_NO_SEH ) + void testfw_exception( unsigned int exception_code ); +#endif #endif /* testfw_h */ @@ -150,6 +158,9 @@ static struct int assertions_total; int assertions_failed; testfw_internal_current_test_state_t current_test; + #if defined( _WIN32 ) && defined( _DEBUG ) + int total_leaks; + #endif } testfw_internal_state = {}; @@ -176,8 +187,6 @@ static void testfw_internal_print_progress_divider( char ch, int fail, int total static int testfw_internal_debug_report_hook( int report_type, char* message, int* return_value ) { - static int total_leaks = 0; - *return_value = 0; // Don't break to debugger if( stricmp( message, "Detected memory leaks!\n" ) == 0 ) @@ -203,21 +212,21 @@ static void testfw_internal_print_progress_divider( char ch, int fail, int total for( unsigned int i = 0; i < 5; ++i ) { if( i != 2 && state.lCounts[ i ] ) - TESTFW_PRINTF( "%Id bytes in %Id %hs Blocks.\n", (long long) state.lSizes[ i ], + TESTFW_PRINTF( "%lld bytes in %lld %hs Blocks.\n", (long long) state.lSizes[ i ], (long long) state.lCounts[ i ], block_use_names[ i ] ); } TESTFW_PRINTF( "\n" ); - TESTFW_PRINTF( "High water mark: %Id bytes.\n", (long long)state.lHighWaterCount); - TESTFW_PRINTF( "All allocations: %Id bytes.\n", (long long)state.lTotalCount); + TESTFW_PRINTF( "High water mark: %lld bytes.\n", (long long)state.lHighWaterCount); + TESTFW_PRINTF( "All allocations: %lld bytes.\n", (long long)state.lTotalCount); TESTFW_PRINTF( "\n" ); TESTFW_PRINTF( "===============================================================================\n" ); - TESTFW_PRINTF( "%sMEMORY LEAKS: %d%s\n\n", TESTFW_ANSI_LIGHT_RED, total_leaks, TESTFW_ANSI_RESET ); + TESTFW_PRINTF( "%sMEMORY LEAKS: %d%s\n\n", TESTFW_ANSI_LIGHT_RED, testfw_internal_state.total_leaks, TESTFW_ANSI_RESET ); return 1; // Tell CRT not to print the message } else if( strnicmp( message, " Data: <", 8 ) == 0 ) { - ++total_leaks; + ++testfw_internal_state.total_leaks; return 1; // Tell CRT not to print the message } @@ -283,9 +292,9 @@ void testfw_print_test_desc() } -void testfw_summary( char const* filename, char const* funcname, int line ) +int testfw_summary( char const* filename, char const* funcname, int line ) { - if( testfw_internal_can_not_be_in_test() ) return; + if( testfw_internal_can_not_be_in_test() ) return -1; TESTFW_PRINTF( "\n" ); testfw_internal_print_progress_divider( '=', testfw_internal_state.tests_failed, testfw_internal_state.tests_total ); @@ -305,6 +314,13 @@ void testfw_summary( char const* filename, char const* funcname, int line ) } TESTFW_PRINTF( "\n\n" ); + + #if defined( _WIN32 ) && defined( _DEBUG ) + int result = _CrtDumpMemoryLeaks(); + testfw_internal_state.tests_failed += result ? 1 : 0; + #endif + + return testfw_internal_state.tests_failed; } @@ -333,6 +349,98 @@ void testfw_test_end( char const* filename, char const* funcname, int line ) } +#if defined( _WIN32 ) && !defined( TESTFW_NO_SEH ) + void testfw_exception( unsigned int exception_code ) + { + if( testfw_internal_must_be_in_test() ) return; + + if( !testfw_internal_state.current_test.counted_as_failed ) + { + testfw_internal_state.current_test.counted_as_failed = 1; + ++testfw_internal_state.tests_failed; + } + + char exception_str[ 64 ]; + switch( exception_code ) + { + case EXCEPTION_ACCESS_VIOLATION: + strcpy( exception_str, "EXCEPTION_ACCESS_VIOLATION" ); + break; + case EXCEPTION_DATATYPE_MISALIGNMENT: + strcpy( exception_str, "EXCEPTION_DATATYPE_MISALIGNMENT" ); + break; + case EXCEPTION_BREAKPOINT: + strcpy( exception_str, "EXCEPTION_BREAKPOINT" ); + break; + case EXCEPTION_SINGLE_STEP: + strcpy( exception_str, "EXCEPTION_SINGLE_STEP" ); + break; + case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: + strcpy( exception_str, "EXCEPTION_ARRAY_BOUNDS_EXCEEDED" ); + break; + case EXCEPTION_FLT_DENORMAL_OPERAND: + strcpy( exception_str, "EXCEPTION_FLT_DENORMAL_OPERAND" ); + break; + case EXCEPTION_FLT_DIVIDE_BY_ZERO: + strcpy( exception_str, "EXCEPTION_FLT_DIVIDE_BY_ZERO" ); + break; + case EXCEPTION_FLT_INEXACT_RESULT: + strcpy( exception_str, "EXCEPTION_FLT_INEXACT_RESULT" ); + break; + case EXCEPTION_FLT_INVALID_OPERATION: + strcpy( exception_str, "EXCEPTION_FLT_INVALID_OPERATION" ); + break; + case EXCEPTION_FLT_OVERFLOW: + strcpy( exception_str, "EXCEPTION_FLT_OVERFLOW" ); + break; + case EXCEPTION_FLT_STACK_CHECK: + strcpy( exception_str, "EXCEPTION_FLT_STACK_CHECK" ); + break; + case EXCEPTION_FLT_UNDERFLOW: + strcpy( exception_str, "EXCEPTION_FLT_UNDERFLOW" ); + break; + case EXCEPTION_INT_DIVIDE_BY_ZERO: + strcpy( exception_str, "EXCEPTION_INT_DIVIDE_BY_ZERO" ); + break; + case EXCEPTION_INT_OVERFLOW: + strcpy( exception_str, "EXCEPTION_INT_OVERFLOW" ); + break; + case EXCEPTION_PRIV_INSTRUCTION: + strcpy( exception_str, "EXCEPTION_PRIV_INSTRUCTION" ); + break; + case EXCEPTION_IN_PAGE_ERROR: + strcpy( exception_str, "EXCEPTION_IN_PAGE_ERROR" ); + break; + case EXCEPTION_ILLEGAL_INSTRUCTION: + strcpy( exception_str, "EXCEPTION_ILLEGAL_INSTRUCTION" ); + break; + case EXCEPTION_NONCONTINUABLE_EXCEPTION: + strcpy( exception_str, "EXCEPTION_NONCONTINUABLE_EXCEPTION" ); + break; + case EXCEPTION_STACK_OVERFLOW: + strcpy( exception_str, "EXCEPTION_STACK_OVERFLOW" ); + break; + case EXCEPTION_INVALID_DISPOSITION: + strcpy( exception_str, "EXCEPTION_INVALID_DISPOSITION" ); + break; + case EXCEPTION_GUARD_PAGE: + strcpy( exception_str, "EXCEPTION_GUARD_PAGE" ); + break; + case EXCEPTION_INVALID_HANDLE: + strcpy( exception_str, "EXCEPTION_INVALID_HANDLE" ); + break; + default: + sprintf( exception_str, "%X", exception_code ); + } + + testfw_print_test_desc(); + TESTFW_PRINTF( "\n%s%s(%d): %sFAILED:%s\n", TESTFW_ANSI_GREY, testfw_internal_state.current_test.file, testfw_internal_state.current_test.line, TESTFW_ANSI_LIGHT_RED, + TESTFW_ANSI_RESET ); + TESTFW_PRINTF( "\n %sEXCEPTION( %s%s%s )%s\n", TESTFW_ANSI_CYAN, TESTFW_ANSI_WHITE, exception_str, + TESTFW_ANSI_CYAN, TESTFW_ANSI_RESET ); + } +#endif + void testfw_current_test_assertion_failed() { if( testfw_internal_must_be_in_test() ) return; @@ -435,4 +543,4 @@ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------ -*/ +*/ \ No newline at end of file diff --git a/thread.h b/thread.h index 22eaafd..d9d173c 100644 --- a/thread.h +++ b/thread.h @@ -576,6 +576,15 @@ struct thread_queue_t #ifdef THREAD_IMPLEMENTATION #undef THREAD_IMPLEMENTATION +#ifndef THREAD_ASSERT + #undef _CRT_NONSTDC_NO_DEPRECATE + #define _CRT_NONSTDC_NO_DEPRECATE + #undef _CRT_SECURE_NO_WARNINGS + #define _CRT_SECURE_NO_WARNINGS + #include + #define THREAD_ASSERT( expression, message ) assert( ( expression ) && ( message ) ) +#endif + #if defined( _WIN32 ) @@ -591,8 +600,10 @@ struct thread_queue_t #define _WINSOCKAPI_ #pragma warning( push ) + #pragma warning( disable: 4619 ) #pragma warning( disable: 4668 ) // 'symbol' is not defined as a preprocessor macro, replacing with '0' for 'directives' - #pragma warning( disable: 4255 ) + #pragma warning( disable: 4768 ) // __declspec attributes before linkage specification are ignored + #pragma warning( disable: 4255 ) // 'function' : no function prototype given: converting '()' to '(void)' #include #pragma warning( pop ) @@ -618,10 +629,6 @@ struct thread_queue_t #endif -#ifndef NDEBUG - #include -#endif - thread_id_t thread_current_thread_id( void ) { @@ -857,6 +864,7 @@ struct thread_internal_signal_t CONDITION_VARIABLE condition; int value; #else + #pragma message( "Warning: _WIN32_WINNT < 0x0600 - condition variables not available" ) HANDLE event; #endif @@ -966,10 +974,10 @@ int thread_signal_wait( thread_signal_t* signal, int timeout_ms ) EnterCriticalSection( &internal->mutex ); while( internal->value == 0 ) { - int res = SleepConditionVariableCS( &internal->condition, &internal->mutex, timeout_ms < 0 ? INFINITE : timeout_ms ); + BOOL res = SleepConditionVariableCS( &internal->condition, &internal->mutex, timeout_ms < 0 ? INFINITE : timeout_ms ); if( !res && GetLastError() == ERROR_TIMEOUT ) { timed_out = 1; break; } } - if( !timed_out ) internal->value = 0; + internal->value = 0; LeaveCriticalSection( &internal->mutex ); return !timed_out; #else @@ -1393,18 +1401,19 @@ int thread_queue_produce( thread_queue_t* queue, void* value, int timeout_ms ) #ifndef NDEBUG if( thread_atomic_int_compare_and_swap( &queue->id_produce_is_set, 0, 1 ) == 0 ) queue->id_produce = thread_current_thread_id(); - assert( thread_current_thread_id() == queue->id_produce ); + THREAD_ASSERT( thread_current_thread_id() == queue->id_produce, "thread_queue_produce called from multiple threads" ); #endif - if( thread_atomic_int_load( &queue->count ) == queue->size ) + while( thread_atomic_int_load( &queue->count ) == queue->size ) // TODO: fix signal so that this can be an "if" instead of "while" { if( timeout_ms == 0 ) return 0; - thread_signal_wait( &queue->space_open, timeout_ms == THREAD_QUEUE_WAIT_INFINITE ? THREAD_SIGNAL_WAIT_INFINITE : timeout_ms ); + if( thread_signal_wait( &queue->space_open, timeout_ms == THREAD_QUEUE_WAIT_INFINITE ? THREAD_SIGNAL_WAIT_INFINITE : timeout_ms ) == 0 ) + return 0; } int tail = thread_atomic_int_inc( &queue->tail ); queue->values[ tail % queue->size ] = value; if( thread_atomic_int_inc( &queue->count ) == 0 ) thread_signal_raise( &queue->data_ready ); - return 0; + return 1; } @@ -1413,12 +1422,13 @@ void* thread_queue_consume( thread_queue_t* queue, int timeout_ms ) #ifndef NDEBUG if( thread_atomic_int_compare_and_swap( &queue->id_consume_is_set, 0, 1 ) == 0 ) queue->id_consume = thread_current_thread_id(); - assert( thread_current_thread_id() == queue->id_consume ); + THREAD_ASSERT( thread_current_thread_id() == queue->id_consume, "thread_queue_consume called from multiple threads" ); #endif - if( thread_atomic_int_load( &queue->count ) == 0 ) + while( thread_atomic_int_load( &queue->count ) == 0 ) // TODO: fix signal so that this can be an "if" instead of "while" { if( timeout_ms == 0 ) return NULL; - thread_signal_wait( &queue->data_ready, THREAD_SIGNAL_WAIT_INFINITE ); + if( thread_signal_wait( &queue->data_ready, timeout_ms == THREAD_QUEUE_WAIT_INFINITE ? THREAD_SIGNAL_WAIT_INFINITE : timeout_ms ) == 0 ) + return NULL; } int head = thread_atomic_int_inc( &queue->head ); void* retval = queue->values[ head % queue->size ];