void memory_context_push( uint16_t context_id ) { memory_context_t* context = get_thread_memory_context(); if( !context ) { context = memory_allocate_zero( sizeof( memory_context_t ), 0, MEMORY_PERSISTENT ); set_thread_memory_context( context ); } FOUNDATION_ASSERT_MSG( context->depth < BUILD_SIZE_MEMORY_CONTEXT_DEPTH, "Memory context stack overflow" ); context->context[ context->depth ] = context_id; if( context->depth < BUILD_SIZE_MEMORY_CONTEXT_DEPTH-1 ) ++context->depth; }
void _error_context_push( const char* name, const char* data ) { error_context_t* context = get_thread_error_context(); if( !context ) { context = memory_allocate_zero( sizeof( error_context_t ), 0, MEMORY_PERSISTENT ); set_thread_error_context( context ); } FOUNDATION_ASSERT_MSG( context->depth < BUILD_SIZE_ERROR_CONTEXT_DEPTH, "Error context stack overflow" ); context->frame[ context->depth ].name = name; context->frame[ context->depth ].data = data; if( context->depth < BUILD_SIZE_ERROR_CONTEXT_DEPTH-1 ) ++context->depth; }
static NOINLINE char* _expand_string( hash_t section_current, char* str ) { char* expanded; char* variable; unsigned int var_pos, var_end_pos, variable_length, separator, var_offset; hash_t section, key; expanded = str; var_pos = string_find_string( expanded, "$(", 0 ); while( var_pos != STRING_NPOS ) { var_end_pos = string_find( expanded, ')', var_pos + 2 ); FOUNDATION_ASSERT_MSG( var_end_pos != STRING_NPOS, "Malformed config variable statement" ); variable = string_substr( expanded, var_pos, ( var_end_pos != STRING_NPOS ) ? ( 1 + var_end_pos - var_pos ) : STRING_NPOS ); section = section_current; key = 0; variable_length = string_length( variable ); separator = string_find( variable, ':', 0 ); if( separator != STRING_NPOS ) { if( separator != 2 ) section = hash( variable + 2, separator - 2 ); var_offset = separator + 1; } else { var_offset = 2; } key = hash( variable + var_offset, variable_length - ( var_offset + ( variable[ variable_length - 1 ] == ')' ? 1 : 0 ) ) ); if( expanded == str ) expanded = string_clone( str ); if( section != HASH_ENVIRONMENT ) expanded = string_replace( expanded, variable, config_string( section, key ), false ); else expanded = string_replace( expanded, variable, _expand_environment( key, variable + var_offset ), false ); string_deallocate( variable ); var_pos = string_find_string( expanded, "$(", 0 ); } #if BUILD_ENABLE_DEBUG_CONFIG if( str != expanded ) log_debugf( HASH_CONFIG, "Expanded config value \"%s\" to \"%s\"", str, expanded ); #endif return expanded; }
objectmap_t* objectmap_allocate( unsigned int size ) { objectmap_t* map; FOUNDATION_ASSERT_MSG( size > 2, "Invalid objectmap size" ); if( size <= 2 ) size = 2; map = memory_allocate( 0, sizeof( objectmap_t ) + ( sizeof( void* ) * size ), 16, MEMORY_PERSISTENT ); objectmap_initialize( map, size ); return map; }
unsigned int ringbuffer_write( ringbuffer_t* buffer, const void* source, unsigned int num ) { unsigned int do_write; unsigned int max_write; unsigned int buffer_size; unsigned int offset_read; unsigned int offset_write; FOUNDATION_ASSERT( buffer ); buffer_size = buffer->buffer_size; offset_read = buffer->offset_read; offset_write = buffer->offset_write; if( offset_write >= offset_read ) { max_write = buffer_size - offset_write; if( max_write && !buffer->offset_read ) //Don't read so write aligns to read, then the entire buffer is discarded --max_write; } else max_write = offset_read - offset_write - 1; //Don't read so write aligns to read, then the entire buffer is discarded do_write = num; if( do_write > max_write ) do_write = max_write; if( !do_write ) return 0; if( source ) memcpy( buffer->buffer + offset_write, source, do_write ); offset_write += do_write; if( offset_write == buffer_size ) { FOUNDATION_ASSERT_MSG( buffer->offset_read, "Ring buffer internal failure, discarded entire buffer" ); offset_write = 0; } buffer->offset_write = offset_write; buffer->total_write += do_write; if( ( do_write < num ) && ( offset_write == 0 ) && ( offset_read > 0 ) ) do_write += ringbuffer_write( buffer, pointer_offset_const( source, do_write ), num - do_write ); return do_write; }
void _array_growfn( void* arr, int increment, int factor, int itemsize ) { void** parr = (void**)arr; int capacity = *parr ? ( factor * _array_rawcapacity(*parr) + increment ) : increment; int storage_size = itemsize * capacity; uint64_t header_size = 4ULL * _array_header_size; uint64_t buffer_size = (unsigned int)storage_size + header_size; int* buffer = *parr ? memory_reallocate( _array_raw( *parr ), buffer_size, 0 ) : memory_allocate( buffer_size, 16, MEMORY_PERSISTENT ); FOUNDATION_ASSERT_MSG( buffer, "Failed to reallocate array storage" ); if( buffer ) { buffer[0] = capacity; if( !*parr ) { buffer[1] = 0; buffer[2] = _array_watermark; } *parr = buffer + _array_header_size; } }
void _array_verifyfn( const void* const* arr ) { FOUNDATION_ASSERT_MSG( !(*arr) || ( _array_raw_const(*arr)[2] == _array_watermark ), "Invalid array" ); }