alloc_mmap_int.c File Reference

Allocate memory using mmap for an integer array. More...

#include <utils.h>
Include dependency graph for alloc_mmap_int.c:

Go to the source code of this file.

Functions

void alloc_mmap_int (int **map, int *fd, size_t *byte_size, char *filename, size_t page_size, int size)
 Allocate memory using mmap for an integer array.

Detailed Description

Allocate memory using mmap for an integer array.

Definition in file alloc_mmap_int.c.


Function Documentation

void alloc_mmap_int ( int **  map,
int *  fd,
size_t *  byte_size,
char *  filename,
size_t  page_size,
int  size 
)

Allocate memory using mmap for an integer array.

Parameters:
[in,out] map Pointer to an integer array.
[in] fd File unit previously opened with open().
[in] byte_size Number of bytes allocated.
[in] filename Filename to use to store allocated mmap virtual memory.
[in] page_size The paging size of the operating system in bytes.
[in] size Number of elements in map array.

Definition at line 58 of file alloc_mmap_int.c.

00058                                                                                                   {
00068   int result; /* Return status of functions */
00069   size_t total_size; /* Total size in bytes, not taking into account the page size */
00070 
00071   /* Open a file for writing.
00072    *  - Creating the file if it doesn't exist.
00073    *  - Truncating it to 0 size if it already exists. (not really needed)
00074    *
00075    * Note: "O_WRONLY" mode is not sufficient when mmaping.
00076    */
00077   *fd = open(filename, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
00078   if (*fd == -1) {
00079     (void) perror("alloc_mmap_int: ERROR: Error opening file for writing");
00080     (void) kill(getpid(), 5);
00081   }
00082   
00083   total_size = size * sizeof(int);
00084   *byte_size = total_size / page_size * page_size + page_size;
00085 
00086   /* Stretch the file size to the size of the (mmapped) array */
00087   result = ftruncate(*fd, (off_t) *byte_size);
00088   if (result != 0) {
00089     (void) close(*fd);
00090     (void) perror("alloc_mmap_int: ERROR: Error calling ftruncate() to 'stretch' the file");
00091     (void) kill(getpid(), 5);
00092   }
00093   
00094   /* Now the file is ready to be mmapped. */
00095   *map = (int *) mmap(NULL, *byte_size, ( PROT_READ | PROT_WRITE ), MAP_SHARED, *fd, 0);
00096   if (*map == (int *) MAP_FAILED) {
00097     (void) close(*fd);
00098     (void) perror("alloc_mmap_int: ERROR: Error mmapping the file");
00099     (void) kill(getpid(), 5);
00100   }
00101 }


Generated on 12 May 2016 for DSCLIM by  doxygen 1.6.1