www.pudn.com > allocator.rar > shared.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 
#include 
#include   // for std::ofstream to()

char key_val[] = "/allocate_key";
char key_addr[] = "0x400d0000";

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

  int passed=0, failed=0;

  // make sure key shared memory segment is not in use
  // shm_unlink(key_val);
  
  // mem_space::allocator_key_t key_val = "/tmp/.allocate_key";
  
  enum {num_of_pages=1000,
	page_size=8192
  };

  mem_space::shared shared_obj(num_of_pages,page_size);

  std::cerr << "----------------------------------------" << std::endl;
  std::cerr << "Shared memory class test. " << std::endl;

  std::cerr << "\tAllocate test" << std::endl;
  // allocate the first segment of memory
  // zero parameter requests first unused shared memory segment.
//   mem_space::memory_index_t mem_idx1 = shared_obj.allocate();
  mem_space::memory_index_t mem_idx1 = shared_obj.allocate(0);
  mem_space::shared_memory_header_t* smh1 =
    static_cast(mem_idx1.get_memory_ptr());
  char tmp_name[mem_space::name_length];
  smh1->get_key(tmp_name);
  size_t str_length = strlen(key_val);
  if (!strncmp(key_val,tmp_name,str_length))
    passed++;
  else
    failed++;
  // get a page of memory from the segment and write to it.
  int page1 = smh1->find_free_pages(1);
  // mark page as taken
  smh1->mark_pages(page1,1);
  // wrote to page, so sync memory
  smh1->sync();
  bool val = smh1->assigned(page1);
  if (val)
    passed++;			// page registered as assigned.
  else
    failed++;
  // determine page offset to memory from start of segment
  int offset1 = smh1->get_page_offset(page1);
  // get pointer to actual memory
  char* char_test =
    static_cast(mem_idx1.get_memory_ptr()) +
		       offset1;
  const char char_test_str[] = "Memory test string";
  strcpy(char_test,char_test_str);
  std::cerr << "\tCopy Constructor Test" << std::endl;
  mem_space::shared shared_obj2(shared_obj);
  // attached to the allocated the segment of memory
  const int& proj_id1 = mem_idx1.get_proj_id();
  // not retrieve the same shared memory segment using the proj_id
  val = smh1->assigned(page1);
  mem_space::memory_index_t mem_idx2 =
    shared_obj2.allocate(proj_id1);
//     shared_obj2.allocate();
  val = smh1->assigned(page1);
  mem_space::shared_memory_header_t* smh2 =
    static_cast(mem_idx2.get_memory_ptr());
  smh2->get_key(tmp_name);
  str_length = strlen(key_val);
  if (!strncmp(key_val,tmp_name,str_length))
    passed++;
  else
    failed++;
  // now check the state of the page marked earlier using the other
  // segment pointer
  smh2->sync();
  smh1->sync();
  val = smh1->assigned(page1);
  if (val)
    passed++;			// page registered as assigned.
  else
    failed++;
  val = smh2->assigned(page1);
  if (val)
    passed++;			// page registered as assigned.
  else
    failed++;
  // determine page offset to memory from start of segment
  int offset2 = smh2->get_page_offset(page1);
  // get pointer to actual memory
  char_test =
    static_cast(mem_idx2.get_memory_ptr()) +
		       offset2;
  if (!strcmp(char_test_str,char_test))
    passed++;
  else
    failed++;
  std::cerr << "\tAssignment Test" << std::endl;
  mem_space::shared shared_obj3(shared_obj);
  // call destructors to eliminate semaphores
  std::cerr << "\tFree test" << std::endl;
//   int shared_memory_addr1 = smh1->get_shared_memory_addr();
//   int shared_memory_size1 = smh1->get_shared_memory_size();
//   int shared_memory_addr2 = smh2->get_shared_memory_addr();
//   int shared_memory_size2 = smh2->get_shared_memory_size();
//   shared_obj.free();
//   //  if (shared_memory_addr1 != shared_memory_addr2)
//   shared_obj2.free();
  std::cerr << "Passed: " << passed << std::endl;
  std::cerr << "Failed: " << failed << std::endl;
  std::cerr << "----------------------------------------" << std::endl;
  
  
  return 0;
}