Linux-Development-Sys Digest #559, Volume #8     Mon, 12 Mar 01 08:13:08 EST

Contents:
  How to use C++ to develope the linux driver module? ("¼B¤_ºÕ")
  How to use C++ to develope kernel module? ("¼B¤_ºÕ")
  Re: How to use C++ to develope the linux driver module? ("Peter T. Breuer")
  Re: How to use C++ to develope the linux driver module? ("¼B¤_ºÕ")
  Re: How to use C++ to develope kernel module? (Erik de Castro Lopo)
  Re: How to use C++ to develope the linux driver module? (Aminudin Khalid)
  C language (Aminudin Khalid)
  Re: C language (Josef Moellers)
  what is the hotest newsgroup that discuss linux kernel  sourcecode  ?? ("hushui")
  Re: How to use C++ to develope kernel module? (Alexander Viro)
  Re: How to use C++ to develope kernel module? (Erik de Castro Lopo)
  Re: semaphore questions, two different ones (Michael Kerrisk)
  NFS. Mount from user program. ("Erlend L. Kvalvaag")
  Re: NFS. Mount from user program. ("Erlend L. Kvalvaag")
  Re: why is mmap() unhappy?? (Kasper Dupont)
  Re: NFS. Mount from user program. ("Erlend L. Kvalvaag")
  Re: what is the hotest newsgroup that discuss linux kernel  sourcecode  ?? 
([EMAIL PROTECTED])
  Re: How to use C++ to develope kernel module? ([EMAIL PROTECTED])
  Re: why is mmap() unhappy?? ([EMAIL PROTECTED])
  Re: Can linux be trusted? (pete)
  Re: why is mmap() unhappy?? ([EMAIL PROTECTED])

----------------------------------------------------------------------------

From: "¼B¤_ºÕ" <[EMAIL PROTECTED]>
Subject: How to use C++ to develope the linux driver module?
Date: Mon, 12 Mar 2001 14:25:54 +0800

Hello,
   I need to port a driver module written in C++  to linux . But
I found some problem. For example, the function "printk" will
not show results on screen; the "udelay" will causes "unresolved
symbols" when I install the module. But if I change the files name
from *.cpp to *.c, the problems are all disappeared.
  Can  C++  be used in developing kernel module? and if yes, how
can I do to fix the problems?

ex:
 g++ -c test.cpp -o test.o -O2

 // test.cpp
   #define MODULE
   #include <linux/module.h>
   #include <linux/kernel.h>
   #include <linux/delay.h>
   int init_module(void) {
     int i;
     for ( i =0; i<10 ; i++){
        udelay(1000000);
        printk("Test %d\n",i);
     }
     return(0);
   }
   void cleanup_module(void)  {
      printk("Unloaded!\n");
  }




------------------------------

From: "¼B¤_ºÕ" <[EMAIL PROTECTED]>
Subject: How to use C++ to develope kernel module?
Date: Mon, 12 Mar 2001 14:27:10 +0800

Hello,
   I need to port a driver module written in C++  to linux . But
I found some problem. For example, the function "printk" will
not show results on screen; the "udelay" will causes "unresolved
symbols" when I install the module. But if I change the files name
from *.cpp to *.c, the problems are all disappeared.
  Can  C++  be used in developing kernel module? and if yes, how
can I do to fix the problems?

ex:
 g++ -c test.cpp -o test.o -O2

 // test.cpp
   #define MODULE
   #include <linux/module.h>
   #include <linux/kernel.h>
   #include <linux/delay.h>
   int init_module(void) {
     int i;
     for ( i =0; i<10 ; i++){
        udelay(1000000);
        printk("Test %d\n",i);
     }
     return(0);
   }
   void cleanup_module(void)  {
      printk("Unloaded!\n");
  }






------------------------------

From: "Peter T. Breuer" <[EMAIL PROTECTED]>
Subject: Re: How to use C++ to develope the linux driver module?
Date: Mon, 12 Mar 2001 07:38:56 +0100

<[EMAIL PROTECTED]> wrote:
>   Can  C++  be used in developing kernel module? and if yes, how

No (short answer).

> can I do to fix the problems?

You can implement C++ memory and exception handling in the kernel.
Etc.

