contrib/gel/gevd/gevd_memory_mixin.h
Go to the documentation of this file.
00001 #ifndef gevd_memory_mixin_h_
00002 #define gevd_memory_mixin_h_
00003 
00004 //=======================================================================
00005 //:
00006 // \file
00007 // \brief Captures all functional details required to access a real computer memory
00008 //
00009 // \author  Brian DeCleene
00010 // \date    Nov. 16, 1990.
00011 //
00012 // \verbatim
00013 //  Modifications:
00014 //   Burt Smith - June 11, 1991
00015 //    - Added status and handling of touched vs. untouched space.
00016 //   Burt Smith - July  8, 1991
00017 //    - Incorporated StatusMixin.
00018 //   Burt Smith - July 17, 1991
00019 //    - Changed ReadBytes(..., int loc) to seek from offset, not from start.
00020 //   Peter Vanroose - Nov.  4, 1996
00021 //    - added copy constructor; this fixes bug by copying the contents of 'buffer'.
00022 //   Peter Vanroose, Jul 97
00023 //    -  copy constructor re-added !?!
00024 // \endverbatim
00025 //=======================================================================
00026 
00027 #include "gevd_status_mixin.h"
00028 
00029 //======================================================================//
00030 /* Status Variables.  Certain events are always errors, hence the error */
00031 /* bit is set for those events.  For events which might be errors but   */
00032 /* are not always, the error bit is left clear; the proper choice will  */
00033 /* be made at the time the event occurs.                                */
00034 //======================================================================//
00035 #define MM_PROTECTED_FLAGS      0x000001CE // bits that may not be altered
00036 //                                         // except by the buffer.
00037 // Error conditions
00038 //
00039 #define MM_MEMORY_ERROR         0x00000083 // unable to allocate buffer
00040 #define MM_DATA_OVERFLOW        0x00000004 // past initialized data
00041 #define MM_OVERFLOW             0x00000088 // past allocated space
00042 #define MM_UNDERFLOW            0x000000C0 // read before allocated space
00043 //
00044 // Status conditions
00045 //
00046 #define MM_NIL_BUFFER           0x00000001 // buffer is nil buffer
00047 #define MM_DIRTY                0x00000010 // buffer changed since alloced
00048 #define MM_FOREIGN_BLOCK        0x00000020 // foreign memory used for buffer
00049 #define MM_ERROR                0x00000080 // error has occurred
00050 #define MM_WARN                 0x00000100 // warning has occurred
00051 //
00052 // Buffer types, usually indicated at creation.  Note that it is important
00053 // NOT to use the MM_PROTECTED type unless absolutely necessary; this will
00054 // guarantee deallocation of resources when the buffer is no longer used.
00055 //
00056 #define MM_READ                 0x00000200 // buffer can be read.
00057 #define MM_WRITE                0x00000400 // buffer can be written to.
00058 #define MM_PROTECTED            0x00000800 // buffer cannot be deleted.
00059 #define MM_FIXED                0x00001000 // buffer cannot be replaced.
00060 #define MM_CLEAR                0x00002000 // buffer cleared at creation.  A
00061                                            // supplied buffer is not cleared.
00062 
00063 #define MM_CREATION_FLAGS       0x00003E00 // Flags used at creation.
00064 
00065 //---------------------------------------------------------------------
00066 
00067 class gevd_memory_mixin : public gevd_status_mixin
00068 {
00069  private:
00070   int size;               // amount of allocated memory (bytes).
00071   int touched;            // amount of allocated space touched.
00072   unsigned char* buffer;  // pointer to allocated memory
00073 
00074   int curr_into;          // amount into the allocated memory (bytes)
00075   unsigned char* current; // pointer to current place in memory.
00076 
00077   int offset;             // marked position in buffer.
00078 
00079  protected:
00080   void                    SetMemoryPtr(int s, void* ib = 0);
00081 
00082   inline unsigned char*   GetBufferPtr()  { return buffer;    }
00083   const  unsigned char* GetBufferPtr() const { return buffer; }
00084   inline unsigned char*   GetCurrentPtr() { return current;   }
00085   inline int              GetSize() const { return size;      }
00086   inline void SetStatus(int x=0)  {gevd_status_mixin::SetStatus(x); }
00087   inline void ClearStatus(int x=0) {gevd_status_mixin::ClearStatus(x);}
00088  public:
00089   // Constructors and Destructors
00090   //
00091   gevd_memory_mixin(int s = 0, void* ib = 0,
00092                     unsigned int type = MM_READ|MM_WRITE);
00093 
00094   virtual ~gevd_memory_mixin();
00095   gevd_memory_mixin(gevd_memory_mixin const&);
00096 
00097   // Methods for moving about the file.
00098   //
00099   int  GetOffset() const { return offset;      }
00100   void SetOffset()       { offset = curr_into; }
00101 #define min_(a,b) ((a)<(b)?a:b)
00102   void SkipBytes(int b)  { int skip = min_(b,size-curr_into);
00103                            current += skip; curr_into += skip;
00104                            if (skip<b) SetStatus(MM_OVERFLOW);
00105                            if (curr_into>touched)
00106                              SetStatus(MM_DATA_OVERFLOW | MM_WARN);     }
00107 #undef min_
00108   void SkipToByte(int b) { if (b>size) SetStatus(MM_OVERFLOW);
00109                                  else {if (b>touched)
00110                                          SetStatus(MM_DATA_OVERFLOW|MM_WARN);
00111                                        current = buffer + b;
00112                                        curr_into = b;}                  }
00113   void SkipToStart()     { current = buffer;        curr_into = 0;      }
00114   void SkipToDataStart() { current = buffer+offset; curr_into = offset; }
00115 
00116   // Methods for reading and writing data.
00117   //
00118   int  ReadBytes(void* ib, int b);
00119   int  ReadBytes(void* ib, int b, int loc);
00120   int  ReadBytes(void* ib, int b, int* mapping);
00121   int  ReadBytes(void* ib, int b, int loc, int* mapping);
00122   int  WriteBytes(const void* ib, int b);
00123   int  WriteBytes(const void* ib, int b, int loc);
00124   void Clear();
00125 };
00126 
00127 //=======================================================================
00128 
00129 #endif // gevd_memory_mixin_h_