⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pool.cc

📁 简单的动态内存管理程序源代码
💻 CC
字号:
// file: test/pool.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 <pooled_allocator.h>#include <fstream>  // for std::ofstream to()char key_val1[] = "/allocate_key";char key_val2[] = "/allocate_key2";char key_addr1[] = "0x400d0000";char key_addr2[] = "0x408d0000";int main(int argc, char **argv) {  int passed=0, failed=0;    // make sure key shared memory segment is not in use  // shm_unlink(key_val1);  // shm_unlink(key_val2);    std::cerr << "----------------------------------------" << std::endl;  std::cerr << "Shared memory class test. " << std::endl;  /////////////////////////////////////////////////////////////////////  //////        //////      Shared Pool and Chunk tests start here  //////        /////////////////////////////////////////////////////////////////////    pooled_allocator::Pool<int,key_val1,key_addr1>    pool_obj1;  std::cerr << "\tAllocate test: 50 elements" << std::endl;  mem_space::memory_index_t mem_idx1 =    pool_obj1.alloc(50);//   mem_idx1.print();  if (mem_idx1.get_memory_ptr() != 0)    passed++;  else    failed++;  std::cerr << "\tCopy Constructor test" << std::endl;  pooled_allocator::Pool<int,key_val1,key_addr1>    pool_obj2(pool_obj1);  if (pool_obj1==pool_obj2)    passed++;  else    failed++;  std::cerr << "\tAssignment test" << std::endl;  pooled_allocator::Pool<int,key_val1,key_addr1>    pool_obj3=pool_obj1;  if (pool_obj1==pool_obj3)    passed++;  else    failed++;//   std::cerr << "\tDestructor test" << std::endl;//   pool_obj2.~Pool();  std::cerr << "\tAllocate test: 50 elements" << std::endl;  mem_space::memory_index_t mem_idx2 =     pool_obj2.alloc(50);//   mem_idx2.print();  if (mem_idx2.get_memory_ptr() != 0)    passed++;  else    failed++;  std::cerr << "\tAllocate test: 4000 elements" << std::endl;  mem_space::memory_index_t mem_idx3 =     pool_obj2.alloc(4000);//   mem_idx3.print();  if (mem_idx3.get_memory_ptr() != 0)    passed++;  else    failed++;  std::cerr << "\tAllocate test: 100 elements" << std::endl;  mem_space::memory_index_t mem_idx4 =     pool_obj2.alloc(100);//   mem_idx4.print();  if (mem_idx4.get_memory_ptr() != 0)    passed++;  else    failed++;  std::cerr << "\tAllocate test: 30000 elements" << std::endl;  mem_space::memory_index_t mem_idx5 =   pool_obj1.alloc(30000);//   mem_idx5.print();  if (mem_idx5.get_memory_ptr() != 0)    passed++;  else    failed++;  pool_obj1.free(static_cast<unsigned char*>(mem_idx1.get_memory_ptr()),50);  pool_obj2.free(static_cast<unsigned char*>(mem_idx2.get_memory_ptr()),50);  pool_obj2.free(static_cast<unsigned char*>(mem_idx3.get_memory_ptr()),4000);  pool_obj2.free(static_cast<unsigned char*>(mem_idx4.get_memory_ptr()),100);  pool_obj1.free(static_cast<unsigned char*>(mem_idx5.get_memory_ptr()),30000);  /////////////////////////////////////////////////////////////////////  //////        //////      Shared Pooled allocator tests start here  //////        /////////////////////////////////////////////////////////////////////    pooled_allocator::Pool_alloc<int,    key_val2,    key_addr2> pool_alloc_obj;  int* buff1 = pool_alloc_obj.allocate(50);  int* buff2 = pool_alloc_obj.allocate(30000);  int* buff3 = pool_alloc_obj.allocate(100);  pool_alloc_obj.deallocate(buff1,50);  pool_alloc_obj.deallocate(buff2,30000);  pool_alloc_obj.deallocate(buff3,100);  // close out semaphore arrays.  Do this after using Pool_alloc object  // as Pool_alloc object does not have a shutdown method call//   pool_obj1.shutdown();//   pool_obj2.shutdown();//   pooled_allocator::Pool<int,key_val2,key_addr2>//     pool_obj4;//   pool_obj4.shutdown();  std::cerr << "Passed: " << passed << std::endl;  std::cerr << "Failed: " << failed << std::endl;  std::cerr << "\t" << std::endl;  std::cerr << "----------------------------------------" << std::endl;      return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -