[haiku-commits] haiku: hrev45231 - src/add-ons/kernel/drivers/random

  • From: korli@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 4 Feb 2013 12:15:09 +0100 (CET)

hrev45231 adds 1 changeset to branch 'master'
old head: 401bf7a0b5b1c49618c7cdc1e7c9a5d0b2609647
new head: 7ab100361f753dd1d81a85c9235e04a47f98eda5
overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=7ab1003+%5E401bf7a

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

7ab1003: /dev/random: style changes and use MutexLocker.

                                   [ Jerome Duval <jerome.duval@xxxxxxxxx> ]

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

Revision:    hrev45231
Commit:      7ab100361f753dd1d81a85c9235e04a47f98eda5
URL:         http://cgit.haiku-os.org/haiku/commit/?id=7ab1003
Author:      Jerome Duval <jerome.duval@xxxxxxxxx>
Date:        Sun Feb  3 15:47:57 2013 UTC

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

1 file changed, 11 insertions(+), 14 deletions(-)
src/add-ons/kernel/drivers/random/driver.cpp | 25 +++++++++++-------------

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

diff --git a/src/add-ons/kernel/drivers/random/driver.cpp 
b/src/add-ons/kernel/drivers/random/driver.cpp
index 194702e..92066ed 100644
--- a/src/add-ons/kernel/drivers/random/driver.cpp
+++ b/src/add-ons/kernel/drivers/random/driver.cpp
@@ -17,6 +17,8 @@
 #include <lock.h>
 #include <thread.h>
 
+#include <util/AutoLock.h>
+
 
 //#define TRACE_DRIVER
 #ifdef TRACE_DRIVER
@@ -374,11 +376,9 @@ publish_devices(void)
 device_hooks *
 find_device(const char* name)
 {
-       int     i;
-
        TRACE((DRIVER_NAME ": find_device(\"%s\")\n", name));
 
-       for (i = 0; sRandomNames[i] != NULL; i++)
+       for (int i = 0; sRandomNames[i] != NULL; i++)
                if (strcmp(name, sRandomNames[i]) == 0)
                        return &sRandomHooks;
 
@@ -401,12 +401,9 @@ random_open(const char *name, uint32 flags, void **cookie)
 static status_t
 random_read(void *cookie, off_t position, void *_buffer, size_t *_numBytes)
 {
-       int32 *buffer = (int32 *)_buffer;
-       uint8 *buffer8 = (uint8 *)_buffer;
-       uint32 i, j;
        TRACE((DRIVER_NAME ": read(%Ld,, %ld)\n", position, *_numBytes));
 
-       mutex_lock(&sRandomLock);
+       MutexLocker locker(&sRandomLock);
        sRandomCount += *_numBytes;
 
        /* Reseed if we have or are gonna use up > 1/16th the entropy around */
@@ -418,12 +415,13 @@ random_read(void *cookie, off_t position, void *_buffer, 
size_t *_numBytes)
        /* ToDo: Yes, i know this is not the way we should do it. What we 
really should do is
         * take the md5 or sha1 hash of the state of the pool, and return that. 
Someday.
         */
-       for (i = 0; i < (*_numBytes) / 4; i++)
+       int32 *buffer = (int32 *)_buffer;
+       uint32 i;
+       for (i = 0; i < *_numBytes / 4; i++)
                buffer[i] = chrand32(sRandomEnv);
-       for (j = 0; j < (*_numBytes) % 4; j++)
-               buffer8[(i*4) + j] = chrand8(sRandomEnv);
-
-       mutex_unlock(&sRandomLock);
+       uint8 *buffer8 = (uint8 *)_buffer;
+       for (uint32 j = 0; j < *_numBytes % 4; j++)
+               buffer8[(i * 4) + j] = chrand8(sRandomEnv);
 
        return B_OK;
 }
@@ -433,13 +431,12 @@ static status_t
 random_write(void *cookie, off_t position, const void *buffer, size_t 
*_numBytes)
 {
        TRACE((DRIVER_NAME ": write(%Ld,, %ld)\n", position, *_numBytes));
-       mutex_lock(&sRandomLock);
+       MutexLocker locker(&sRandomLock);
        OCTET* data = (OCTET*)buffer;
        for (size_t i = 0; i < *_numBytes / sizeof(OCTET); i++) {
                chseed(sRandomEnv, data->Q[0]);
                data++;
        }
-       mutex_unlock(&sRandomLock);
        return B_OK;
 }
 


Other related posts:

  • » [haiku-commits] haiku: hrev45231 - src/add-ons/kernel/drivers/random - korli