# FreeBSD Device driver books (notes)



## darkshadow (Mar 9, 2013)

Hello,

I read  the book (FreeBSD device driver), but I'm not sure if the writer intend to make examples easier to read, for example :-


```
static int
echo_write(struct cdev *dev, struct uio *uio, int ioflag)
{
        int error = 0;

        error = copyin(uio->uio_iov->iov_base,
 echo_message->buffer,
            MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1));
        if (error != 0) {
                uprintf("Write failed.\n");
                return (error);
        }

      *(echo_message->buffer +
            MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1)) = 0;

      echo_message->length = MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1);

        return (error);
}
```

He used MIN so many times, despite doing this will increase  calculation  time and make code plotted , and it can be easily written as :-


```
static int
echo_write(struct cdev *dev, struct uio *uio, int ioflag)
{
        int error = 0;
	int length  = MIN(uio->uio_iov->iov_len, BUFFER_SIZE - 1);
        error = copyin(uio->uio_iov->iov_base, echo_message->buffer, length);
        if (error != 0) {
                uprintf("Write failed.\n");
                return (error);
        }
      	echo_message->buffer[length] = '\0';
      	echo_message->length = length;
        return (error);
}
```

*W*hich is more readable for me? Does anybody share the same point with me? *O*r does he make it that way for some reason I don't understand? 

There are other examples but I don't feel we have mush space here to list them out?


----------



## ananm1 (Mar 9, 2013)

Yes that makes sense. Have you tested it to see if there is a difference in performance?


----------

