I am working on a program to stack astronomical images. It will involve having to store hundreds of 16 bit RGB images in memory for processing. I can not just handle the images one at a time since in some cases I want to take the median value for each pixel. I decided to create temporary files and mmap my images to them so I can use my image api like nothing changed between the image being in normal memory or being mmaped since the kernel should handle accessing the necessary parts of the file behind the scenes. I am posting this since I just wanted to make sure I am doing it correctly. Will this approach make it possible to load 50gb of images (hypothetically) if I only have 16GB of ram + my 8GB swap partition?
typedef struct
{
size_t w;
size_t h;
pixel_t px[];
} image_t;
//allocate in normal memory
image_t* image_new(size_t w, size_t h)
{
assert(w && h);
image_t* img = malloc(sizeof(image_t) + sizeof(pixel_t) * w * h);
if(!img)
return NULL;
img->w = w;
img->h = h;
memset(img->px, 0, sizeof(pixel_t) * w * h);
return img;
}
image_t* image_mmap_new(image_t* img)
{
FILE* tmp = tmpfile();
if(tmp == 0)
return NULL;
size_t wsize = sizeof(image_t) + sizeof(pixel_t) * img->w * img->h;
if(fwrite(img, 1, wsize, tmp) != wsize)
{
fclose(tmp);
return NULL;
}
image_t* nimg = mmap(NULL, wsize, PROT_WRITE | PROT_READ, MAP_SHARED, fileno(tmp), 0);
fclose(tmp);
return nimg;
}