File pointer to a memory location?

Thomas Tornblom thomas at uplog.se
Wed Sep 12 19:05:20 AEST 1990


In article <119609 at linus.mitre.org> rtidd at ccels3.mitre.org (Randy Tidd) writes:


   With all these new comp.unix.* groups coming out, I hope it's
   appropriate to cross-post to .programmer and .internals...

   Anyhow, in the application i'm working on I have a series of routines
   that were written by someone else that do image processing (the fbm
   library, if you're familiar with it), including source.  These
   routines take a file pointer as an argument, and they read the
   image-to-be-processed through this fp.  Normally what you do is open a
   file and pass in a file pointer to it, or pass in stdin and pipe your
   image through the program through the shell.

   The problem is in my application, I am using a database and thus don't
   have files and thus I don't have file pointers either.  What I *can*
   do is query the database for an image, and it gives me a block of
   memory that holds the image *exactly* as it is stored in file form.

   So what I have is a block of memory, having exactly the same size and
   contents of a file. What I have to do is pass my image processing
   routines a file pointer that points to this block of memory.  If the
   routines used file *descriptors*, it wouldn't be a problem because I
   could just use pipes and be done with it.

You can make a fp from a fd by fp = fdopen(fd, type) it would however
require all the data to pass through the kernel once more.

   What I do now is query the database, get a block of memory, dump this
   memory to a temporary file, open the file with a file pointer, and
   pass the file pointer to the image processing routines.  Not only is
   this dumb, but images can be a big as 3 megs and this is incredibly
   inefficient.

Depending on the system you are using and whether you are concerned
with portability it could be done by faking up an _iobuf struct (the things
fp:s point at) that would hold a pointer to the block of memory and
have all the rest of the members in the struct set up to some sane value.

Something along the line of:
(Warning this is untested and non portable)
---------------
#include <stdio.h>

struct _iobuf fake;

FILE *getimage()
{
	char *image;
/* read the image from the db and have image point at it */
	fake._ptr = fake._base = image;
	fake._cnt = fake._bufsiz = image_size;
	fake._flag = _IOREAD | _IOMYBUF;
	fake._file = -1;
	return &fake;
}
---------------



More information about the Comp.unix.programmer mailing list