degate  0.1.2
Public Member Functions | Private Member Functions | Private Attributes
degate::MemoryMap< T > Class Template Reference

#include <MemoryMap.h>

Inheritance diagram for degate::MemoryMap< T >:
Inheritance graph
[legend]

List of all members.

Public Member Functions

 MemoryMap (unsigned int width, unsigned int height)
 Allocate a heap based memory chunk.
 MemoryMap (unsigned int width, unsigned int height, MAP_STORAGE_TYPE mode, std::string const &file_to_map)
 Create a file based memory chunk.
 ~MemoryMap ()
 The destructor.
int get_width () const
int get_height () const
void clear ()
 Cear the whole memory map.
void clear_area (unsigned int min_x, unsigned int min_y, unsigned int width, unsigned int height)
 Clear an area given by start point and width and height.
void set (unsigned int x, unsigned int y, T new_val)
 Set the value of a memory element.
get (unsigned int x, unsigned int y) const
 Get the value of an memory element.
void raw_copy (void *buf) const
 Copy the whole memory content into a buffer.
std::string const & get_filename () const
 Get the name of the mapped file.

Private Member Functions

ret_t alloc_memory ()
ret_t map_temp_file (std::string const &filename_pattern)
 Create a temp file and use it as storage for the map data.
ret_t map_file (std::string const &filename)
 Use storage in file as storage for memory map.
ret_t map_file_by_fd ()
void * get_void_ptr (unsigned int x, unsigned int y) const
bool is_temp_file () const
bool is_persistent_file () const
bool is_mem () const

Private Attributes

unsigned int width
unsigned int height
MAP_STORAGE_TYPE storage_type
T * mem
std::string filename
int fd
size_t filesize

Detailed Description

template<typename T>
class degate::MemoryMap< T >

Definition at line 58 of file MemoryMap.h.


Constructor & Destructor Documentation

template<typename T >
degate::MemoryMap< T >::MemoryMap ( unsigned int  width,
unsigned int  height 
)

Allocate a heap based memory chunk.

Parameters:
widthThe width of a 2D map.
heightThe height of a 2D map.

Definition at line 153 of file MemoryMap.h.

References degate::MemoryMap< T >::alloc_memory(), degate::MemoryMap< T >::height, degate::RET_OK, and degate::MemoryMap< T >::width.

                                                                   :
    width(_width), height(_height),
    storage_type(MAP_STORAGE_TYPE_MEM),
    mem(NULL),
    fd(-1),
    filesize(0) {

    assert(width > 0 && height > 0);

    ret_t ret = alloc_memory();
    assert(ret == RET_OK);
  }

Here is the call graph for this function:

template<typename T >
degate::MemoryMap< T >::MemoryMap ( unsigned int  width,
unsigned int  height,
MAP_STORAGE_TYPE  mode,
std::string const &  file_to_map 
)

Create a file based memory chunk.

The storage is filebases. The file is mapped into memory.

Parameters:
widthThe width of a 2D map.
heightThe height of a 2D map.
modeIs either MAP_STORAGE_TYPE_PERSISTENT_FILE or MAP_STORAGE_TYPE_TEMP_FILE.
file_to_mapThe name of the file, which should be mmap().

Definition at line 167 of file MemoryMap.h.

References debug(), degate::MemoryMap< T >::height, degate::MemoryMap< T >::map_file(), degate::MAP_STORAGE_TYPE_PERSISTENT_FILE, degate::MAP_STORAGE_TYPE_TEMP_FILE, degate::MemoryMap< T >::map_temp_file(), RET_IS_NOT_OK, RET_IS_OK, TM, and degate::MemoryMap< T >::width.

                                                                                :
    width(_width), height(_height),
    storage_type(mode),
    mem(NULL),
    filename(file_to_map),
    fd(-1),
    filesize(0) {

    assert(mode == MAP_STORAGE_TYPE_PERSISTENT_FILE ||
           mode == MAP_STORAGE_TYPE_TEMP_FILE);

    assert(width > 0 && height > 0);

    ret_t ret;

    if(mode == MAP_STORAGE_TYPE_TEMP_FILE) {
      ret = map_temp_file(file_to_map);
      if(RET_IS_NOT_OK(ret))
        debug(TM, "Can't open a temp file with pattern %s", file_to_map.c_str());

      assert(RET_IS_OK(ret));
    }
    else if(mode == MAP_STORAGE_TYPE_PERSISTENT_FILE) {
      ret = map_file(file_to_map);
      if(RET_IS_NOT_OK(ret)) debug(TM, "Can't open file %s as persistent file", file_to_map.c_str());
      assert(RET_IS_OK(ret));
    }
  }

Here is the call graph for this function:

template<typename T >
degate::MemoryMap< T >::~MemoryMap ( )

The destructor.

Definition at line 199 of file MemoryMap.h.

