/** * ipsec pool --add - add a new pool */ static void add(char *name, host_t *start, host_t *end, int timeout) { chunk_t start_addr, end_addr, cur_addr; u_int id, count; start_addr = start->get_address(start); end_addr = end->get_address(end); cur_addr = chunk_clonea(start_addr); count = get_pool_size(start_addr, end_addr); if (start_addr.len != end_addr.len || memcmp(start_addr.ptr, end_addr.ptr, start_addr.len) > 0) { fprintf(stderr, "invalid start/end pair specified.\n"); exit(EXIT_FAILURE); } id = create_pool(name, start_addr, end_addr, timeout); printf("allocating %d addresses... ", count); fflush(stdout); db->transaction(db, FALSE); while (TRUE) { db->execute(db, NULL, "INSERT INTO addresses (pool, address, identity, acquired, released) " "VALUES (?, ?, ?, ?, ?)", DB_UINT, id, DB_BLOB, cur_addr, DB_UINT, 0, DB_UINT, 0, DB_UINT, 1); if (chunk_equals(cur_addr, end_addr)) { break; } chunk_increment(cur_addr); } db->commit(db); printf("done.\n"); }
void info_handler(void *arg) { struct config *config = (struct config*)arg; storage_sync(config->api); int pool_items, pool_bytes; get_pool_size(&pool_items, &pool_bytes); log_info("Memory stats: %iMB in %i items in pool. Pool freed.", pool_bytes/1024/1024, pool_items); pool_free(); }
int get_pool_size(const unit *head) { if (head != NULL) { return 1 + get_pool_size(head->next); } else { return 0; } }
int CHashMap::open(char* pool, bool init, int node_total, int bucket_size, int n_chunks, int chunk_size, int key_size, pHashMapGetBucketID get_bucket_id, pHashMapCmpKey cmp_key) { int ret = 0; m_get_bucket_id = get_bucket_id; m_cmp_key = cmp_key; int hash_map_pool_size = get_pool_size(node_total, bucket_size, key_size); int head_size = sizeof(THashMap) - sizeof(BC_MEM_HANDLER[1]); int bucket_total_size = bucket_size * sizeof(BC_MEM_HANDLER); pool_ = pool; pool_tail_ = pool_ + hash_map_pool_size; hash_map_ = (THashMap*)pool_; hash_node_ = (THashNode*)(pool_ + head_size + bucket_total_size); if (init) { init_pool_data(node_total, bucket_size, key_size); } else { if ((ret = verify_pool_data(node_total, bucket_size, key_size)) != 0) { return ret; } } if ((ret = allocator_.open(pool_tail_, init, n_chunks, chunk_size)) != 0) { return ret; } return 0; }
/** * ipsec pool --resize - resize a pool */ static void resize(char *name, host_t *end) { enumerator_t *query; chunk_t old_addr, new_addr, cur_addr; u_int id, count; host_t *old_end; new_addr = end->get_address(end); query = db->query(db, "SELECT id, end FROM pools WHERE name = ?", DB_TEXT, name, DB_UINT, DB_BLOB); if (!query || !query->enumerate(query, &id, &old_addr)) { DESTROY_IF(query); fprintf(stderr, "resizing pool failed.\n"); exit(EXIT_FAILURE); } if (old_addr.len != new_addr.len || memcmp(new_addr.ptr, old_addr.ptr, old_addr.len) < 0) { fprintf(stderr, "shrinking of pools not supported.\n"); query->destroy(query); exit(EXIT_FAILURE); } cur_addr = chunk_clonea(old_addr); count = get_pool_size(old_addr, new_addr) - 1; query->destroy(query); /* Check whether pool is resizable */ old_end = host_create_from_chunk(AF_UNSPEC, old_addr, 0); if (old_end && old_end->is_anyaddr(old_end)) { fprintf(stderr, "pool is not resizable.\n"); old_end->destroy(old_end); exit(EXIT_FAILURE); } DESTROY_IF(old_end); db->transaction(db, FALSE); if (db->execute(db, NULL, "UPDATE pools SET end = ? WHERE name = ?", DB_BLOB, new_addr, DB_TEXT, name) <= 0) { fprintf(stderr, "pool '%s' not found.\n", name); exit(EXIT_FAILURE); } printf("allocating %d new addresses... ", count); fflush(stdout); while (count-- > 0) { chunk_increment(cur_addr); db->execute(db, NULL, "INSERT INTO addresses (pool, address, identity, acquired, released) " "VALUES (?, ?, ?, ?, ?)", DB_UINT, id, DB_BLOB, cur_addr, DB_UINT, 0, DB_UINT, 0, DB_UINT, 1); } db->commit(db); printf("done.\n"); }
// 取HASH_MAP 和CHUNK的内存块尺寸 static size_t get_total_pool_size(int node_total, int bucket_size, int n_chunks, int chunk_size) { return get_pool_size(node_total, bucket_size) + CChunkAllocator::get_pool_size(n_chunks, chunk_size); }