int main() { MemoryPool *pool = memory_pool_init(sizeof(int)*100); int values[] = {1, 2, 3, 4, 5, 6, 7}; BinarySearchTree *tree = construct_minimal_height_tree( values, sizeof(values)/sizeof(int), pool); assert(3 == tree_height(tree)); memory_pool_free(pool); return 0; }
void memory_pool_create(memory_pool_t *pool, void * storage, int count, int block_size){ node_t *free_blocks = (node_t*) pool; char *mem_ptr = (char *) storage; int i; // create singly linked list of all available blocks free_blocks->next = NULL; for (i = 0 ; i < count ; i++){ memory_pool_free(pool, mem_ptr); mem_ptr += block_size; } }
int main() { MemoryPool *pool = memory_pool_init(sizeof(100) * 100); LinkedList *list = NULL; list = linked_list_append(list, memory_pool_alloc_int(pool, 1)); list = linked_list_append(list, memory_pool_alloc_int(pool, 2)); list = linked_list_append(list, memory_pool_alloc_int(pool, 3)); list = linked_list_append(list, memory_pool_alloc_int(pool, 2)); list = linked_list_append(list, memory_pool_alloc_int(pool, 4)); linked_list_print(list); remove_duplicates_in_place(list); linked_list_print(list); list = linked_list_free(list); memory_pool_free(pool); return 0; }
static void *thread_func(void *arg) { memory_pool_t* pool = arg; while (!exit_all) { struct poolobj* e[5]; bzero(e, sizeof(e)); assert(sizeof(e) == (sizeof(void*) * 5)); int rand_n = rand() % 6; int i; for (i = 0; i < rand_n; i++) { e[i] = memory_pool_alloc(pool); } sleep(rand_t()); for (i = 0; i < rand_n; i++) { memory_pool_free(pool, e[i]); } sleep(rand_t()); } return 0; }
int machine_reset( int hard_reset ) { size_t i; int error; /* Clear poke list (undoes effects of active pokes on Spectrum memory) */ pokemem_clear(); sound_ay_reset(); tape_stop(); memory_pool_free(); machine_current->ram.romcs = 0; machine_set_variable_timings( machine_current ); memory_reset(); /* Do the machine-specific bits, including loading the ROMs */ error = machine_current->reset(); if( error ) return error; module_reset( hard_reset ); error = machine_current->memory_map(); if( error ) return error; /* Set up the contention array */ for( i = 0; i < machine_current->timings.tstates_per_frame; i++ ) { ula_contention[ i ] = machine_current->ram.contend_delay( i ); ula_contention_no_mreq[ i ] = machine_current->ram.contend_delay_no_mreq( i ); } /* Update the disk menu items */ ui_menu_disk_update(); /* clear out old display image ready for new one */ display_refresh_all(); return 0; }
int machine_reset( int hard_reset ) { size_t i; int error; sound_ay_reset(); tape_stop(); memory_pool_free(); machine_current->ram.romcs = 0; machine_set_variable_timings( machine_current ); /* Do the machine-specific bits, including loading the ROMs */ error = machine_current->reset(); if( error ) return error; module_reset( hard_reset ); error = machine_current->memory_map(); if( error ) return error; /* Set up the contention array */ for( i = 0; i < machine_current->timings.tstates_per_frame; i++ ) { ula_contention[ i ] = machine_current->ram.contend_delay( i ); ula_contention_no_mreq[ i ] = machine_current->ram.contend_delay_no_mreq( i ); } /* Check for an Interface I ROM */ ui_statusbar_update( UI_STATUSBAR_ITEM_MICRODRIVE, UI_STATUSBAR_STATE_NOT_AVAILABLE ); /* Update the disk menu items */ ui_menu_disk_update(); /* clear out old display image ready for new one */ display_refresh_all(); return 0; }
int main() { MemoryPool *pool = memory_pool_init(sizeof(100)*100); LinkedList *number_a = NULL; number_a = linked_list_append(number_a, memory_pool_alloc_int(pool, 3)); number_a = linked_list_append(number_a, memory_pool_alloc_int(pool, 1)); number_a = linked_list_append(number_a, memory_pool_alloc_int(pool, 5)); linked_list_print(number_a); LinkedList *number_b = NULL; number_b = linked_list_append(number_b, memory_pool_alloc_int(pool, 5)); number_b = linked_list_append(number_b, memory_pool_alloc_int(pool, 9)); number_b = linked_list_append(number_b, memory_pool_alloc_int(pool, 2)); number_b = linked_list_append(number_b, memory_pool_alloc_int(pool, 1)); linked_list_print(number_b); LinkedList *result = add(number_a, number_b, pool); linked_list_print(result); linked_list_free(result); linked_list_free(number_b); linked_list_free(number_a); memory_pool_free(pool); return 0; }
int main() { MemoryPool *pool = memory_pool_init(sizeof(int) * 100); BinarySearchTree *tree = binary_search_tree_init(int_compare_by_pointer, int_compare_by_pointer); binary_search_tree_insert(tree, memory_pool_alloc_int(pool, 2), NULL); binary_search_tree_insert(tree, memory_pool_alloc_int(pool, 1), NULL); binary_search_tree_insert(tree, memory_pool_alloc_int(pool, 3), NULL); int height = 0; assert(true == tree_balanced(tree)); tree = binary_search_tree_free(tree); // Construct an almost balanced tree. tree = binary_search_tree_init(int_compare_by_pointer,int_compare_by_pointer); int keys[] = {5, 3, 6, 2, 4, 1}; for (size_t i = 0; i < sizeof(keys)/sizeof(int); ++i) { binary_search_tree_insert(tree, memory_pool_alloc_int(pool, keys[i]), NULL); } assert(false == tree_balanced(tree)); tree = binary_search_tree_free(tree); pool = memory_pool_free(pool); }
void btstack_memory_rfcomm_channel_free(void *rfcomm_channel){ memory_pool_free(&rfcomm_channel_pool, rfcomm_channel); }
void btstack_memory_l2cap_channel_free(void *l2cap_channel){ memory_pool_free(&l2cap_channel_pool, l2cap_channel); }
void btstack_memory_l2cap_service_free(void *l2cap_service){ memory_pool_free(&l2cap_service_pool, l2cap_service); }
void le_btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){ memory_pool_free(&l2cap_service_pool, l2cap_service); }
void btstack_memory_sm_lookup_entry_free(sm_lookup_entry_t *sm_lookup_entry){ memory_pool_free(&sm_lookup_entry_pool, sm_lookup_entry); }
void btstack_memory_gatt_subclient_free(gatt_subclient_t *gatt_subclient){ memory_pool_free(&gatt_subclient_pool, gatt_subclient); }
void btstack_memory_rfcomm_service_free(void *rfcomm_service){ memory_pool_free(&rfcomm_service_pool, rfcomm_service); }
void btstack_memory_rfcomm_multiplexer_free(void *rfcomm_multiplexer){ memory_pool_free(&rfcomm_multiplexer_pool, rfcomm_multiplexer); }
/* * The event scheduler and resource ticker. */ static void run_ticker (db_t *database) { time_t last = time(NULL); int events = 0, sleeps = 0; debug(DEBUG_TICKER, "running"); while (!finish) { /* set up memory pool */ struct memory_pool *pool = memory_pool_new(); time_t now = time(NULL); int secs = now - last; if (secs >= TICKER_LOADAVG_TIME) { debug(DEBUG_TICKER, "ticker load: %.2f (%d events/min)", 1 - sleep_time / 1000000.0 * sleeps / secs, 60 * events / secs); events = sleeps = 0; last = now; #ifdef DEBUG_MALLOC CHECK_LEAKS(); #endif } if (reload) { debug(DEBUG_TICKER, "reload config"); reload = 0; /* read config file */ config_read_file(config_file); fetch_config_values(); log_handler_set_file(debug_log, debug_logfile); log_handler_set_file(error_log, error_logfile); log_handler_set_file(msg_log, msg_logfile); } try { char resource_timestamp[TIMESTAMP_LEN]; char timestamp[TIMESTAMP_LEN]; db_result_t *next_result = NULL; const char *next_timestamp = make_timestamp_gm(resource_timestamp, tick_next_event()); const char *next_db_eventID; int next_eventType; int i; /* check each event queue to find the next event to process */ debug(DEBUG_EVENTS, "start loop, next resource event"); for (i = 0; i < eventTableSize; ++i) { db_result_t *result; /* get only the next non-blocked event, if its timestamp is smaller than the smallest found timestamp */ debug(DEBUG_EVENTS, "query event table: %s", eventTableList[i].table); // debug(DEBUG_TICKER, "query event table: %s", eventTableList[i].table); result = db_query(database, "SELECT * FROM %s WHERE blocked = 0 AND end < '%s' " "ORDER BY end ASC, %s ASC LIMIT 0,1", eventTableList[i].table, next_timestamp, eventTableList[i].id_field); if (db_result_num_rows(result)) /* is there an earlier event? */ { /* extract this earlier event's needed data */ db_result_next_row(result); next_result = result; /* remember the earlier one */ next_timestamp = db_result_get_string(result, "end"); next_db_eventID = db_result_get_string(result, eventTableList[i].id_field); next_eventType = i; } } if (strcmp(next_timestamp, make_timestamp_gm(timestamp, time(NULL))) > 0) { debug(DEBUG_EVENTS, "no event pending, sleep"); ++sleeps; usleep(sleep_time); } else { debug(DEBUG_TICKER, "event: scheduled %s, now %s", next_timestamp, timestamp); /* check which handler to call (resource ticker or event handler) */ if (next_result) { /* found an event in the event tables: block the event */ debug(DEBUG_EVENTS, "block event: %s", next_db_eventID); ++events; db_query(database, "UPDATE %s SET blocked = 1 WHERE %s = %s", eventTableList[next_eventType].table, eventTableList[next_eventType].id_field, next_db_eventID); /* call handler and delete event */ eventTableList[next_eventType].handler(database, next_result); debug(DEBUG_EVENTS_DELETE, "delete event: %s", next_db_eventID); db_query(database, "DELETE FROM %s WHERE %s = %s", eventTableList[next_eventType].table, eventTableList[next_eventType].id_field, next_db_eventID); } else { /* next event is resource tick: call resource ticker */ debug(DEBUG_TICKER, "resource tick %s", resource_timestamp); resource_ticker(database, tick_advance()); debug(DEBUG_TICKER, "resource tick ended"); tick_log(); /* log last successful update */ } } } catch (BAD_ARGUMENT_EXCEPTION) { warning("%s", except_msg); } catch (SQL_EXCEPTION) { warning("%s", except_msg); } catch (GENERIC_EXCEPTION) { warning("%s", except_msg); } catch (DB_EXCEPTION) { block_ticker(except_msg); } catch (NULL) { error("%s", except_msg); } end_try; memory_pool_free(pool); } debug(DEBUG_TICKER, "end"); }
static void mempool_eqent_free(struct event_queue_entry* e) { memory_pool_free(mempool_eqent, e); }
void le_btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){ memory_pool_free(&l2cap_channel_pool, l2cap_channel); }
void btstack_memory_db_mem_device_name_free(void *db_mem_device_name){ memory_pool_free(&db_mem_device_name_pool, db_mem_device_name); }
void btstack_memory_whitelist_entry_free(whitelist_entry_t *whitelist_entry){ memory_pool_free(&whitelist_entry_pool, whitelist_entry); }
void btstack_memory_db_mem_device_link_key_free(void *db_mem_device_link_key){ memory_pool_free(&db_mem_device_link_key_pool, db_mem_device_link_key); }
void le_btstack_memory_hci_connection_free(hci_connection_t *hci_connection){ memory_pool_free(&hci_connection_pool, hci_connection); }
void btstack_memory_db_mem_service_free(void *db_mem_service){ memory_pool_free(&db_mem_service_pool, db_mem_service); }
void symbols_free(struct _symbols *symbols) { memory_pool_free(symbols->memory_pool); symbols->memory_pool = NULL; }
void btstack_memory_hci_connection_free(void *hci_connection){ memory_pool_free(&hci_connection_pool, hci_connection); }