C API data pending file name

I am writing a straightforward C program on Linux and want to use an existing library API that expects data from a file. I have to give it the file name as const char *. But I have data, as well as the contents of the file already sitting in the buffer allocated on the heap. There is a lot of RAM, and we want high performance. Wanting to avoid writing a temporary file to disk, what is a good way to feed data into this API in a way that looks like a file?

Here is a cheap mock version of my code:

marvelouslibrary.h:

int marvelousfunction(const char *filename);

normal-persons-usage.cpp, for which the library was originally developed:

#include "marvelouslibrary.h"
int somefunction(char *somefilename)
{
    return marvelousfunction(somefilename);
}

myprogram.cpp:

#include "marvelouslibrary.h"
int one_of_my_routines() 
{
    byte* stuff = new byte[1000000];
    // fill stuff[] with...stuff!
    // stuff[] holds same bytes as might be found in a file

    /* magic goes here: make filename referring to stuff[] */

   return marvelousfunction( ??? );
}

, marvelouslibrary API, ; .

mkfifo(), , , . . ? ?

, "B", shuddup . , - , , .

+3
5

, , , :

char *read_data(const char *fileName)

, " , " B ", shuddup ."

, , , , * int , - .

, , , ? , , ( ), .

+3

, ... / ?

, ( ), /dev/shm.

mmap , , posix_madvise() mmap() ( counterpart posix_fadvise(), ).

, , , , .

Edit

, . , . ​​, :

char * foo(const char *filepath)

... mmap().

, ( ), /dev/shm , .

+2

: . . , , " , ". , , push...


, :

  • .
  • , ,

...

0

Linux, ? , , .

0

mmap(), ?

-1
source

Source: https://habr.com/ru/post/1704016/


All Articles