t_shared_memory.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_shared_memory                                                *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2003-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/byte_array.h>
00016 #include <basis/chaos.h>
00017 #include <basis/function.h>
00018 #include <basis/guards.h>
00019 #include <basis/istring.h>
00020 #include <opsystem/application_shell.h>
00021 #include <loggers/console_logger.h>
00022 #include <opsystem/filename.h>
00023 #include <opsystem/shared_memory.h>
00024 #include <data_struct/static_memory_gremlin.h>
00025 
00026 const int CHUNKING_SIZE = 20 * KILOBYTE;
00027   // the size of the area we examine at one time.
00028 
00029 class test_shared_memory : public application_shell
00030 {
00031 public:
00032   test_shared_memory() : application_shell(class_name()) {}
00033   IMPLEMENT_CLASS_NAME("test_shared_memory");
00034   virtual int execute();
00035 };
00036 
00037 // this never returns zero so we can assume a zero is original contents.
00038 int zingle(int base)
00039 {
00040   return (base * 7 + 23108) % 255 + 1;
00041 }
00042 
00043 int test_shared_memory::execute()
00044 {
00045   FUNCDEF("execute");
00046   const int chunk_size = 1208227;
00047   shared_memory biggy_chunk(chunk_size, const_cast<char *>(class_name()));
00048 
00049   byte *mem = biggy_chunk.lock();
00050   if (!mem)
00051     deadly_error(class_name(), func,
00052         "allocation failure--could not create our shared memory chunk.");
00053   if (biggy_chunk.first_usage(mem, 100)) {
00054     // prepare the chunk in some manner, if needed.
00055   }
00056   biggy_chunk.unlock(mem);
00057 
00058   // iterate through the chunk and verify the values.
00059   for (int i = 0; i < chunk_size; i += CHUNKING_SIZE) {
00060     byte *mem = biggy_chunk.lock();
00061 
00062     if (!mem[i]) {
00063       // this area hasn't been set yet, so we'll write it in now.
00064       for (int j = 0; j < CHUNKING_SIZE; j++) {
00065         // verify that all bytes are zero for the range.
00066         if (mem[i + j])
00067           deadly_error(class_name(), func,
00068               "value failure--non-zero value in range where zero is required.");
00069         // use the offset to set a new value in our range.
00070         mem[i + j] = zingle(i + j);
00071       }
00072       // now we'll fall through to the snooze.
00073     } else {
00074       // this area has some kind of content.  let's test that it's valid.
00075       for (int j = 0; j < CHUNKING_SIZE; j++) {
00076         // check a value at this offset.
00077         if (mem[i + j] != zingle(i + j))
00078           deadly_error(class_name(), func,
00079               "value failure--this range has an incorrect value.");
00080       }
00081       // since we liked the values in this range, we can keep going and catch
00082       // up to other apps doing this by not snoozing.
00083       biggy_chunk.unlock(mem);
00084       continue;
00085     }
00086 
00087     biggy_chunk.unlock(mem);
00088     // now we snooze for a bit since this is designed to work with other
00089     // programs who could steal a range.
00090     int nap_length = randomizer().inclusive(10, 300);
00091     portable::sleep_ms(nap_length);
00092   }
00093 
00094   guards::alert_message("shared_memory:: works for those functions tested.\n");
00095   return 0;
00096 }
00097 
00098 HOOPLE_MAIN(test_shared_memory, )
00099 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1