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

namith
  • 546
  • 1
  • 8
  • 18
  • Both methods should work fine. For me I would go with the first approach as it will probably be more elegant. – Zaid Amir Aug 17 '14 at 13:25
  • Not sure, but maybe this will help you. http://stackoverflow.com/questions/16815785/is-it-possible-to-create-a-c-file-object-to-read-write-in-memory/16815884#16815884 –  Aug 17 '14 at 13:26
  • For your reference: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/open_memstream.html Linux: http://man7.org/linux/man-pages/man3/open_memstream.3.html – alk Aug 17 '14 at 13:35
  • Why are you doing this? Any modern operating system already virtualizes file access. You'll make your program *slower* by doubling the RAM usage for the file content. – Hans Passant Aug 17 '14 at 13:36
  • Thanks for the reply I will tryout this. I am making this change for some security reasons not for performance. – namith Aug 17 '14 at 17:42

2 Answers2

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

Community
  • 1
  • 1
Zelix
  • 1,914
  • 1
  • 14
  • 11
  • Thanks Zelix. I am using linux. I think "fmemopen" will work for me. I will tryout and share the outcome. – namith Aug 17 '14 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 '14 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)
Vineet1982
  • 7,730
  • 4
  • 32
  • 67