bool MappedMemoryArea::MoveAddressForward(Address const& rAddress, Address& rMovedAddress, s64 Offset) const { if (Offset == 0) { rMovedAddress = rAddress; return true; } TOffset MovedOffset = rAddress.GetOffset(); while (Offset--) { while (true) { auto spCellData = GetCellData(MovedOffset); if (spCellData != nullptr) MovedOffset += spCellData->GetLength(); // TODO: check intoverflow here else ++MovedOffset; if (IsCellPresent(MovedOffset)) break; if (MovedOffset > (m_VirtualBase.GetOffset() + GetSize())) return false; } } rMovedAddress = MakeAddress(MovedOffset); return true; }
bool VirtualMemoryArea::MoveAddressForward(Address const& rAddress, Address& rMovedAddress, s64 Offset) const { TOffset MovedOffset = rAddress.GetOffset() + Offset; if (IsCellPresent(MovedOffset) == false) return false; rMovedAddress = MakeAddress(MovedOffset); return true; }
bool VirtualMemoryArea::GetNearestAddress(Address const& rAddress, Address& rNearestAddress) const { if (IsCellPresent(rAddress.GetOffset()) == false) return false; rNearestAddress = MakeAddress(rAddress.GetOffset()); return true; }
bool VirtualMemoryArea::GetNextAddress(Address const& rAddress, Address& rNextAddress) const { TOffset Offset = rAddress.GetOffset() + 1; if (IsCellPresent(Offset) == false) return false; rNextAddress = MakeAddress(Offset); return true; }
bool MappedMemoryArea::GetNearestAddress(Address const& rAddress, Address& rNearestAddress) const { auto Offset = rAddress.GetOffset(); if (Offset < m_VirtualBase.GetOffset()) { rNearestAddress = m_VirtualBase; return true; } if (GetCellData(Offset) != nullptr) { rNearestAddress = MakeAddress(rAddress.GetOffset()); return true; } bool Found = false; do { // Avoid integer underflow if (Offset == 0x0) return false; --Offset; if (GetCellData(Offset) != nullptr) { Found = true; break; } } while (Offset >= m_VirtualBase.GetOffset()); if (Found == false) return false; rNearestAddress = MakeAddress(Offset); return true; }
// TODO: Check if this function works for every cases bool MappedMemoryArea::MoveAddressBackward(Address const& rAddress, Address& rMovedAddress, s64 Offset) const { if (Offset == 0) { rMovedAddress = rAddress; return true; } Offset = -Offset; TOffset MovedOff = rAddress.GetOffset(); TOffset PrevOff; TOffset MemAreaBegOff = m_VirtualBase.GetOffset(); while (true) { s32 SkippedOff = 0; for (PrevOff = MovedOff - 1; PrevOff >= MemAreaBegOff; --PrevOff) { auto spCellData = GetCellData(PrevOff); if (spCellData != nullptr) { SkippedOff -= spCellData->GetLength(); break; } ++SkippedOff; } if (SkippedOff < 0) SkippedOff = 0; if (SkippedOff == 0) --Offset; if (SkippedOff > Offset) Offset = 0; MovedOff = PrevOff; if (Offset == 0) { rMovedAddress = MakeAddress(MovedOff); return true; } if (MovedOff == MemAreaBegOff) return false; } return false; }
bool MappedMemoryArea::GetNextAddress(Address const& rAddress, Address& rNextAddress) const { TOffset LimitOffset = m_VirtualBase.GetOffset() + GetSize(); for (auto Offset = rAddress.GetOffset() + 1; Offset < LimitOffset; ++Offset) { auto pCurrentCell = GetCellData(Offset); if (pCurrentCell != nullptr) { rNextAddress = MakeAddress(Offset); return true; } } return false; }
Address Medusa::MakeAddress(Loader::SharedPtr pLoader, Architecture::SharedPtr pArch, TOffset Offset) { return MakeAddress(pLoader, pArch, 0x0, Offset); }
Address Medusa::MakeAddress(TBase Base, TOffset Offset) { return MakeAddress(Loader::SharedPtr(), Architecture::SharedPtr(), Base, Offset); }
int HashFunction::MakeAddress (string key, unsigned int depth){ unsigned int hash = Hash(key); return MakeAddress(hash,depth); }
/* * Loads Customer Table * Also inserts corresponding history record * * id - customer id * d_id - district id * w_id - warehouse id */ int Customer(long d_id, long w_id ) { long c_id; long c_d_id; long c_w_id; char c_first[17]; char c_middle[3]; char c_last[17]; char c_street_1[21]; char c_street_2[21]; char c_city[21]; char c_state[3]; char c_zip[10]; char c_phone[17]; char c_since[12]; char c_credit[3]; long c_credit_lim; float c_discount; float c_balance; char c_data[501]; float h_amount; char h_data[25]; int err; printf("Loading Customer for DID=%ld, WID=%ld\n",d_id,w_id); for (c_id=1; c_id<=CUST_PER_DIST; c_id++) { DBT key, data; CUSTOMER_PRIMARY_KEY c_key; CUSTOMER_PRIMARY_DATA c_primdata; HISTORY_PRIMARY_KEY h_key; memset(&c_key, 0, sizeof(CUSTOMER_PRIMARY_KEY)); memset(&c_primdata, 0, sizeof(CUSTOMER_PRIMARY_DATA)); memset(&h_key, 0, sizeof(HISTORY_PRIMARY_KEY)); /* Generate Customer Data */ c_d_id=d_id; c_w_id=w_id; MakeAlphaString( 8, 16, c_first ); c_middle[0]='O'; c_middle[1]='E'; c_middle[2] = '\0'; if (c_id <= 1000) Lastname(c_id-1,c_last); else Lastname(NURand(255,0,999),c_last); MakeAddress( c_street_1, c_street_2, c_city, c_state, c_zip ); MakeNumberString( 16, 16, c_phone ); if (random1(0L,1L)) c_credit[0]='G'; else c_credit[0]='B'; c_credit[1]='C'; c_credit[2]='\0'; c_credit_lim=50000; c_discount=((float)random1(0L,50L))/100.0; c_balance= -10.0; MakeAlphaString(300,500,c_data); /* Prepare for putting into the database */ c_key.C_W_ID = c_w_id; c_key.C_D_ID = c_d_id; c_key.C_ID = c_id; memcpy(&c_primdata.C_FIRST, &c_first, 17); memcpy(&c_primdata.C_MIDDLE, &c_middle, 3); memcpy(&c_primdata.C_LAST, &c_last, 17); memcpy(&c_primdata.C_STREET_1, &c_street_1, 21); memcpy(&c_primdata.C_STREET_2, &c_street_2, 21); memcpy(&c_primdata.C_CITY, &c_city, 21); memcpy(&c_primdata.C_STATE, &c_state, 3); memcpy(&c_primdata.C_ZIP, &c_zip, 10); memcpy(&c_primdata.C_PHONE, &c_phone, 17); memcpy(&c_primdata.C_DATA, &c_data, 501); memcpy(&c_primdata.C_CREDIT, &c_credit, 3); memcpy(c_primdata.C_CREDIT, &c_credit, 3); c_primdata.C_DISCOUNT = c_discount; c_primdata.C_BALANCE = c_balance; memset(&key, 0, sizeof(DBT)); key.data = &c_key; key.size = sizeof(CUSTOMER_PRIMARY_KEY); memset(&data, 0, sizeof(DBT)); data.data = &c_primdata; data.size = sizeof(CUSTOMER_PRIMARY_DATA); if((err=dbp_customer->put(dbp_customer, 0, &key, &data, 0))) { db_error("DB->put", err); return -1; } h_amount = 10.0; MakeAlphaString(12,24,h_data); memset(&key, 0, sizeof(DBT)); key.data = &h_key; key.size = sizeof(HISTORY_PRIMARY_KEY); memset(&data, 0, sizeof(DBT)); h_key.H_C_ID = c_id; h_key.H_C_D_ID = c_d_id; h_key.H_C_W_ID = c_w_id; h_key.H_W_ID = c_w_id; h_key.H_D_ID = c_d_id; h_key.H_AMOUNT = h_amount; memcpy(&h_key.H_DATE, ×tamp, 26); memcpy(&h_key.H_DATA, &h_data, 24); if((err=dbp_history->put(dbp_history, 0, &key, &data, 0))) { db_error("DB->put", err); return -1; } if ( option_debug ) printf( "CID = %ld, LST = %s, P# = %s\n", c_id, c_last, c_phone ); if ( !(c_id % 100) ) { printf("."); if ( !(c_id % 1000) ) printf(" %ld\n",c_id); } } printf("Customer Done.\n"); return 0; }
/* * Loads the District table * w_id - warehouse id */ int District(long w_id) { long d_id; long d_w_id; char d_name[11]; char d_street_1[21]; char d_street_2[21]; char d_city[21]; char d_state[3]; char d_zip[10]; float d_tax; float d_ytd; long d_next_o_id; DBT key, data; DISTRICT_PRIMARY_KEY d_key; DISTRICT_PRIMARY_DATA d_data; int err; printf("Loading District\n"); for (d_id=1; d_id<=DIST_PER_WARE; d_id++) { memset(&d_key, 0, sizeof(DISTRICT_PRIMARY_KEY)); memset(&d_data, 0, sizeof(DISTRICT_PRIMARY_DATA)); d_w_id=w_id; d_ytd=30000.0; d_next_o_id=3001L; d_key.D_W_ID = d_w_id; d_key.D_ID = d_id; d_data.D_YTD = d_ytd; d_data.D_NEXT_O_ID = d_next_o_id; /* Generate District Data */ MakeAlphaString(6L,10L,d_name); MakeAddress( d_street_1, d_street_2, d_city, d_state, d_zip ); d_tax=((float)random1(10L,20L))/100.0; memcpy(&d_data.D_NAME, &d_name, 11); memcpy(&d_data.D_STREET_1, &d_street_1, 21); memcpy(&d_data.D_STREET_2, &d_street_2, 21); memcpy(&d_data.D_CITY, &d_city, 21); memcpy(&d_data.D_STATE, &d_state, 3); memcpy(&d_data.D_ZIP, &d_zip, 10); d_data.D_TAX = d_tax; memset(&key, 0, sizeof(DBT)); key.data = &d_key; key.size = sizeof(DISTRICT_PRIMARY_KEY); memset(&data, 0, sizeof(DBT)); data.data = &d_data; data.size = sizeof(DISTRICT_PRIMARY_DATA); if((err=dbp_district->put(dbp_district, 0, &key, &data, 0))) { db_error("DB->put", err); return -1; } if ( option_debug ) printf( "DID = %ld, WID = %ld, Name = %10s, Tax = %5.2f\n", d_id, d_w_id, d_name, d_tax ); } return 0; }
/* * Loads the Warehouse table * Loads Stock, District as Warehouses are created */ void LoadWare(void) { long w_id; char w_name[11]; char w_street_1[21]; char w_street_2[21]; char w_city[21]; char w_state[3]; char w_zip[10]; float w_tax; float w_ytd; DB *dbp; char *name = WAREHOUSE_INDEX_NAME; char *stock_name = STOCK_INDEX_NAME; char *district_name = DISTRICT_INDEX_NAME; int err; if(create_db(db_env, &dbp, DB_WH_PAGE_SIZE, 0) || open_db(dbp, name, 0)) return; if(create_db(db_env, &dbp_stock, DB_COMMON_PAGE_SIZE, 0) || open_db(dbp_stock, stock_name, 0)) goto done; if(create_db(db_env, &dbp_district, DB_DS_PAGE_SIZE, 0)) goto done; if((err = dbp_district->set_bt_compare(dbp_district, district_comparison_func))) { db_error("DB->set_bt_compare", err); goto done; } if(open_db(dbp_district, district_name, 0)) { goto done; } printf("Loading Warehouse \n"); for (w_id=1L; w_id<=count_ware; w_id++) { DBT key, data; WAREHOUSE_PRIMARY_KEY w_key; WAREHOUSE_PRIMARY_DATA w_data; memset(&w_key, 0, sizeof(WAREHOUSE_PRIMARY_KEY)); memset(&w_data, 0, sizeof(WAREHOUSE_PRIMARY_DATA)); /* Generate Warehouse Data */ MakeAlphaString( 6, 10, w_name); MakeAddress( w_street_1, w_street_2, w_city, w_state, w_zip ); w_tax=((float)random1(10L,20L))/100.0; w_ytd=3000000.00; w_key.W_ID = w_id; memcpy(&w_data.W_NAME, &w_name, 11); memcpy(&w_data.W_STREET_1, &w_street_1, 21); memcpy(&w_data.W_STREET_2, &w_street_2, 21); memcpy(&w_data.W_CITY, &w_city, 21); memcpy(&w_data.W_STATE, &w_state, 3); memcpy(&w_data.W_ZIP, &w_zip, 10); w_data.W_TAX = w_tax; w_data.W_YTD = w_ytd; /* Initialize the key */ memset(&key, 0, sizeof(key)); key.data = &w_key; key.size = sizeof(WAREHOUSE_PRIMARY_KEY); /* Initialize the data */ memset(&data, 0, sizeof(data)); data.data = &w_data; data.size = sizeof(WAREHOUSE_PRIMARY_DATA); if ( option_debug ) printf( "WID = %ld, Name= %16s, Tax = %5.2f\n", w_id, w_name, w_tax ); if((err=dbp->put(dbp, 0, &key, &data, 0))) { db_error("DB->put", err); goto done; } /** Make Rows associated with Warehouse **/ if(Stock(w_id) || District(w_id)) { goto done; } } done: if(dbp) dbp->close(dbp, 0); if(dbp_stock) dbp_stock->close(dbp_stock, 0); if(dbp_district) dbp_district->close(dbp_district, 0); }