Address PoolAllocator::allocate(Size *size) { Size index, nPools = 1; MemoryPool *pool = ZERO; /* Find the correct pool size. */ for (index = POOL_MIN_POWER; index < POOL_MAX_POWER; index++) if (*size <= (Size) 1 << (index + 1)) break; /* Do we need to allocate an initial pool? */ if (!pools[index] && parent) pool = pools[index] = newPool(index, POOL_MIN_COUNT(*size)); /* Search for pool with enough memory. */ else { /* Loop current pools. */ for (pool = pools[index]; pool; pool = pool->next, nPools++) { /* At least one block still free? */ if (pool->free) break; /* If no pool has free space anymore, allocate another. */ if (!pool->next) { pool = newPool(index, POOL_MIN_COUNT(*size) * nPools); break; } } } /* Attempt to allocate. */ return pool ? pool->allocate() : ZERO; }
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; } }
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 }