Peter

------------------------------

From: "¼B¤_ºÕ" <[EMAIL PROTECTED]>
Subject: Re: How to use C++ to develope the linux driver module?
Date: Mon, 12 Mar 2001 14:56:33 +0800

I see.
Thanks for your advice.



------------------------------

From: Erik de Castro Lopo <[EMAIL PROTECTED]>
Subject: Re: How to use C++ to develope kernel module?
Date: Mon, 12 Mar 2001 07:14:48 GMT

=BCB=A4_=BA=D5 wrote:
> =

> Hello,
>    I need to port a driver module written in C++  to linux . But
> I found some problem. For example, the function "printk" will
> not show results on screen; the "udelay" will causes "unresolved
> symbols" when I install the module. But if I change the files name
> from *.cpp to *.c, the problems are all disappeared.
>   Can  C++  be used in developing kernel module? and if yes, how
> can I do to fix the problems?

C++ and the Linux kernel do not mix. =


Port the code to C and try that.

Erik
-- =

=================================================================
Erik de Castro Lopo     [EMAIL PROTECTED]      (Yes its valid)
=================================================================
"I love deadlines. I especially like the whooshing sound =

they make as they go flying by."  -- Douglas Adams

------------------------------

From: Aminudin Khalid <[EMAIL PROTECTED]>
Subject: Re: How to use C++ to develope the linux driver module?
Date: Mon, 12 Mar 2001 23:19:08 +0800
Reply-To: [EMAIL PROTECTED]

Hi ,

"less /var/log/messages "
You  will see the prink output .

TQ




"¼B¤_ºÕ" wrote:

> Hello,
>    I need to port a driver module written in C++  to linux . But
> I found some problem. For example, the function "printk" will
> not show results on screen; the "udelay" will causes "unresolved
> symbols" when I install the module. But if I change the files name
> from *.cpp to *.c, the problems are all disappeared.
>   Can  C++  be used in developing kernel module? and if yes, how
> can I do to fix the problems?
>
>


------------------------------

From: Aminudin Khalid <[EMAIL PROTECTED]>
Subject: C language
Date: Mon, 12 Mar 2001 23:45:04 +0800
Reply-To: [EMAIL PROTECTED]


==============56B7E208F64306D2DF6D94B3
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,

Please explain the following C language statement.Thank you.

  //declaration

  typedef BOOLEAN (*Open)(void, unsigned long , int);


  //execute

  ((Open)(buffer->port_open))(buffer->context, 1,port_number);


==============56B7E208F64306D2DF6D94B3
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>

<pre>Hi,</pre>

<pre></pre>

<pre>Please explain the following C language statement.Thank you.</pre>

<pre>

&nbsp; //declaration&nbsp;

&nbsp; typedef BOOLEAN (*Open)(void, unsigned long , int);&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp; //execute&nbsp;

&nbsp; ((Open)(buffer->port_open))(buffer->context, 1,port_number);&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
</pre>

<pre></pre>
</html>

==============56B7E208F64306D2DF6D94B3==


------------------------------

From: Josef Moellers <[EMAIL PROTECTED]>
Subject: Re: C language
Date: Mon, 12 Mar 2001 08:47:03 +0100

Aminudin Khalid wrote:
> =

> Hi,
> =

> Please explain the following C language statement.Thank you.
> =

>   //declaration
> =

>   typedef BOOLEAN (*Open)(void, unsigned long , int);

This defines a new type: "Open" will be a pointer to a function
returning a BOOLEAN. I doubt that the "void" argument is valid, though.
Most likely, this will have been a "void *", i.e. a generic pointer.

>   //execute
> =

>   ((Open)(buffer->port_open))(buffer->context, 1,port_number);

This takes whatever is in buffer->port_open, casts this into an "Open",
i.e. a pointer to a function returning a BOOLEAN, and calls this
function.

-- =

Josef M=F6llers (Pinguinpfleger bei FSC)
        If failure had no penalty success would not be a prize
                                                -- T.  Pratchett

------------------------------

From: "hushui" <[EMAIL PROTECTED]>
Subject: what is the hotest newsgroup that discuss linux kernel  sourcecode  ??
Date: Mon, 12 Mar 2001 14:57:29 +0800

