From owner-freebsd-hackers Tue Dec 17 06:20:18 1996 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.4/8.8.4) id GAA25693 for hackers-outgoing; Tue, 17 Dec 1996 06:20:18 -0800 (PST) Received: from bacall.lodgenet.com (bacall.lodgenet.com [205.138.147.242]) by freefall.freebsd.org (8.8.4/8.8.4) with SMTP id GAA25680 for ; Tue, 17 Dec 1996 06:20:09 -0800 (PST) Received: (from mail@localhost) by bacall.lodgenet.com (8.6.12/8.6.12) id IAA13689; Tue, 17 Dec 1996 08:18:47 -0600 Received: from garbo.lodgenet.com(204.124.123.250) by bacall via smap (V1.3) id sma013679; Tue Dec 17 08:18:42 1996 Received: from jake.lodgenet.com (jake.lodgenet.com [10.0.11.30]) by garbo.lodgenet.com (8.6.12/8.6.9) with ESMTP id IAA03773; Tue, 17 Dec 1996 08:19:03 -0600 Received: from jake.lodgenet.com (localhost [127.0.0.1]) by jake.lodgenet.com (8.8.3/8.6.12) with ESMTP id IAA08984; Tue, 17 Dec 1996 08:19:33 -0600 (CST) Message-Id: <199612171419.IAA08984@jake.lodgenet.com> X-Mailer: exmh version 1.6.9 8/22/96 To: "Marc G. Fournier" cc: Terry Lambert , hackers@freebsd.org Subject: Re: Almost have mmap() figured, I think... In-reply-to: Your message of "Mon, 16 Dec 1996 21:38:34 EST." Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Tue, 17 Dec 1996 08:19:33 -0600 From: "Eric L. Hernes" Sender: owner-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk "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 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: 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 (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