static int do_slabs_newslab(slabs_t* pst, const unsigned int id) { slabclass_t *p = &pst->slabclass[id]; //int len = settings.slab_reassign ? settings.item_size_max // : p->size * p->perslab; int len = p->size * p->perslab; char *ptr; if ((pst->mem_limit && pst->mem_malloced + len > pst->mem_limit && p->slabs > 0) || (grow_slab_list(pst, id) == 0) || ((ptr = memory_allocate(pst, (size_t)len)) == 0)) { //MEMCACHED_SLABS_SLABCLASS_ALLOCATE_FAILED(id); return 0; } memset(ptr, 0, (size_t)len); p->end_page_ptr = ptr; p->end_page_free = p->perslab; p->slab_list[p->slabs++] = ptr; pst->mem_malloced += len; //MEMCACHED_SLABS_SLABCLASS_ALLOCATE(id); return 1; }
void stream_determine_binary_mode(stream_t* stream, size_t num) { char fixed_buffer[32]; char* buf; size_t cur; size_t actual_read, i; if (!(stream->mode & STREAM_IN) || stream_is_sequential(stream)) return; if (!num) num = 8; buf = (num <= sizeof(fixed_buffer)) ? fixed_buffer : memory_allocate(0, num, 0, MEMORY_TEMPORARY); memset(buf, 32, num); cur = stream_tell(stream); actual_read = stream_read(stream, buf, num); stream_seek(stream, (ssize_t)cur, STREAM_SEEK_BEGIN); stream->mode &= ~STREAM_BINARY; for (i = 0; i < actual_read; ++i) { //TODO: What about UTF-8? if (((buf[i] < 0x20) && (buf[i] != 0x09) && (buf[i] != 0x0a) && (buf[i] != 0x0d)) || (buf[i] > 0x7e)) { stream->mode |= STREAM_BINARY; break; } } if (buf != fixed_buffer) memory_deallocate(buf); }
static int do_slabs_newslab(const unsigned int id) { slabclass_t *p = &slabclass[id]; int len = settings.slab_reassign ? settings.item_size_max : p->size * p->perslab; char *ptr; DBG_INFO(DBG_ASSOC_HOPSCOTCH, "%s:%d: mem_limit = %u, len = %u\n", __func__, __LINE__, (unsigned int)mem_limit, (unsigned int)len); DBG_INFO(DBG_ASSOC_HOPSCOTCH, "%s:%d: p->slabs = %u\n", __func__, __LINE__, p->slabs); if ((mem_limit && mem_malloced + len > mem_limit && p->slabs > 0) || (grow_slab_list(id) == 0) || ((ptr = memory_allocate((size_t)len)) == 0)) { mem_limit_reached = true; MEMCACHED_SLABS_SLABCLASS_ALLOCATE_FAILED(id); DBG_INFO(DBG_ASSOC_HOPSCOTCH, "%s:%d: mem_limit = %u, mem_malloced = %u, len = %d\n", __func__, __LINE__, (unsigned int)mem_limit, (unsigned int)mem_malloced, len); DBG_INFO(DBG_ASSOC_HOPSCOTCH, "%s:%d: returning 0\n", __func__, __LINE__); return 0; } memset(ptr, 0, (size_t)len); // TODO: Should this be commented? split_slab_page_into_freelist(ptr, id); p->slab_list[p->slabs++] = ptr; #ifdef HOPSCOTCH_CLOCK p->clock_max = p->slabs * p->perslab; #endif mem_malloced += len; MEMCACHED_SLABS_SLABCLASS_ALLOCATE(id); return 1; }
static int do_slabs_newslab(const unsigned int id) { slabclass_t *p = &slabclass[id]; slabclass_t *g = &slabclass[SLAB_GLOBAL_PAGE_POOL]; int len = settings.slab_reassign ? settings.item_size_max : p->size * p->perslab; char *ptr; if ((mem_limit && mem_malloced + len > mem_limit && p->slabs > 0 && g->slabs == 0)) { mem_limit_reached = true; MEMCACHED_SLABS_SLABCLASS_ALLOCATE_FAILED(id); return 0; } if ((grow_slab_list(id) == 0) || (((ptr = get_page_from_global_pool()) == NULL) && ((ptr = memory_allocate((size_t)len)) == 0))) { MEMCACHED_SLABS_SLABCLASS_ALLOCATE_FAILED(id); return 0; } memset(ptr, 0, (size_t)len); split_slab_page_into_freelist(ptr, id); p->slab_list[p->slabs++] = ptr; MEMCACHED_SLABS_SLABCLASS_ALLOCATE(id); return 1; }
render_shader_t* render_vertexshader_allocate(void) { render_shader_t* shader = memory_allocate(HASH_RENDER, sizeof(render_shader_t), 16, MEMORY_PERSISTENT); render_vertexshader_initialize(shader); return shader; }
/* * Uses FCFS to simulate process arrival. */ void long_term_scheduler() { // If there is nothing is the NEW queue, then no decisions are to be made. if (new_queue.empty()){ if (DEBUG) cout << "DEBUG: (long_term_scheduler): New queue is empty." << endl; return; } PCB next_process = new_queue.front(); // Check if memory is available for the next process in line. if (next_process.get_size() <= available_memory) { if (DEBUG) cout << "DEBUG: (long_term_scheduler): available_memory " << available_memory << endl; if (DEBUG) cout << "DEBUG: (long_term_scheduler): allocating Process " << next_process.get_id() << " with size " << next_process.get_size() << endl; available_memory -= next_process.get_size(); if (DEBUG) cout << "DEBUG: (long_term_scheduler): allocated memory, available_memory now " << available_memory << endl; memory_allocate(next_process); // Remove the process from the NEW queue new_queue.erase(new_queue.begin()); // Change the process to READY and move to the READY queue next_process.set_state("READY"); ready_queue.push_back(next_process); } }
DECLARE_TEST(stacktrace, resolve) { #define TEST_DEPTH 64 void* trace[TEST_DEPTH]; size_t num_frames; char* buffer; string_t resolved; if (system_platform() == PLATFORM_PNACL) return 0; num_frames = stacktrace_capture(trace, TEST_DEPTH, 0); EXPECT_GT(num_frames, 3); buffer = memory_allocate(0, 1024, 0, MEMORY_TEMPORARY); resolved = stacktrace_resolve(buffer, 1024, trace, num_frames, 0); EXPECT_NE(resolved.str, 0); EXPECT_NE(resolved.length, 0); //log_infof(HASH_TEST, STRING_CONST("Resolved stack trace:\n%.*s"), (int)resolved.length, // resolved.str); #if !FOUNDATION_PLATFORM_ANDROID && !(FOUNDATION_PLATFORM_WINDOWS && (FOUNDATION_COMPILER_GCC || FOUNDATION_COMPILER_CLANG)) EXPECT_NE(string_find_string(resolved.str, resolved.length, STRING_CONST("stacktraceresolve_fn"), 0), STRING_NPOS); EXPECT_NE(string_find_string(resolved.str, resolved.length, STRING_CONST("main"), 0), STRING_NPOS); #endif memory_deallocate(buffer); return 0; }
blast_reader_t* blast_reader_open(string_t source) { void* addr; int64_t size; blast_reader_t* reader; int fd; fd = _open(source.str, O_RDONLY); if (fd == 0) return 0; size = _lseek(fd, 0, SEEK_END); if (size <= 0) { _close(fd); return 0; } _lseek(fd, 0, SEEK_SET); addr = memory_allocate(HASH_BLAST, (size_t)size, 0, MEMORY_PERSISTENT); _read(fd, addr, (int)size); _close(fd); /*addr = mmap( 0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0 ); if( addr == MAP_FAILED ) { int err = system_error(); log_warnf( HASH_BLAST, WARNING_SYSTEM_CALL_FAIL, "Unable to mmap file '%s' (%d) size %d: %s (%d)", source, fd, size, system_error_message( err ), err ); close( fd ); return 0; } log_infof( HASH_BLAST, "Mapped '%s' size %lld to memory region 0x%" PRIfixPTR, source, size, addr );*/ reader = memory_allocate(HASH_BLAST, sizeof(blast_reader_t), 0, MEMORY_PERSISTENT | MEMORY_ZERO_INITIALIZED); string_const_t filename = path_file_name(STRING_ARGS(source)); reader->name = string_clone(STRING_ARGS(filename)); reader->data = addr; //reader->id = fd; reader->size = (uint64_t)size; reader->cache = blast_reader_cache; reader->uncache = blast_reader_uncache; reader->map = blast_reader_map; reader->unmap = blast_reader_unmap; return reader; }
static void _log_outputf( uint64_t context, int severity, const char* prefix, const char* format, va_list list, void* std ) { log_timestamp_t timestamp = _log_make_timestamp(); uint64_t tid = thread_id(); unsigned int pid = thread_hardware(); int need, more, remain, size = 383; char local_buffer[385]; char* buffer = local_buffer; while(1) { //This is guaranteed to always fit in minimum size of 383 bytes defined above, so need is always > 0 if( _log_prefix ) need = snprintf( buffer, size, "[%u:%02u:%02u.%03u] <%" PRIx64 ":%d> %s", timestamp.hours, timestamp.minutes, timestamp.seconds, timestamp.milliseconds, tid, pid, prefix ); else need = snprintf( buffer, size, "%s", prefix ); remain = size - need; { va_list clist; va_copy( clist, list ); more = vsnprintf( buffer + need, remain, format, clist ); va_end( clist ); } if( ( more > -1 ) && ( more < remain ) ) { buffer[need+more] = '\n'; buffer[need+more+1] = 0; #if FOUNDATION_PLATFORM_WINDOWS OutputDebugStringA( buffer ); #endif #if FOUNDATION_PLATFORM_ANDROID if( _log_stdout ) __android_log_write( ANDROID_LOG_DEBUG + severity - 1, environment_application()->short_name, buffer ); #else if( _log_stdout && std ) fprintf( std, "%s", buffer ); #endif if( _log_callback ) _log_callback( context, severity, buffer ); break; } if( ( more > -1 ) && ( need > -1 ) ) size = more + need + 1; else size *= 2; if( buffer != local_buffer ) memory_deallocate( buffer ); buffer = memory_allocate( size + 2, 0, MEMORY_TEMPORARY ); } if( buffer != local_buffer ) memory_deallocate( buffer ); }
stream_t* pipe_allocate( void ) { stream_pipe_t* pipestream = memory_allocate( HASH_STREAM, sizeof( stream_pipe_t ), 8, MEMORY_PERSISTENT ); pipe_initialize( pipestream ); return (stream_t*)pipestream; }
process_t* process_allocate() { process_t* proc = memory_allocate( 0, sizeof( process_t ), 0, MEMORY_PERSISTENT ); process_initialize( proc ); return proc; }
stream_t* buffer_stream_allocate( void* buffer, unsigned int mode, uint64_t size, uint64_t capacity, bool adopt, bool grow ) { stream_buffer_t* stream = memory_allocate( HASH_STREAM, sizeof( stream_buffer_t ), 8, MEMORY_PERSISTENT ); buffer_stream_initialize( stream, buffer, mode, size, capacity, adopt, grow ); return (stream_t*)stream; }
static int test_profile_initialize(void) { profile_set_output(test_profile_output); _test_profile_buffer = memory_allocate(0, TEST_PROFILE_BUFFER_SIZE, 0, MEMORY_PERSISTENT); return 0; }
stream_t* ringbuffer_stream_allocate( unsigned int buffer_size, uint64_t total_size ) { stream_ringbuffer_t* bufferstream = memory_allocate( 0, sizeof( stream_ringbuffer_t ) + buffer_size, 0, MEMORY_PERSISTENT ); ringbuffer_stream_initialize( bufferstream, buffer_size, total_size ); return (stream_t*)bufferstream; }
int NetMemAllcate(unsigned char *recvBuf,unsigned char *pFileStore) { recvBuf =(unsigned char*) memory_allocate(SystemPartition,REC_BUFFER_SIZE); /*分配数据包接收缓冲区*/ if(recvBuf==NULL) { return -1; } pFileStore =(unsigned char*) memory_allocate(SystemPartition,FILE_SIZE); /*分配文件接收缓冲区*/ if(pFileStore==NULL) { return -1; } memset(pFileStore,0,FILE_SIZE); memset(recvBuf,0,REC_BUFFER_SIZE); return 1; }
static unsigned int* _random_allocate_buffer( void ) { unsigned int* buffer = memory_allocate( sizeof( unsigned int ) * ( RANDOM_STATE_SIZE + 1 ), 0, MEMORY_PERSISTENT ); _random_seed_buffer( buffer ); buffer[RANDOM_STATE_SIZE] = 0; array_push( _random_state, buffer ); return buffer; }
render_vertex_decl_t* render_vertex_decl_allocate_vlist(render_vertex_format_t format, render_vertex_attribute_id attribute, va_list list) { render_vertex_decl_t* decl = memory_allocate(HASH_RENDER, sizeof(render_vertex_decl_t), 0, MEMORY_PERSISTENT | MEMORY_ZERO_INITIALIZED); render_vertex_decl_initialize_vlist(decl, format, attribute, list); return decl; }
ringbuffer_t* ringbuffer_allocate( unsigned int size ) { ringbuffer_t* buffer = memory_allocate( 0, sizeof( ringbuffer_t ) + size, 0, MEMORY_PERSISTENT ); ringbuffer_initialize( buffer, size ); return buffer; }
/* Initialize the write io handle * Returns 1 if successful or -1 on error */ int libewf_io_handle_initialize( libewf_io_handle_t **io_handle, liberror_error_t **error ) { static char *function = "libewf_io_handle_initialize"; if( io_handle == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid write io handle.", function ); return( -1 ); } if( *io_handle == NULL ) { *io_handle = (libewf_io_handle_t *) memory_allocate( sizeof( libewf_io_handle_t ) ); if( io_handle == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create write io handle.", function ); return( -1 ); } if( memory_set( *io_handle, 0, sizeof( libewf_io_handle_t ) ) == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear write io handle.", function ); memory_free( *io_handle ); *io_handle = NULL; return( -1 ); } ( *io_handle )->format = LIBEWF_FORMAT_UNKNOWN; ( *io_handle )->ewf_format = EWF_FORMAT_UNKNOWN; ( *io_handle )->compression_level = EWF_COMPRESSION_UNKNOWN; } return( 1 ); }
/* Initialize the header sections * Returns 1 if successful or -1 on error */ int libewf_header_sections_initialize( libewf_header_sections_t **header_sections, liberror_error_t **error ) { static char *function = "libewf_header_sections_initialize"; if( header_sections == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid header sections.", function ); return( -1 ); } if( *header_sections == NULL ) { *header_sections = (libewf_header_sections_t *) memory_allocate( sizeof( libewf_header_sections_t ) ); if( *header_sections == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create header sections.", function ); return( -1 ); } if( memory_set( *header_sections, 0, sizeof( libewf_header_sections_t ) ) == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear header sections.", function ); memory_free( *header_sections ); *header_sections = NULL; return( -1 ); } ( *header_sections )->header_codepage = LIBEWF_CODEPAGE_ASCII; } return( 1 ); }
string_t stream_read_line(stream_t* stream, char delimiter) { char buffer[128]; char* outbuffer = 0; size_t outsize = 0; size_t cursize = 0; size_t read, i; size_t want_read = 128; if (!(stream->mode & STREAM_IN)) return (string_t) { 0, 0 }; //Need to read one byte at a time since we can't scan back if overreading if (stream_is_sequential(stream)) want_read = 1; while (!stream_eos(stream)) { read = stream->vtable->read(stream, buffer, want_read); if (!read) break; for (i = 0; i < read; ++i) { if (buffer[i] == delimiter) break; } if (cursize + i > outsize) { size_t nextsize; if (!outbuffer) { nextsize = (i >= 32 ? i + 1 : (i > 1 ? i + 1 : 32)); outbuffer = memory_allocate(0, nextsize, 0, MEMORY_PERSISTENT); } else { nextsize = (outsize < 511 ? 512 : outsize + 513); //Always aligns to 512 multiples FOUNDATION_ASSERT(!(nextsize % 512)); outbuffer = memory_reallocate(outbuffer, nextsize, 0, outsize + 1); } outsize = nextsize - 1; } if (i) { memcpy(outbuffer + cursize, buffer, i); //lint !e613 cursize += i; } if (i < read) { if ((i + 1) < read) { //Sequential should never end up here reading one byte at a time FOUNDATION_ASSERT(!stream_is_sequential(stream)); stream_seek(stream, (ssize_t)(1 + i) - (ssize_t)read, STREAM_SEEK_CURRENT); } break; } } if (outbuffer) outbuffer[cursize] = 0; return (string_t) { outbuffer, cursize }; }
static void _atomic_allocate_initialize( uint64_t storagesize ) { if( storagesize < 1024 ) storagesize = BUILD_SIZE_TEMPORARY_MEMORY; _memory_temporary.storage = memory_allocate( 0, storagesize, 16, MEMORY_PERSISTENT ); _memory_temporary.end = pointer_offset( _memory_temporary.storage, storagesize ); _memory_temporary.size = storagesize; _memory_temporary.maxchunk = ( storagesize / 8 ); atomic_storeptr( &_memory_temporary.head, _memory_temporary.storage ); }
static void _atomic_allocate_initialize( uint64_t storagesize ) { if( storagesize < 1024 ) storagesize = BUILD_SIZE_TEMPORARY_MEMORY; _memory_temporary.storage = memory_allocate( storagesize, FOUNDATION_PLATFORM_POINTER_SIZE, MEMORY_PERSISTENT ); _memory_temporary.end = pointer_offset( _memory_temporary.storage, storagesize ); _memory_temporary.head = _memory_temporary.storage; _memory_temporary.size = storagesize; _memory_temporary.maxchunk = ( storagesize / 8 ); }
network_address_t* network_address_clone(const network_address_t* address) { network_address_t* cloned = 0; if (address) { cloned = memory_allocate(HASH_NETWORK, sizeof(network_address_t) + address->address_size, 0, MEMORY_PERSISTENT); memcpy(cloned, address, sizeof(network_address_t) + address->address_size); } return cloned; }
static void* objectmap_thread(void* arg) { objectmap_t* map; object_base_t* objects; int obj; int loop; object_base_t* lookup; map = arg; objects = memory_allocate(0, sizeof(object_base_t) * 512, 16, MEMORY_PERSISTENT | MEMORY_ZERO_INITIALIZED); thread_sleep(10); for (loop = 0; loop < 32; ++loop) { thread_yield(); for (obj = 0; obj < 512; ++obj) { atomic_store32(&objects[obj].ref, 1); objects[obj].id = objectmap_reserve(map); EXPECT_NE_MSGFORMAT(objects[obj].id, 0, "Unable to reserve slot for object num %d", obj); EXPECT_EQ_MSGFORMAT(objectmap_lookup(map, objects[obj].id), 0, "Object %d (%" PRIx64 ") already stored in map in loop %d", obj, objects[obj].id, loop); EXPECT_TRUE(objectmap_set(map, objects[obj].id, objects + obj)); lookup = objectmap_lookup(map, objects[obj].id); EXPECT_NE_MSGFORMAT(lookup, 0, "Object num %d (%" PRIx64 ") not set in map, got null on lookup in loop %d", obj, objects[obj].id, loop); EXPECT_EQ_MSGFORMAT(lookup, objects + obj, "Object %d (%" PRIx64 ") 0x%" PRIfixPTR " was not set at reserved slot in map, got object 0x%" PRIfixPTR " in loop %d", obj, objects[obj].id, (uintptr_t)(objects + obj), (uintptr_t)lookup, loop); } thread_yield(); for (obj = 0; obj < 512; ++obj) { void* raw = map->map[ objects[obj].id & map->mask_index ]; lookup = objectmap_lookup(map, objects[obj].id); EXPECT_NE_MSGFORMAT(lookup, 0, "Object 0x%" PRIfixPTR " num %d (%" PRIx64 ") not set in map, got null on lookup in loop %d (raw 0x%" PRIfixPTR ")", (uintptr_t)(objects + obj), obj, objects[obj].id, loop, (uintptr_t)raw); EXPECT_EQ_MSGFORMAT(lookup, objects + obj, "Object %d (%" PRIx64 ") 0x%" PRIfixPTR " was not set at reserved slot in map, got object 0x%" PRIfixPTR " in loop %d", obj, objects[obj].id, (uintptr_t)(objects + obj), (uintptr_t)lookup, loop); EXPECT_TRUE(objectmap_free(map, objects[obj].id)); lookup = objectmap_lookup(map, objects[obj].id); EXPECT_EQ_MSGFORMAT(lookup, 0, "Object %d (%" PRIx64 ") 0x%" PRIfixPTR " still set in map, got non-null (0x%" PRIfixPTR ") on lookup in loop %d", obj, objects[obj].id, (uintptr_t)(objects + obj), (uintptr_t)lookup, loop); } } memory_deallocate(objects); return 0; }
/* Initialize a value entry * Returns 1 if successful or -1 on error */ int libfvalue_value_entry_initialize( libfvalue_value_entry_t **value_entry, liberror_error_t **error ) { static char *function = "libfvalue_value_entry_initialize"; if( value_entry == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_ARGUMENTS, LIBERROR_ARGUMENT_ERROR_INVALID_VALUE, "%s: invalid value entry.", function ); return( -1 ); } if( *value_entry == NULL ) { *value_entry = (libfvalue_value_entry_t *) memory_allocate( sizeof( libfvalue_value_entry_t ) ); if( *value_entry == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_INSUFFICIENT, "%s: unable to create value entry.", function ); return( -1 ); } if( memory_set( *value_entry, 0, sizeof( libfvalue_value_entry_t ) ) == NULL ) { liberror_error_set( error, LIBERROR_ERROR_DOMAIN_MEMORY, LIBERROR_MEMORY_ERROR_SET_FAILED, "%s: unable to clear value entry.", function ); memory_free( *value_entry ); *value_entry = NULL; return( -1 ); } } return( 1 ); }
static void _load_process_modules() { int error = 0; bool succeeded; HMODULE module_handles[MAX_MOD_HANDLES]; HMODULE* module_handle = module_handles; int module_count = 0; int i; DWORD bytes = 0; MODULEINFO module_info; HANDLE process_handle = GetCurrentProcess(); succeeded = CallEnumProcessModules( process_handle, module_handles, sizeof( module_handles ), &bytes ); if( !succeeded ) { error = GetLastError(); return; } if( bytes > sizeof( module_handles ) ) { module_handle = memory_allocate( bytes, 0, MEMORY_TEMPORARY ); CallEnumProcessModules( process_handle, module_handle, bytes, &bytes ); } module_count = bytes / sizeof( HMODULE ); for( i = 0; i < module_count; ++i ) { char module_name[1024]; char image_name[1024]; char search_path[1024]; char* file_name = 0; uint64_t base_address; CallGetModuleInformation( process_handle, module_handle[i], &module_info, sizeof( module_info ) ); CallGetModuleFileNameEx( process_handle, module_handle[i], image_name, 1024 ); CallGetModuleBaseName( process_handle, module_handle[i], module_name, 1024 ); GetFullPathNameA( image_name, 1024, search_path, &file_name ); *file_name = 0; CallSymSetSearchPath( process_handle, search_path ); base_address = CallSymLoadModule64( process_handle, module_handle[i], image_name, module_name, (uint64_t)((uintptr_t)module_info.lpBaseOfDll), module_info.SizeOfImage ); if( !base_address ) { error = GetLastError(); } } // Free the module handle pointer allocated in case the static array was insufficient. if( module_handle != module_handles ) memory_deallocate( module_handle ); }
static void* custom_allocate(size_t size) { if(test_runner::_memory_fail_threshold > 0 && test_runner::_memory_fail_threshold < g_memory_total_size + size) return 0; else { void* ptr = memory_allocate(size); g_memory_total_size += memory_size(ptr); g_memory_total_count++; return ptr; } }
Node* avl_insert(int k, Node* tree){ /* Lisää uuden solmun puuhun. */ if (tree == NULL){ tree = memory_allocate(tree); tree->key = k; tree->ptrLeft = NULL; tree->ptrRight = NULL; } /* Vasempaan haaraan lisääminen. */ else if (tree->key > k){ if (tree->ptrLeft == NULL) printf("Arvo %d asetetaan solmun %d vasemmanpuoleiseksi lapseksi.\n", k, tree->key); tree->ptrLeft = avl_insert(k, tree->ptrLeft); /* tasapainottaminen: */ if(get_height(tree->ptrLeft) == get_height(tree->ptrRight)+ 2){ if(k < tree->ptrLeft->key){ tree = right_rotate(tree); printf("R-rotaatio\n"); } else{ tree->ptrLeft = left_rotate(tree->ptrLeft); tree = right_rotate(tree); printf("LR-rotaatio\n"); } } } /* Oikeaan haaraan lisääminen */ else if (tree->key < k){ if (tree->ptrRight == NULL) printf("Arvo %d asetetaan solmun %d oikeanpuoleiseksi lapseksi.\n", k, tree->key); tree->ptrRight = avl_insert(k, tree->ptrRight); /* tasapainottaminen: */ if (get_height(tree->ptrRight) == get_height(tree->ptrLeft) + 2){ /* R-rotaatio */ if (k > tree->ptrRight->key){ tree = left_rotate(tree); printf("L-rotaatio\n"); } else{ /* RL-rotaatio */ tree->ptrRight = right_rotate(tree->ptrRight); tree = left_rotate(tree); printf("RL-rotaatio\n"); } } } else{ printf("Arvo on jo aikaisemmin lisätty puuhun!\n"); } /* Korjataan solmum korkeus. */ tree->height = max(get_height(tree->ptrLeft), get_height(tree->ptrRight)) + 1; return (tree); }
font_t font_create(const char* name) { //todo: make this a task FOUNDATION_ASSERT(fs_is_file(name)); stream_t* ttf_stream = fs_open_file(name, STREAM_IN|STREAM_BINARY); FOUNDATION_ASSERT(ttf_stream); const uint64_t ttf_stream_size = stream_size(ttf_stream); unsigned char* ttf_buffer = (unsigned char*) memory_allocate(ttf_stream_size, 4, MEMORY_TEMPORARY); unsigned char* ttf_bitmap = (unsigned char*) memory_allocate(MINT_FONT_BITMAPSIZE*MINT_FONT_BITMAPSIZE, 4, MEMORY_PERSISTENT); const uint64_t read = stream_read(ttf_stream, ttf_buffer, ttf_stream_size); FOUNDATION_ASSERT(read == ttf_stream_size); font_data_t data; stbtt_BakeFontBitmap( ttf_buffer,0, 32.0, ttf_bitmap, MINT_FONT_BITMAPSIZE, MINT_FONT_BITMAPSIZE, 32,96, data.cdata ); TextureCreationInfo info; info.type = Texture2D; info.format = FormatR8; info.width = MINT_FONT_BITMAPSIZE; info.height = MINT_FONT_BITMAPSIZE; data.texture = texture_create(ttf_bitmap, info); array_push_memcpy(s_fontCtx.fonts, &data); memory_deallocate(ttf_bitmap); memory_deallocate(ttf_buffer); stream_deallocate(ttf_stream); return array_size(s_fontCtx.fonts)-1; }