I want to join it.
I am a new learner.




------------------------------

From: [EMAIL PROTECTED] (Alexander Viro)
Subject: Re: How to use C++ to develope kernel module?
Date: 12 Mar 2001 02:50:46 -0500

In article <[EMAIL PROTECTED]>,
¼B¤_ºÕ <[EMAIL PROTECTED]> wrote:
>Hello,
>   I need to port a driver module written in C++  to linux . But
>I found some problem. For example, the function "printk" will
>not show results on screen; the "udelay" will causes "unresolved
>symbols" when I install the module. But if I change the files name
>from *.cpp to *.c, the problems are all disappeared.
>  Can  C++  be used in developing kernel module? and if yes, how
>can I do to fix the problems?

Write in C.

-- 
"You're one of those condescending Unix computer users!"
"Here's a nickel, kid.  Get yourself a better computer" - Dilbert.

------------------------------

From: Erik de Castro Lopo <[EMAIL PROTECTED]>
Subject: Re: How to use C++ to develope kernel module?
Date: Mon, 12 Mar 2001 08:02:38 GMT

=BCB=A4_=BA=D5 wrote:
> =

> Hello,
>    I need to port a driver module written in C++  to linux . But
> I found some problem. For example, the function "printk" will
> not show results on screen; the "udelay" will causes "unresolved
> symbols" when I install the module. But if I change the files name
> from *.cpp to *.c, the problems are all disappeared.
>   Can  C++  be used in developing kernel module? and if yes, how
> can I do to fix the problems?


C++ and the Linux kernel do not mix. =


Port the code to C and try that.

Erik
-- =

=================================================================
Erik de Castro Lopo     [EMAIL PROTECTED]      (Yes its valid)
=================================================================
"Don't be fooled by NT/Exchange propaganda. M$ Exchange is =

just plain broken and NT cannot handle the sustained load =

of a high-volume remote mail server"  =

-- Eric S. Raymond in the Fetchmail FAQ

------------------------------

From: [EMAIL PROTECTED] (Michael Kerrisk)
Subject: Re: semaphore questions, two different ones
Date: Mon, 12 Mar 2001 10:06:54 GMT

On Mon, 5 Mar 2001 12:39:07 -0800, "Dan Miller" <[EMAIL PROTECTED]>
wrote:

>I'm trying to use semaphores for the first time, mainly using Stevens (Adv.
>Prog. in Unix Env) as reference.  I initially thought it was pretty
>straight-forward, but I'm having some problems that I don't understand...
>
>1. I created a semaphore successfully, but in my shutdown routine I want to
>release to release the semaphore.  I initially was trying to use semop(...
>IPC_RMID), but this fails with "invalid argument".  Based on an example
>elsewhere on the web, I instead used semctl(... IPC_RMID)... this succeeded,
>but I noticed that on subsequent semaphore acquisitions (when I reload my
>daemon), the semaphore number is jumping by about 120 per load.  I expected
>it to increase by one at each instance, didn't I??  Why is this happening??

This is an implementation detail - it doesn't really matter.  There is
no requirement of guarantee that the IDs be sequential.


>And what do I have to put in the sembuf struct if I want to use semop() for
>this purpose??

Using semctl(... IPC_RMID) is the correct way of doing this - you
can't use semop() for this purpose.

Cheers

Michael

------------------------------

From: "Erlend L. Kvalvaag" <[EMAIL PROTECTED]>
Crossposted-To: comp.os.linux.networking,comp.unix.programmer
Subject: NFS. Mount from user program.
Date: Mon, 12 Mar 2001 13:16:11 +0100

Hi,

I am trying to mount a remote filesystem from a c-program (se
attachment).
I use the mount(2) command.

I have successfully made a similar program
which is able to mount a local filesystem like e.g a floppydrive with
an msdos filesystem.

I am also able to mount remote filesystems
from my OS prompt (Linux version 2.2.16-22)

When I execute the attached program, I get :

Mar 12 12:50:09 janus kernel: nfs_read_super: get root fattr failed

in my /var/log/messages, and the mount call gives an "Invalid argument".