References debug(), degate::MAP_STORAGE_TYPE_MEM, degate::MAP_STORAGE_TYPE_PERSISTENT_FILE, degate::MAP_STORAGE_TYPE_TEMP_FILE, and TM.

                           {

    switch(storage_type) {
    case MAP_STORAGE_TYPE_MEM:
      if(mem != NULL) free(mem);
      mem = NULL;
      break;
    case MAP_STORAGE_TYPE_PERSISTENT_FILE:
    case MAP_STORAGE_TYPE_TEMP_FILE:
      if(mem != NULL) {
        if(msync(mem, filesize, MS_SYNC) == -1) {
          perror("msync() failed");
        }

        if(munmap(mem, filesize) == -1) {
          perror("munmap failed");
        }

        mem = NULL;
      }

      close(fd);

      if(is_temp_file()) {
        if(unlink(filename.c_str()) == -1) {
          debug(TM, "Can't unlink temp file");
        }
      }
      break;
    }

  }

Here is the call graph for this function:


Member Function Documentation

template<typename T >
ret_t degate::MemoryMap< T >::alloc_memory ( ) [private]

Definition at line 233 of file MemoryMap.h.

References degate::RET_MALLOC_FAILED, and degate::RET_OK.

Referenced by degate::MemoryMap< T >::MemoryMap().

                                   {

    /* If it is not null, it would indicates,
       that there is already any allocation. */
    assert(mem == NULL);

    mem = (T *) malloc(width * height * sizeof(T));
    assert(mem != NULL);
    if(mem == NULL) return RET_MALLOC_FAILED;
    memset(mem, 0, width * height * sizeof(T));
    return RET_OK;
  }

Here is the caller graph for this function:

template<typename T >
void degate::MemoryMap< T >::clear ( )

Cear the whole memory map.

Clear map data.

Definition at line 251 of file MemoryMap.h.

                           {
    assert(mem != NULL);
    if(mem != NULL) memset(mem, 0, width * height * sizeof(T));
  }
template<typename T >
void degate::MemoryMap< T >::clear_area ( unsigned int  min_x,
unsigned int  min_y,
unsigned int  width,
unsigned int  height 
)

Clear an area given by start point and width and height.

Definition at line 261 of file MemoryMap.h.

                                                                          {
    assert(mem != NULL);

    if(mem != NULL) {
      unsigned int x, y;
      for(y = min_y; y < min_y + height; y++)
        memset(get_void_ptr(x, y), 0, width * sizeof(T));
    }
  }
template<typename T >
T degate::MemoryMap< T >::get ( unsigned int  x,
unsigned int  y 
) const [inline]

Get the value of an memory element.

Definition at line 451 of file MemoryMap.h.

References debug(), and TM.

