|
degate 0.1.1
|
#include <MemoryMap.h>

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. | |
| T | 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 |
Definition at line 58 of file MemoryMap.h.
| degate::MemoryMap< T >::MemoryMap | ( | unsigned int | width, |
| unsigned int | height | ||
| ) |
Allocate a heap based memory chunk.
| width | The width of a 2D map. |
| height | The 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);
}

| 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.
| width | The width of a 2D map. |
| height | The height of a 2D map. |
| mode | Is either MAP_STORAGE_TYPE_PERSISTENT_FILE or MAP_STORAGE_TYPE_TEMP_FILE. |
| file_to_map | The 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));
}
}

| 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;
}
}

| 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;
}

| void degate::MemoryMap< T >::clear | ( | ) |
| 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));
}
}
| 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.
Referenced by degate::StoragePolicy_File< PixelPolicy >::get_pixel(), and degate::StoragePolicy_Memory< 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;
}
*/
}


| std::string const& degate::MemoryMap< T >::get_filename | ( | ) | const [inline] |
Get the name of the mapped file.
Definition at line 148 of file MemoryMap.h.
{ return filename; }
| int degate::MemoryMap< T >::get_height | ( | ) | const [inline] |
Definition at line 117 of file MemoryMap.h.
{ return height; }
| void * degate::MemoryMap< T >::get_void_ptr | ( | unsigned int | x, |
| unsigned int | y | ||
| ) | const [private] |
| int degate::MemoryMap< T >::get_width | ( | ) | const [inline] |
Definition at line 116 of file MemoryMap.h.
{ return width; }
| bool degate::MemoryMap< T >::is_mem | ( | ) | const [inline, private] |
Definition at line 86 of file MemoryMap.h.
{
return storage_type == MAP_STORAGE_TYPE_MEM;
}
| bool degate::MemoryMap< T >::is_persistent_file | ( | ) | const [inline, private] |
Definition at line 82 of file MemoryMap.h.
{
return storage_type == MAP_STORAGE_TYPE_PERSISTENT_FILE;
}
| bool degate::MemoryMap< T >::is_temp_file | ( | ) | const [inline, private] |
Definition at line 78 of file MemoryMap.h.
{
return storage_type == MAP_STORAGE_TYPE_TEMP_FILE;
}
| 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();
}


| 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;
}
}

| 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.
| filename_pattern | The 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;
}


| 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.
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.
Definition at line 416 of file MemoryMap.h.
| void degate::MemoryMap< T >::set | ( | unsigned int | x, |
| unsigned int | y, | ||
| T | new_val | ||
| ) | [inline] |
Set the value of a memory element.
Definition at line 434 of file MemoryMap.h.
Referenced by degate::StoragePolicy_File< PixelPolicy >::set_pixel(), and degate::StoragePolicy_Memory< 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);
}
*/
}


int degate::MemoryMap< T >::fd [private] |
Definition at line 66 of file MemoryMap.h.
std::string degate::MemoryMap< T >::filename [private] |
Definition at line 65 of file MemoryMap.h.
Referenced by degate::MemoryMap< typename PixelPolicy::pixel_type >::get_filename().
size_t degate::MemoryMap< T >::filesize [private] |
Definition at line 67 of file MemoryMap.h.
unsigned int degate::MemoryMap< T >::height [private] |
Definition at line 61 of file MemoryMap.h.
Referenced by degate::MemoryMap< typename PixelPolicy::pixel_type >::get_height(), and degate::MemoryMap< T >::MemoryMap().
T* degate::MemoryMap< T >::mem [private] |
Definition at line 64 of file MemoryMap.h.
MAP_STORAGE_TYPE degate::MemoryMap< T >::storage_type [private] |
unsigned int degate::MemoryMap< T >::width [private] |
Definition at line 61 of file MemoryMap.h.
Referenced by degate::MemoryMap< typename PixelPolicy::pixel_type >::get_width(), and degate::MemoryMap< T >::MemoryMap().
1.7.4