As far as I have understood it is the NFS server which is supposed to
fill
in the root file handle. I am able to mount exactly the same filesystem
from
my OS prompt.

Why do I get these error-messages, and what do I do wrong ?

Regards
Erlend Kvalvaag
Department of Informatics
University of Oslo
NORWAY


------------------------------

From: "Erlend L. Kvalvaag" <[EMAIL PROTECTED]>
Crossposted-To: comp.os.linux.networking,comp.unix.programmer
Subject: Re: NFS. Mount from user program.
Date: Mon, 12 Mar 2001 13:18:31 +0100

This is a multi-part message in MIME format.
==============10F302C7676D4CEDEB65A754
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Sorry, I forgot the attachment...............

here it is.



"Erlend L. Kvalvaag" wrote:

> Hi,
>
> I am trying to mount a remote filesystem from a c-program (se
> attachment).
> I use the mount(2) command.
>
> I have successfully made a similar program
> which is able to mount a local filesystem like e.g a floppydrive with
> an msdos filesystem.
>
> I am also able to mount remote filesystems
> from my OS prompt (Linux version 2.2.16-22)
>
> When I execute the attached program, I get :
>
> Mar 12 12:50:09 janus kernel: nfs_read_super: get root fattr failed
>
> in my /var/log/messages, and the mount call gives an "Invalid argument".
>
> As far as I have understood it is the NFS server which is supposed to
> fill
> in the root file handle. I am able to mount exactly the same filesystem
> from
> my OS prompt.
>
> Why do I get these error-messages, and what do I do wrong ?
>
> Regards
> Erlend Kvalvaag
> Department of Informatics
> University of Oslo
> NORWAY

==============10F302C7676D4CEDEB65A754
Content-Type: text/plain; charset=us-ascii;
 name="hack.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="hack.c"


#define NFS

#include <sys/param.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#include "/usr/src/linux/include/linux/nfs.h"
#include "/usr/src/linux/include/linux/nfs_mount.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <errno.h>

