www.pudn.com > allocator.rar > shared2.cc


// file: test/shared2.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_val[] = "/allocate_key";
char key_addr[] = "0x400d0000";

int main(int argc, char **argv) {

  // make sure key shared memory segment is not in use
  // shm_unlink(key_val);
  
  enum {num_of_pages=1000,
	bit_vec_sz=num_of_pages/8 + 1,
	page_size=(8*1024),
	max_num_shared_segments=128,
	max_segment_size=32768
  };

  mem_space::shared shared_mem =
    mem_space::shared(num_of_pages,page_size);

  mem_space::memory_index_t mem_idx = shared_mem.allocate(1);
  mem_space::shared_memory_header_t* smh =
    static_cast(mem_idx.get_memory_ptr());
  int start_page1 = smh->find_free_pages(3);
  std::cerr << "\t find_free_pages 3 " << std::endl;
  smh->mark_pages(start_page1,3);
  std::cerr << "\t\t 3 pages start: " << start_page1 << std::endl;
  int start_page2 = smh->find_free_pages(2);
  std::cerr << "\t find_free_pages 2 " << std::endl;
  smh->mark_pages(start_page2,2);
  std::cerr << "\t\t 2 pages start: " << start_page2 << std::endl;
  int start_page3 = smh->find_free_pages(5);
  std::cerr << "\t find_free_pages 5 " << std::endl;
  smh->mark_pages(start_page3,5);
  std::cerr << "\t\t 5 pages start: " << start_page3 << std::endl;
  smh->clear_pages(start_page2,2);
  std::cerr << "\t\t clear 2 pages start: " << start_page2 << std::endl;
  int start_page4 = smh->find_free_pages(2);
  std::cerr << "\t find_free_pages 2 " << std::endl;
  smh->mark_pages(start_page4,2);
  std::cerr << "\t\t clear 2 pages start: " << start_page4 << std::endl;
  std::cerr << "----------------------------------------" << std::endl;
  if (start_page4 == 3) 
    std::cerr << "Pass: bitvec page allocation test" << std::endl;
  else
    std::cerr << "Fail: bitvec page allocation test" << std::endl;
  
  int start_page_offset = smh->get_page_offset(start_page4);
  unsigned char* buff = reinterpret_cast(smh) + start_page_offset;
  std::string* val = new (buff) std::string("Pass: Shared Memory attach");
  std::cerr << *val << std::endl;
  smh->clear_pages(start_page4,5);
  
//   int shared_memory_addr = smh->get_shared_memory_addr();
//   int shared_memory_size = smh->get_shared_memory_size();

//   // smh->shutdown();
//   if (shared_mem.free()) {
//     std::cerr << "Pass: Shared Memory detach" << std::endl;
//   } else {
//     std::cerr << "Fail: Shared Memory detach" << std::endl;
//   }
  std::cerr << "----------------------------------------" << std::endl;

}