// Listing 4 // Listing 5 code/ch17 int handle_child (void) { ACE_TRACE (ACE_TEXT ("::handle_child")); ACE_GUARD_RETURN (ACE_Process_Mutex, ace_mon, coordMutex, -1); ALLOCATOR * shmem_allocator = 0; ACE_MMAP_Memory_Pool_Options options (ACE_DEFAULT_BASE_ADDR, ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED); ACE_NEW_RETURN (shmem_allocator, ALLOCATOR (BACKING_STORE, BACKING_STORE, &options), -1); MAP *map = smap (shmem_allocator); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Map has %d entries\n"), map->current_size ())); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("In child, map is located at %@\n"), map)); processRecords (map, shmem_allocator); shmem_allocator->sync (); delete shmem_allocator; return 0; }
static void unbind_test (MAP &map, size_t iterations, KEY *keys) { // Remove system generated keys. size_t counter = iterations; for (VALUE i = 0; i < iterations; ++i) { ACE_ASSERT (map.unbind (keys[i]) != -1); --counter; ACE_ASSERT (map.current_size () == counter); } }
static void insert_test (MAP &map, size_t iterations, KEY *keys) { // Add to the map, allowing keys to be created by the map. size_t counter = 0; for (VALUE i = 0; i < iterations; ++i) { ACE_ASSERT (map.bind_create_key (i, keys[i]) == 0); ++counter; ACE_ASSERT (map.current_size () == counter); } }
static void functionality_test (MAP &map, size_t iterations) { size_t counter; VALUE i; KEY *original_keys = new KEY[iterations]; KEY *modified_keys = new KEY[iterations]; // Setup the keys to have some initial data. for (i = 0; i < iterations; ++i) { original_keys[i].size (sizeof i / sizeof (KEY::TYPE)); ACE_OS::memcpy (&original_keys[i][0], &i, sizeof i); } // Make a copy of the keys so that we can compare with the original // keys later. for (i = 0; i < iterations; ++i) { modified_keys[i] = original_keys[i]; } // Add to the map, allowing keys to be modified. counter = 0; for (i = 0; i < iterations; ++i) { ACE_ASSERT (map.bind_modify_key (i, modified_keys[i]) == 0); ++counter; ACE_ASSERT (map.current_size () == counter); } // Forward iteration... { counter = 0; MAP::iterator end = map.end (); for (MAP::iterator iter = map.begin (); iter != end; ++iter, ++counter) { MAP::value_type entry = *iter; // Recover original key. KEY original_key; ACE_ASSERT (map.recover_key (entry.first (), original_key) == 0); // Make sure recovering keys work. ACE_ASSERT (original_keys[entry.second ()] == original_key); // Obtain value from key. VALUE original_value; ACE_OS::memcpy (&original_value, &original_key[0], sizeof original_value); // Debugging info. ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%d|%d|%d)"), counter, original_value, entry.second ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); ACE_ASSERT (counter == iterations); } // Reverse iteration... { counter = iterations; MAP::reverse_iterator end = map.rend (); for (MAP::reverse_iterator iter = map.rbegin (); iter != end; ++iter) { --counter; MAP::value_type entry = *iter; // Recover original key. KEY original_key; ACE_ASSERT (map.recover_key (entry.first (), original_key) == 0); // Make sure recovering keys work. ACE_ASSERT (original_keys[entry.second ()] == original_key); // Obtain value from key. VALUE original_value; ACE_OS::memcpy (&original_value, &original_key[0], sizeof original_value); // Debugging info. ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%d|%d|%d)"), counter, original_value, entry.second ())); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n"))); ACE_ASSERT (counter == 0); } // Search using the modified keys. for (i = 0; i < iterations; ++i) { VALUE j; ACE_ASSERT (map.find (modified_keys[i], j) != -1); ACE_ASSERT (i == j); } // Rmoved keys from map. counter = iterations; for (i = 0; i < iterations; ++i) { ACE_ASSERT (map.unbind (modified_keys[i]) != -1); --counter; ACE_ASSERT (map.current_size () == counter); } // Cleanup. delete[] modified_keys; delete[] original_keys; }
// Listing 5 // Listing 3 code/ch17 int handle_parent (char *cmdLine) { ACE_TRACE (ACE_TEXT ("::handle_parent")); ALLOCATOR * shmem_allocator = 0; ACE_MMAP_Memory_Pool_Options options (ACE_DEFAULT_BASE_ADDR, ACE_MMAP_Memory_Pool_Options::ALWAYS_FIXED); ACE_NEW_RETURN (shmem_allocator, ALLOCATOR (BACKING_STORE, BACKING_STORE, &options), -1); MAP *map = smap (shmem_allocator); ACE_Process processa, processb; ACE_Process_Options poptions; poptions.command_line("%s a", cmdLine); { ACE_GUARD_RETURN (ACE_Process_Mutex, ace_mon, coordMutex, -1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Map has %d entries\n"), map->current_size ())); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("In parent, map is located at %@\n"), map)); // Then have the child show and eat them up. processa.spawn (poptions); // First append a few records. addRecords (map, shmem_allocator); } { ACE_GUARD_RETURN (ACE_Process_Mutex, ace_mon, coordMutex, -1); // Add a few more records.. addRecords (map, shmem_allocator); // Let's see what's left. ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) Parent finished adding, ") ACE_TEXT ("map has %d entries\n"), map->current_size ())); // Have another child try to eat them up. processb.spawn (poptions); } processa.wait (); processb.wait (); // No processes are left and we don't want to keep the data // around anymore; it's now safe to remove it. // !!This will remove the backing store.!! shmem_allocator->remove (); delete shmem_allocator; return 0; }