int main (argc, argv)
     int argc;
     char *argv[];
{
  struct nfs_mount_data nfsargs;
  struct hostent* servptr;
  struct in_addr* addr_ptr;

  if (argc != 6) {
    fprintf (stderr, "Usage: ./hack remotehost :remotefs localhost localmntpoint 
2049\n");
    fprintf (stderr, "example: ./hack nfs.remote.com :/exports my.home.host 
/mnt/import 2049\n");
    exit (1);
  }
  if ((umount (argv[4]) < 0) && (errno != EINVAL)) {
    perror ("umount");
    exit (1);
  }
  printf("this machine : %s\n", argv[3]);
  if ((servptr = gethostbyname(argv[3])) == NULL) {
    printf("gethostbyname error\n");
    exit(1);
  }
  addr_ptr = (struct in_addr*) *(servptr->h_addr_list);

  memset (&nfsargs, 0, sizeof (nfsargs));
  nfsargs.version = NFS_MOUNT_VERSION;

  nfsargs.addr.sin_family = AF_INET;
  nfsargs.addr.sin_port = htons (atoi (argv[5]));     /* port */
  nfsargs.addr.sin_addr.s_addr = addr_ptr->s_addr; /* client host */

  if ((nfsargs.fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror ("socket");
    exit (1);
  }
  printf("socket done\n");


  if ((bindresvport (nfsargs.fd, 0)) < 0) {
    perror ("bindresvport");
    exit (1);
  }
  printf("bindresvport done \n");

  if ((connect(nfsargs.fd, (struct sockaddr *)&nfsargs.addr, sizeof(nfsargs.addr))) < 
0) {
    perror ("connect");
    exit (1);
  }
  printf("connect done \n");

  nfsargs.flags = NFS_MOUNT_SOFT | NFS_MOUNT_INTR | NFS_MOUNT_NONLM;
  nfsargs.rsize = 1024;/* NFS_DEF_FILE_IO_BUFFER_SIZE; ? */
  nfsargs.wsize = 1024;/* NFS_DEF_FILE_IO_BUFFER_SIZE; */
  nfsargs.timeo = 7;
  nfsargs.retrans = 3;
  nfsargs.acregmin = 3;
  nfsargs.acregmax = 60;
  nfsargs.acdirmin = 30;
  nfsargs.acdirmax = 60;
  strcpy (nfsargs.hostname, argv[1]); /* server name */
  nfsargs.namlen = 512;
  nfsargs.bsize = 0;
  
  /*  nfsargs.root = ???????????????????????????? */

  printf("Trying mount......\n");
  if (mount (strcat(argv[1], argv[2]), argv[4], "nfs", 0xC0ED0000 | nfsargs.flags , 
&nfsargs) <
      0) {
    perror ("mount error ");
    exit (1);
  }
  printf("Success\n");
  exit (0);
}

==============10F302C7676D4CEDEB65A754==


------------------------------

From: Kasper Dupont <[EMAIL PROTECTED]>
Subject: Re: why is mmap() unhappy??
Date: Mon, 12 Mar 2001 11:49:07 +0000

Alexander Viro wrote:
> 
> In article <[EMAIL PROTECTED]>,
>  <[EMAIL PROTECTED]> wrote:
> 
> >Before there was POSIX, some systems were just not as well designed.
> >Now that there is POSIX, perhaps this has not improved?
> >
> >So suggest something that works.
> 
> Suggestion: stop bothering with Missed'em'V shared memory and use the
> right thing. Namely, create a file on tmpfs, mmap it and unlink when
> you want it to become invisible.  Will work on Solaris, *BSD and Linux (2.4)
> (s/tmpfs/ramfs). Normal semantics of unlink() applies - when the last
> user of file with i_nlink==0 releases it the file is destroyed.

That will not use swap space, only physical memory.

> 
> That's how shared memory should've been implemented in the first place,
> instead of wanking with additional syscalls.

I never claimed that SysV shared memory was well
designed. But until someone comes up with something
better, I will have to live with it.

> 
> --
> "You're one of those condescending Unix computer users!"
> "Here's a nickel, kid.  Get yourself a better computer" - Dilbert.

-- 
Kasper Dupont

------------------------------

From: "Erlend L. Kvalvaag" <[EMAIL PROTECTED]>
Crossposted-To: comp.os.linux.networking,comp.unix.programmer
Subject: Re: NFS. Mount from user program.
Date: Mon, 12 Mar 2001 13:50:47 +0100

If this hellps, on the console I get:

nfs: RPC call returned error 111
RPC: task of released still queued!
RPC: (task is on xprt_pending)
nfs_read_super: get root fattr failed





------------------------------

From: [EMAIL PROTECTED]
Subject: Re: what is the hotest newsgroup that discuss linux kernel  sourcecode  ??
Date: Mon, 12 Mar 2001 12:14:31 -0000

On Mon, 12 Mar 2001 14:57:29 +0800 hushui <[EMAIL PROTECTED]> wrote:

| I want to join it.
| I am a new learner.

You're here.  I'm assuming you don't want to subscribe to the mailing
list.  As a lerner, read everything.  Eventually you could ask questions
to get clarifications of things that posters assumed each other would
already know about.  But watch what people talk about first.

-- 
=================================================================
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |
| [EMAIL PROTECTED] | Texas, USA | http://phil.ipal.org/     |
=================================================================

------------------------------

From: [EMAIL PROTECTED]
Subject: Re: How to use C++ to develope kernel module?
Date: Mon, 12 Mar 2001 12:29:41 -0000

On 12 Mar 2001 02:50:46 -0500 Alexander Viro <[EMAIL PROTECTED]> wrote:

| In article <[EMAIL PROTECTED]>,
| ¼B¤_ºÕ <[EMAIL PROTECTED]> wrote:
|>Hello,
|>   I need to port a driver module written in C++  to linux . But
|>I found some problem. For example, the function "printk" will
|>not show results on screen; the "udelay" will causes "unresolved
|>symbols" when I install the module. But if I change the files name
|>from *.cpp to *.c, the problems are all disappeared.
|>  Can  C++  be used in developing kernel module? and if yes, how
|>can I do to fix the problems?
|
| Write in C.

Yes.

To the original poster:

OO languages like C++ are intended to take an OO design and make it
simple and straightforward to code the design right into an abstract
syntax.  IMHO, C++ does a half-assed job of this; something like Java
does it better.  There are plenty of other choices.

A non-OO language like C can still be done in an object oriented way,
but you now have to think about how your objects (which should have
been determined in the design phase ... which doesn't exclude going
back to design phase to revise it if you need to in extreme programming
style) are actually implemented.  C forces you to consider details that
C++ allows, and Java requires, you to ignore.

But detail is what it is all about when coding an operating system
kernel, especially at the device driver level.  If what you have is
inherintly more abstract, for example converting a scanned image from
a scanner into a particular image format, then that part should be
done at a higher level and not as a device driver.

-- 
=================================================================
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |
| [EMAIL PROTECTED] | Texas, USA | http://phil.ipal.org/     |
=================================================================

------------------------------

From: [EMAIL PROTECTED]
Subject: Re: why is mmap() unhappy??
Date: Mon, 12 Mar 2001 12:49:07 -0000

On 11 Mar 2001 23:39:03 -0500 Alexander Viro <[EMAIL PROTECTED]> wrote:
| In article <[EMAIL PROTECTED]>,
|  <[EMAIL PROTECTED]> wrote:
|
|>Before there was POSIX, some systems were just not as well designed.
|>Now that there is POSIX, perhaps this has not improved?
|>
|>So suggest something that works.
|
| Suggestion: stop bothering with Missed'em'V shared memory and use the
| right thing. Namely, create a file on tmpfs, mmap it and unlink when
| you want it to become invisible.  Will work on Solaris, *BSD and Linux (2.4)
| (s/tmpfs/ramfs). Normal semantics of unlink() applies - when the last
| user of file with i_nlink==0 releases it the file is destroyed.

This falls under the "does not work" for me.  The reason is because standard
system administration practices in determining how much space is to be
partitioned for each filesystem or swap space does not assume that program
virtual memory goes to any backing store other than swap space.  Further,
most systems allow swap space to be scattered among multiple partitions
very easily.  Doing so for /tmp is difficult and rarely done.

While not every place does this very normal practice, I do not want to be
the one to impose "something strange" onto them by distributing a library
which then causes programs to start eating up tons of /tmp space when all
their swap space goes unused.


| That's how shared memory should've been implemented in the first place,
| instead of wanking with additional syscalls. 

If it had been done this way from day one, great.  Once you have this
implemented in every major UNIX and those new implementations deployed
on 99% of the servers, let me know.  The world will be a better place
on that day.

Actually, the way I implemented my library code is that it accepts a
file name from the calling program.  If no name is given, it checks
for a name given on a specific environment variable.  If still no name
is available, then it uses SysV shared memory.  If it did get a name,
it first tries that name as a pattern with mkstemp().  If that works
it goes with that open file.  If that fails, it uses open() on the
raw name.  So at least the programmer and user do have a choice.  I
think that is the best I can do, and still not exceed the standard
expectation that program space uses swap space.

I have not decided what that environment variable name should be.
Do you have a suggestion?

Source code is at the bottom of:  http://phil.ipal.org/freeware/vrb/

-- 
=================================================================
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |
| [EMAIL PROTECTED] | Texas, USA | http://phil.ipal.org/     |
=================================================================

------------------------------

From: pete <[EMAIL PROTECTED]>
Crossposted-To: 
comp.lang.c,comp.os.linux.development.apps,comp.sys.be.programmer,comp.sys.mac.programmer.misc,comp.unix.bsd.freebsd.misc,comp.unix.bsd.misc,gnu.gcc
Subject: Re: Can linux be trusted?
Date: Mon, 12 Mar 2001 07:54:04 -0500
Reply-To: [EMAIL PROTECTED]

Peter da Silva wrote:
 
> By making this an auto, you're initialising it to 0 every pass through
> the program, and thus your realloc is equivalent to a malloc. 
> Your original code was correct, just a little obscure. 
> This code works but will leak memory
> every time through since the allocated memory is discarded.

Thanks. I see what you mean.
I was trying to use realloc(), so that I wouldn't have to free
the memory everytime Bits() was used. But after thinking about it,
it seems too complicated. I've never written anything before
where I tried to free dynamically allocated memory from outside the
function, where it was allocated.
I think I'm happy doing it this way now.
The syntax for calling Bits() and freeing it's memeory now,
are similar to the standard C library function, strdup()

/* BEGIN, bytebits.c */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

unsigned char *Bits(void const *p, size_t bytes);

int main(void)
{
    float a;
    char *s = "(float)%f = %s\n", *t;

    printf("\n");
    for(a = -2.0; a < 2.5; a += 0.5){   
        t = Bits(&a, sizeof a);
        printf(s, a, t);
        free(t);
    }     
    a += 0.1;
    t = Bits(&a, sizeof a);
    printf(s, a, t);
    free(t);
    return 0;
}

unsigned char *Bits(void const *p, size_t bytes)
{
    unsigned char *a, *b, mask;

    a = b = malloc(CHAR_BIT * bytes + 1);       
    if(!a)
        printf("malloc() failure in Bits()\n"), exit(EXIT_FAILURE); 
    while(bytes--)
        for(mask = 1 << CHAR_BIT - 1; mask; mask >>= 1)
            *a++ = ((char*)p)[bytes] & mask? '1': '0';
    *a = '\0';
    return b;   
}

/* END, bytebits.c */

-- 
 pete

------------------------------

From: [EMAIL PROTECTED]
Subject: Re: why is mmap() unhappy??
Date: Mon, 12 Mar 2001 13:08:05 -0000

On Mon, 12 Mar 2001 11:49:07 +0000 Kasper Dupont <[EMAIL PROTECTED]> wrote:
| Alexander Viro wrote:
...
|> That's how shared memory should've been implemented in the first place,
|> instead of wanking with additional syscalls.
|
| I never claimed that SysV shared memory was well
| designed. But until someone comes up with something
| better, I will have to live with it.

If it had been specified this way at the beginning:

  Open file "/dev/zero" and mmap() calls against the same open instance,
  e.g. that same file descriptor, shall refer to the very same object of
  memory, while mmap() calls against a different open instance in the same
  or a different process, shall refer to a separate memory object.  This
  means that if a parent opens "/dev/zero" and then forks a child, and
  then both processes do mmap() to the same relative offset on the same
  open instance (the same file descriptor the child inherited, even if
  its numeric value is changed by dup() or dup2() in either the parent
  or the child) then they each have memory with data shared between
  them.  If the relative offsets and size result in partial overlap
  of that virtual memory object, then that part overlapping is shared.

then I think it might have worked quite well to get it into swap space.
Needs for totally disjoint space can always be easily done by another
open instance.  My big question would be, how hard would it be to do
this?  And would it break anything that expects a different behaviour?

It does work this way if you mk[s]temp() a file, open it, and unlink
it as soon as opened.  The trouble is there is no standard way to get
this into swap space.  If every system had a standard /swapfs which
always made files that go into swap space and vanish on reboot, then it
might work (but I can see other problems with that way).  One could use
an appropriate ramdisk, ramfs, or mfs facility, but these are not done
in standard ways across systems, do not always swap out, and may have
limitations (such as how big ramdisk is initialized to).

In addition to my previous statements elsewhere about problems in using
filesystem space for this due to the way space allocations might be done,
I realise yet another reason to prefer swap space over filesystem space.
As long as the process with the filesystem as backing store is running,
that filesystem cannot either be unmounted or remouned read/only.  That
surely is not a commonly needed thing, but it would certainly annoy the
hell out of me if I were to run into it.

-- 
=================================================================
| Phil Howard - KA9WGN |   Dallas   | http://linuxhomepage.com/ |
| [EMAIL PROTECTED] | Texas, USA | http://phil.ipal.org/     |
=================================================================

------------------------------


** FOR YOUR REFERENCE **

The service address, to which questions about the list itself and requests
to be added to or deleted from it should be directed, is:

    Internet: [EMAIL PROTECTED]

You can send mail to the entire list by posting to the
comp.os.linux.development.system newsgroup.

Linux may be obtained via one of these FTP sites:
    ftp.funet.fi                                pub/Linux
    tsx-11.mit.edu                              pub/linux
    sunsite.unc.edu                             pub/Linux

End of Linux-Development-System Digest
******************************

Reply via email to