www.pudn.com > allocator.rar > shared_header.cc
// file: test/shared_header.cc // author: Marc Bumble // Mon Aug 11, 2003 // Memory allocator code for shared memory access // Copyright (C) 2003 by Marc D. Bumble // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include#include #include // for std::ofstream to() char key_val1[] = "/allocate_key"; // char key_val2[] = "/tmp/.allocate_key2"; int main(int argc, char **argv) { int passed=0, failed=0; enum { num_of_pages = 1000, page_size = 8*1024, bit_vector_size = 1000/8 + 1 }; // First, make sure that the shared memory segment are not in use // during the test. // shm_unlink(key_val1); // create memory for the shared memory header // simulate the page sizes int buff_size = num_of_pages*page_size + sizeof(mem_space::shared_memory_header_t) + bit_vector_size; unsigned char buffer[buff_size]; mem_space::shared_memory_header_t* smh = new(buffer)mem_space::shared_memory_header_t(1000, 8*1024, key_val1, (int) buffer, buff_size, 0); // proj_id // key_val1, std::cerr << "----------------------------------------" << std::endl; std::cerr << "Shared memory header test. " << std::endl; smh->mark_pages(0,5); std::cerr << "\tMarking pages 0 through 4" << std::endl; int start_page = smh->find_free_pages(4); std::cerr << "\tFinding 4 free pages" << std::endl; if (start_page==5) passed++; else failed++; smh->clear_pages(2,3); std::cerr << "\tClearing pages 2 through 4" << std::endl; start_page = smh->find_free_pages(3); std::cerr << "\tFind 3 free pages" << std::endl; if (start_page==2) passed++; else failed++; smh->mark_pages(0,num_of_pages-3); std::cerr << "\tMarking pages 0 through " << num_of_pages-3 << std::endl; start_page = smh->find_free_pages(3); std::cerr << "\tFinding 3 free pages" << std::endl; if (start_page==num_of_pages-3) passed++; else failed++; std::cerr << "\tTesting page offset." << std::endl; int page_offset = smh->get_page_offset(1); if (page_offset == static_cast (sizeof(mem_space::shared_memory_header_t) + smh->get_bit_vec_size() + 1*page_size)) passed++; else failed++; std::cerr << "\tTesting page offset." << std::endl; page_offset = smh->get_page_offset(num_of_pages-1); if (page_offset == static_cast (sizeof(mem_space::shared_memory_header_t) + smh->get_bit_vec_size() + (num_of_pages-1)*page_size)) passed++; else failed++; smh->set_key("/key_name_is_not_path"); char key_name[512]; smh->get_key(key_name); std::cerr << "\tPath name test: " << key_name << std::endl; // Destruct the shared memory head which was constructed with new above // Cleans up the semaphores smh->~shared_memory_header_t(); std::cerr << "Passed: " << passed << std::endl; std::cerr << "Failed: " << failed << std::endl; std::cerr << "----------------------------------------" << std::endl; return 0; }