TVMStatus VMMemoryPoolDeallocate(TVMMemoryPoolID memory, void *pointer){ TMachineSignalState sigState; MachineSuspendSignals(&sigState); ThreadStore* tStore = ThreadStore::getInstance(); if(pointer == NULL){ printf("VMMemoryPoolDeallocate(): pointer was null.\n"); return VM_STATUS_ERROR_INVALID_PARAMETER; } MemoryPool *pool = tStore->findMemoryPoolByID(memory); if(pool == NULL){ printf("VMMemoryPoolDeallocate(): pool was null.\n"); return VM_STATUS_ERROR_INVALID_PARAMETER; } // printf("VMMemoryPoolDeallocate(): attempting to deallocate chunk %d\n", pointer); if(pool->deallocate((uint8_t*)pointer) == false){ //attempts to deallocate the memory specified by pointer return VM_STATUS_ERROR_INVALID_PARAMETER; //returns true on successful deallocation, and false if the }//memory pointed to by pointer was not previously allocated in the memory pool MachineResumeSignals(&sigState); return VM_STATUS_SUCCESS; }
int main() { MemoryPool<A, 32> m; cout << endl; for (int i = 0; i < 5; ++i) { cout << "***** " << i << " *****" << endl << endl; A *temp = m.allocate(); cout << "return pointer = " << static_cast<const void *>(temp) << endl; m.deallocate(temp); cout << endl; } return 0; }
explicit SecurityAttributes(MemoryPool& pool) : m_pool(pool) { // Ensure that our process has the SYNCHRONIZE privilege granted to everyone PSECURITY_DESCRIPTOR pOldSD = NULL; PACL pOldACL = NULL; // Pseudo-handles do not work on WinNT. Need real process handle. HANDLE hCurrentProcess = OpenProcess(READ_CONTROL | WRITE_DAC, FALSE, GetCurrentProcessId()); if (hCurrentProcess == NULL) { Firebird::system_call_failed::raise("OpenProcess"); } DWORD result = GetSecurityInfo(hCurrentProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pOldACL, NULL, &pOldSD); if (result == ERROR_CALL_NOT_IMPLEMENTED) { // For Win9X - sumulate that the call worked alright pOldACL = NULL; result = ERROR_SUCCESS; } if (result != ERROR_SUCCESS) { CloseHandle(hCurrentProcess); Firebird::system_call_failed::raise("GetSecurityInfo", result); } // NULL pOldACL means all privileges. If we assign pNewACL in this case // we'll lost all privileges except assigned SYNCHRONIZE if (pOldACL) { SID_IDENTIFIER_AUTHORITY sidAuth = SECURITY_WORLD_SID_AUTHORITY; PSID pSID = NULL; AllocateAndInitializeSid(&sidAuth, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pSID); EXPLICIT_ACCESS ea; memset(&ea, 0, sizeof(EXPLICIT_ACCESS)); ea.grfAccessPermissions = SYNCHRONIZE; ea.grfAccessMode = GRANT_ACCESS; ea.grfInheritance = NO_INHERITANCE; ea.Trustee.TrusteeForm = TRUSTEE_IS_SID; ea.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; ea.Trustee.ptstrName = (LPTSTR) pSID; PACL pNewACL = NULL; SetEntriesInAcl(1, &ea, pOldACL, &pNewACL); SetSecurityInfo(hCurrentProcess, SE_KERNEL_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pNewACL, NULL); if (pSID) { FreeSid(pSID); } if (pNewACL) { LocalFree(pNewACL); } } CloseHandle(hCurrentProcess); if (pOldSD) { LocalFree(pOldSD); } // Create and initialize the default security descriptor // to be assigned to various IPC objects. // // WARNING!!! The absent DACL means full access granted // to everyone, this is a huge security risk! PSECURITY_DESCRIPTOR p_security_desc = static_cast<PSECURITY_DESCRIPTOR>( pool.allocate(SECURITY_DESCRIPTOR_MIN_LENGTH)); attributes.nLength = sizeof(attributes); attributes.lpSecurityDescriptor = p_security_desc; attributes.bInheritHandle = TRUE; if (!InitializeSecurityDescriptor(p_security_desc, SECURITY_DESCRIPTOR_REVISION) || !SetSecurityDescriptorDacl(p_security_desc, TRUE, NULL, FALSE)) { pool.deallocate(p_security_desc); attributes.lpSecurityDescriptor = NULL; } }
TVMStatus VMFileWrite(int fileDescriptor, void* data, int *length){ TMachineSignalState sigState; MachineSuspendSignals(&sigState); //suspend signals so we cannot be pre-empted while scheduling a new thread ThreadStore* tStore = ThreadStore::getInstance(); MemoryPool* sharedMemory = tStore->findMemoryPoolByID((TVMMemoryPoolID)1); TCB* currentThread = tStore->getCurrentThread(); uint8_t* sharedLocation; TVMMemorySize allocSize = *length; //Step 0 - validate data if((data == NULL) || (length == NULL)){ MachineResumeSignals (&sigState); return VM_STATUS_ERROR_INVALID_PARAMETER; } //Step 1 - initialize shared memory location, and wait if memory is not available from share pool if(allocSize > 512){ //make sure thread never asks for > 512 bytes allocSize = 512; } if(allocSize > sharedMemory->getSize()){ allocSize = sharedMemory->getSize(); } sharedLocation = sharedMemory->allocateMemory(allocSize); //allocate a space in the shared memory pool if(sharedLocation == NULL){ // printf("VMFileWrite(): shared location was null\n"); sharedLocation = tStore->waitCurrentThreadOnMemory(allocSize, 1); } //Step 2 - IO loop. If the data to transfer is > 512 bytes, loop. If it isn't, loop only runs once. int bytesLeft = *length; int chunkSize = bytesLeft; void *dataLocation = data; int bytesTransferred = 0; if(bytesLeft > allocSize) chunkSize = allocSize; for(bytesLeft; bytesLeft > 0; bytesLeft -= chunkSize){ if(bytesLeft < chunkSize) chunkSize = bytesLeft; //printf("tid %d outputting\n", tStore->getCurrentThread()->getThreadID()); //copy chunkSize bytes of data from *data into the shared memory location, starting at dataLocation memcpy((void*)sharedLocation, dataLocation, chunkSize*sizeof(uint8_t)); //step 3 - call MachineFileWrite with the pointer to the shared memory location MachineFileWrite(fileDescriptor, sharedLocation, chunkSize, &machineFileIOCallback, (void*)currentThread); tStore->waitCurrentThreadOnIO(); //switch to a new thread while waiting on IO //update bytesLeft and dataLocation for the next iteration bytesTransferred = bytesTransferred + currentThread->getFileIOResult(); dataLocation = ((uint8_t*)dataLocation + chunkSize); } //step 4 - Deallocate the shared memory location, do last error check, and return sharedMemory->deallocate(sharedLocation); if(bytesTransferred < 0){ MachineResumeSignals (&sigState); return VM_STATUS_FAILURE; } MachineResumeSignals (&sigState); return VM_STATUS_SUCCESS; }
void testAllocator() { printf("Test Firebird::MemoryPool\n"); MemoryPool* parent = getDefaultMemoryPool(); MemoryPool* pool = MemoryPool::createPool(parent); MallocAllocator allocator; BePlusTree<AllocItem, AllocItem, MallocAllocator, DefaultKeyValue<AllocItem>, AllocItem> items(&allocator), bigItems(&allocator); Vector<void*, LARGE_ITEMS> la; printf("Allocate %d large items: ", LARGE_ITEMS); int i; for (i = 0; i<LARGE_ITEMS; i++) { la.add(pool->allocate(LARGE_ITEM_SIZE)); VERIFY_POOL(pool); } VERIFY_POOL(pool); printf(" DONE\n"); printf("Allocate %d items: ", ALLOC_ITEMS); int n = 0; VERIFY_POOL(pool); for (i = 0; i < ALLOC_ITEMS; i++) { n = n * 47163 - 57412; // n = n * 45578 - 17651; AllocItem temp = {n, pool->allocate((n % MAX_ITEM_SIZE + MAX_ITEM_SIZE) / 2 + 1)}; items.add(temp); } printf(" DONE\n"); VERIFY_POOL(pool); VERIFY_POOL(parent); printf("Deallocate half of items in quasi-random order: "); n = 0; if (items.getFirst()) do { pool->deallocate(items.current().item); n++; } while (n < ALLOC_ITEMS / 2 && items.getNext()); printf(" DONE\n"); VERIFY_POOL(pool); VERIFY_POOL(parent); printf("Allocate %d big items: ", BIG_ITEMS); n = 0; VERIFY_POOL(pool); for (i = 0; i < BIG_ITEMS; i++) { n = n * 47163 - 57412; // n = n * 45578 - 17651; AllocItem temp = {n, pool->allocate((n % BIG_SIZE + BIG_SIZE) / 2 + 1)}; bigItems.add(temp); } printf(" DONE\n"); VERIFY_POOL(pool); VERIFY_POOL(parent); printf("Deallocate the rest of small items in quasi-random order: "); while (items.getNext()) { pool->deallocate(items.current().item); } printf(" DONE\n"); VERIFY_POOL(pool); VERIFY_POOL(parent); printf("Deallocate big items in quasi-random order: "); if (bigItems.getFirst()) do { pool->deallocate(bigItems.current().item); } while (bigItems.getNext()); printf(" DONE\n"); printf("Deallocate %d large items: ", LARGE_ITEMS/2); for (i = 0; i<LARGE_ITEMS/2; i++) pool->deallocate(la[i]); VERIFY_POOL(pool); printf(" DONE\n"); // pool->verify_pool(); // parent->verify_pool(); pool->print_contents(stdout, false); parent->print_contents(stdout, false); MemoryPool::deletePool(pool); // parent->verify_pool(); // TODO: // Test critically low memory conditions // Test that tree correctly recovers in low-memory conditions }