2

I have a legacy C library which accepts a file, works on the file payload and writes the processed payload to an output file. The functions in the library are tightly coupled with FILE i.e. it passes around FILE handle to the functions and functions do file IO to retrieve the necessary data.

I want to modify this library such that it works with in memory data(No file IO). i.e pass a binary array and get back binary array.

I have 2 solution in mind

  1. Implement a InMemory File module (which implants all operations as C FILE) and override the default file operations with new implementation using typedef or #define
  2. Pass around binary array to all the functions of the library and retrieve the necessary data from the same.

Which one of this is better or any other better way to solve the problem

5

2 Answers 2

3

I would suggest not to change legacy code if any other code depends on it.

If you are building for a somewhat POSIX compliant platform, you can use fmemopen http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html

For Windows maybe this might help C - create file in memory

2
  • Thanks Zelix. I am using linux. I think "fmemopen" will work for me. I will tryout and share the outcome.
    – namith
    Aug 17, 2014 at 17:44
  • This is working great for some part of the library. The problem is happening in the place where file descriptor is being used rather than FILE pointer. Unfortunately, fmemopen FILE handle does not has file descriptor (i.e. fileno() returns -1). Any suggestions?
    – namith
    Sep 11, 2014 at 18:50
0

I don't know what is the exact purpose of changing legacy code.The issue which is I understand is the overhead caused by reading and writing. But there are many methods are available to resolve overhead issues as:

  1. As already told you can use fmemopen
  2. You may also use mmap for plain read/write should make little difference; either way, everything happens through the filesystem cache/buffers.
  3. You can also use tmpfs to leverage memory as (temporary) files also known as RAMDisk as storage. As the files are washed out easily as files are temporary already in nature
  4. Another solution - you can use inmemory database (TimesTen for sample)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.