Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 17 Dec 1996 08:19:33 -0600
From:      "Eric L. Hernes" <erich@lodgenet.com>
To:        "Marc G. Fournier" <scrappy@hub.org>
Cc:        Terry Lambert <terry@lambert.org>, hackers@freebsd.org
Subject:   Re: Almost have mmap() figured, I think... 
Message-ID:  <199612171419.IAA08984@jake.lodgenet.com>
In-Reply-To: Your message of "Mon, 16 Dec 1996 21:38:34 EST." <Pine.NEB.3.95.961216212824.283H-100000@hub.org> 

next in thread | previous in thread | raw e-mail | index | archive | help
"Marc G. Fournier" writes:
>On Mon, 16 Dec 1996, Terry Lambert wrote:
>
>> start = mmap( 0, FRAMESIZE * FRAMES, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0
>));
>> for( i = 0; i < FRAMES; i++)
>> 	frame[i] = start + i * FRAMESIZE;
>>
>	Okay, that one makes sense...
>
>	Then, would it make sense to also have:
>
>size = mmap( 0, sizeof(int) * FRAMES, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)
>);
>for(i = 0; i < FRAMES; i++)
>  framesize[i] = start + i * sizeof(int);
                  ^^^^^ you mean size, right?

I think you could just make framesize an `int *' rather than an `int []',
then `framesize = mmap(0, sizeof(int)*FRAMES ...);' and skip the for loop.

then you should be able to refer to framesize[frameno] just as before...
I think Terry was assuming that all your frames were the same size, I'm
not sure that they are.  It's a little more complicated when they're
different sizes, but not impossible.  you'll want to do something like

int *framesize, total;
caddr_t alldata, framedata[FRAMES];

framesize = mmap(0, sizeof(int) * FRAMES, ..., sizefd, 0);
for(i=0; i<FRAMES; i++){
  total+=framesize[i];
}

alldata = mmap(0, total, ..., datafd, 0);

for(i=0; i<FRAMES; i++){
  framedata[i]=alldata+framesize[i];
}

now framedata[i] is a pointer to the i'th frames data.

`sizefd' is the file descriptor to the file containing the frame sizes
`datafd' is the file descriptor to the (contigious) frame data.

from the first mail:

>	The problem comes up when I try and print out the frame itself,
>using 'printf("%s\n", frame[ii]);'...the value printed out each time
>is exactly the same: <FF><D8><FF><E0>

this is probably right as Terry pointed out.

>
>	Ack, okay...just tried something quickly, and changed %s to %d, and
>the results are:
>
>hub> ./client
>Frame == 000, ii == 00, datasize == 1944, Size == 1944
>134840320
 ^^^^^^^^^  this should be an address.  %s dereferences a pointer, %d prints
the value.  You could gdb attach to the process and examin the memory
at this address just to see, it should start with <FF><D8><FF><EO>
(I think ;-) )

>	So...am I accurate in assuming that what I'm getting is the memory
>location?  I still don't understand this fully, so please bear with me...I'm
>*purely* guessing here :(

Yes, mmap returns a pointer to the data, not the data directly. You
can (have to) use it as any other pointer.  Just as if you had malloc'ed
it.  These use structures, because it's easier to show when a pointer
is being dereferenced versus just an offset of with structures than
arrays.

correct:
struct mystruct *mystructp;
mystructp=mmap(...);

/*useful stuff with mystructp->members */

*incorrect*:
struct mystruct mystruct;
mystruct=mmap(...);

/* useful stuff with mystruct.members*/

this will generate a compiler warning, because you're stuffing a pointer
into a structure.  (The first will too, but only because the mmap should
have a cast)

also correct (but clumsier):
struct mystruct mystruct, *mystructp;
mystructp=mmap(...);
bcopy(mystructp, &mystruct, sizeof(mystruct));

/* useful stuff with mystruct.members */

>
>	So, I'm making the assumption that either the way I'm calling, or
>using, readn() is incorrect for use with mmap()'d regions...would I be
>better off to read in the socket, then memcpy() the data, or strncpy()?  Or
>is what I'm missing even more simple then that?

No readn() should be ok.  You can read directly instead of read/memcpy.
strncpy() is almost certainly wrong unless you can be guaranteed there
are no zeros in your data ;-)

>
>Marc G. Fournier                                 scrappy@hub.org
>Systems Administrator @ hub.org              scrappy@freebsd.org
>
>

eric.
-- 
erich@lodgenet.com
http://rrnet.com/~erich erich@rrnet.com






Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199612171419.IAA08984>