Referenced by degate::StoragePolicy_Memory< PixelPolicy >::get_pixel(), and degate::StoragePolicy_File< PixelPolicy >::get_pixel().

                                                                 {
    if(x >= width || y >= height)  {
      debug(TM, "error: out of bounds x=%d, y=%d / width=%d, height=%d", x, y, width, height);
    }

    assert(x < width && y < height);
    return mem[y * width + x];
    /*
    if(x + y * width < width * height)
      return mem[y * width + x];
    else {
      debug(TM, "error: out of bounds x=%d, y=%d / width=%d, height=%d", x, y, width, height);
      assert(1 == 0);
      return NULL;
    }
    */
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T>
std::string const& degate::MemoryMap< T >::get_filename ( ) const [inline]

Get the name of the mapped file.

Returns:
Returns a string with the mapped file. If the memory chunk is heap and not file based, an empty string is returned.

Definition at line 148 of file MemoryMap.h.

{ return filename; }
template<typename T>
int degate::MemoryMap< T >::get_height ( ) const [inline]

Definition at line 117 of file MemoryMap.h.

{ return height; }
template<typename T >
void * degate::MemoryMap< T >::get_void_ptr ( unsigned int  x,
unsigned int  y 
) const [private]

Definition at line 423 of file MemoryMap.h.

References debug(), and TM.

                                                                        {
    if(x + y < width * height)
      return mem + (y * width + x);
    else {
      debug(TM, "error: out of bounds x=%d, y=%d", x, y);
      assert(1 == 0);
      return NULL;
    }
  }

Here is the call graph for this function:

template<typename T>
int degate::MemoryMap< T >::get_width ( ) const [inline]

Definition at line 116 of file MemoryMap.h.

{ return width; }
template<typename T>
bool degate::MemoryMap< T >::is_mem ( ) const [inline, private]

Definition at line 86 of file MemoryMap.h.

template<typename T>
bool degate::MemoryMap< T >::is_persistent_file ( ) const [inline, private]

Definition at line 82 of file MemoryMap.h.

template<typename T>
bool degate::MemoryMap< T >::is_temp_file ( ) const [inline, private]

Definition at line 78 of file MemoryMap.h.

template<typename T >
ret_t degate::MemoryMap< T >::map_file ( std::string const &  filename) [private]

Use storage in file as storage for memory map.

Definition at line 308 of file MemoryMap.h.

References debug(), degate::RET_ERR, and TM.

Referenced by degate::MemoryMap< T >::MemoryMap().

                                                        {

    assert(is_persistent_file());

    if((fd = open(filename.c_str(), O_RDWR | O_CREAT, 0600)) == -1) {
      debug(TM, "can't open file: %s", filename.c_str());
      return RET_ERR;
    }

    return map_file_by_fd();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
ret_t degate::MemoryMap< T >::map_file_by_fd ( ) [private]

Definition at line 321 of file MemoryMap.h.

References debug(), degate::RET_ERR, degate::RET_OK, and TM.

                                     {

    assert(fd != -1);
    if(fd == -1) {
      debug(TM, "error: invalid file handle");
      return RET_ERR;
    }
    else {
      // get file size
      filesize = lseek(fd, 0, SEEK_END);
      if(filesize < width * height * sizeof(T)) {
        filesize = width * height * sizeof(T);
        lseek(fd, filesize - 1, SEEK_SET);
        if(write(fd, "\0", 1) != 1) {
          debug(TM, "can't open file: %s", filename.c_str());
          return RET_ERR;
        }
      }

      // map the file into memory
      if((mem = (T *) mmap(NULL, filesize,
                           PROT_READ | PROT_WRITE,
                           MAP_FILE | MAP_SHARED, fd, 0)) == (void *)(-1)) {
        debug(TM, "mmap failed for %s", filename.c_str());
        close(fd);
        return RET_ERR;
      }

      return RET_OK;
    }
  }

Here is the call graph for this function:

template<typename T >
ret_t degate::MemoryMap< T >::map_temp_file ( std::string const &  filename_pattern) [private]

Create a temp file and use it as storage for the map data.

Parameters:
filename_patternThe parameter file is a string that specifies the temp file pattern, e.g. "/tmp/temp.XXXXXXX"

Definition at line 279 of file MemoryMap.h.

References debug(), degate::RET_ERR, and TM.

Referenced by degate::MemoryMap< T >::MemoryMap().

                                                                     {
    ret_t ret;

    assert(is_temp_file());

    char * tmp_filename = strdup(filename_pattern.c_str());

    fd = mkstemp(tmp_filename);

    if(fd == -1) {
      debug(TM, "mkstemp() failed");
      ret = RET_ERR;
    }
    else {
      ret = map_file_by_fd();
      filename = std::string(tmp_filename);
    }

    // cleanup
    free(tmp_filename);

    return ret;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
void degate::MemoryMap< T >::raw_copy ( void *  buf) const

Copy the whole memory content into a buffer.

On 32 bit architectures it might be neccessary to temporarily unmap data files.

Make sure that the buffer buf is large enough to hold get_width() * get_height() * sizeof(T) bytes.

This function should be used to unmap the data file from address space.

See also:
reactivate_mapping()
Todo:
Remove this method.

On 32 bit architectures it might be neccessary to temporarily unmap data files. This function should be used to map the data file again into address space.

See also:
gr_deactivate_mapping()
Todo:
Remove this method.

Definition at line 416 of file MemoryMap.h.

                                              {
    assert(mem != NULL);
    memcpy(buf, mem, width * height * sizeof(T));
  }
template<typename T>
void degate::MemoryMap< T >::set ( unsigned int  x,
unsigned int  y,
new_val 
) [inline]

Set the value of a memory element.

Definition at line 434 of file MemoryMap.h.

References debug(), and TM.

Referenced by degate::StoragePolicy_Memory< PixelPolicy >::set_pixel(), and degate::StoragePolicy_File< PixelPolicy >::set_pixel().

                                                                         {
    if(x >= width || y >= height)  {
      debug(TM, "error: out of bounds x=%d, y=%d / width=%d, height=%d", x, y, width, height);
    }
    assert(x < width && y < height);
    mem[y * width + x] = new_val;
    /*
    if(x + y * width < width * height)
      mem[y * width + x] = new_val;
    else {
      debug(TM, "error: out of bounds x=%d, y=%d / width=%d, height=%d", x, y, width, height);
      assert(1 == 0);
    }
    */
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

template<typename T>
int degate::MemoryMap< T >::fd [private]

Definition at line 66 of file MemoryMap.h.

template<typename T>
std::string degate::MemoryMap< T >::filename [private]
template<typename T>
size_t degate::MemoryMap< T >::filesize [private]

Definition at line 67 of file MemoryMap.h.

template<typename T>
unsigned int degate::MemoryMap< T >::height [private]
template<typename T>
T* degate::MemoryMap< T >::mem [private]

Definition at line 64 of file MemoryMap.h.

template<typename T>
MAP_STORAGE_TYPE degate::MemoryMap< T >::storage_type [private]
template<typename T>
unsigned int degate::MemoryMap< T >::width [private]

The documentation for this class was generated from the following file: