]> git.saurik.com Git - apple/xnu.git/blobdiff - libkern/gen/OSAtomicOperations.c
xnu-517.3.7.tar.gz
[apple/xnu.git] / libkern / gen / OSAtomicOperations.c
index 735fa2ac3719d70c5a453d1b8e03bf299884134e..222db2a333e870bb4a259b867978684131c7ace9 100644 (file)
@@ -3,19 +3,22 @@
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this
+ * file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -96,45 +99,23 @@ UInt32      OSBitXorAtomic(UInt32 mask, UInt32 * value)
        return OSBitwiseAtomic((UInt32) -1, 0, mask, value);
 }
 
-
-static Boolean OSCompareAndSwap8(UInt8 oldValue8, UInt8 newValue8, UInt8 * value8)
+static Boolean OSCompareAndSwap8(UInt8 oldValue8, UInt8 newValue8, UInt8 * value8)
 {
-       UInt32          mask = 0x000000ff;
-       UInt32          newbits = (UInt32) newValue8;
-       int                     shift;
-       UInt32          alignment = ((UInt32) value8) & (sizeof(UInt32) - 1);
-       UInt32          oldValue;
-       UInt32          newValue;
-       UInt32 *        value;
-
-       switch (alignment) {
-               default:
-                       // assert(false);
-               case 0:
-                       value = (UInt32 *) value8;
-                       shift = 24;
-                       break;
-               case 1:
-                       value = (UInt32 *) (value8 + 1);
-                       shift = 16;
-                       break;
-               case 2:
-                       value = (UInt32 *) (value8 + 2);
-                       shift = 8;
-                       break;
-               case 3:
-                       value = (UInt32 *) (value8 + 3);
-                       shift = 0;
-                       break;
-       }
+       UInt32          mask        = 0x000000ff;
+       UInt32          alignment   = ((UInt32) value8) & (sizeof(UInt32) - 1);
+    UInt32      shiftValues = (24 << 24) | (16 << 16) | (8 << 8);
+    int                        shift       = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
+       UInt32 *        value32     = (UInt32 *) (value8 - alignment);
+    UInt32      oldValue;
+    UInt32      newValue;
 
-       mask <<= shift;
-       newbits <<= shift;
-       
-       oldValue = *value;
-       newValue = (oldValue & ~mask) | (newbits & mask);
-       
-       return OSCompareAndSwap(oldValue, newValue, value);
+    mask <<= shift;
+
+    oldValue = *value32;
+    oldValue = (oldValue & ~mask) | (oldValue8 << shift);
+    newValue = (oldValue & ~mask) | (newValue8 << shift);
+
+       return OSCompareAndSwap(oldValue, newValue, value32);
 }
 
 static Boolean OSTestAndSetClear(UInt32 bit, Boolean wantSet, UInt8 * startAddress)
@@ -253,34 +234,23 @@ UInt8     OSBitXorAtomic8(UInt32 mask, UInt8 * value)
        return OSBitwiseAtomic8((UInt32) -1, 0, mask, value);
 }
 
-
-static Boolean OSCompareAndSwap16(UInt16 oldValue16, UInt16 newValue16, UInt16 * value16)
+static Boolean OSCompareAndSwap16(UInt16 oldValue16, UInt16 newValue16, UInt16 * value16)
 {
-       UInt32          mask = 0x0000ffff;
-       UInt32          newbits = (UInt32) newValue16;
-       int                     shift;
-       UInt32          alignment = ((UInt32) value16) & (sizeof(UInt32) - 1);
-       UInt32          oldValue;
-       UInt32          newValue;
-       UInt32 *        value;
-
-       if (alignment == 2) {
-               value = (UInt32 *) (value16 - 1);
-               shift = 0;
-       }
-       else {
-               // assert(alignment == 0);
-               value = (UInt32 *) value16;
-               shift = 16;
-       }
-
-       mask <<= shift;
-       newbits <<= shift;
-       
-       oldValue = *value;
-       newValue = (oldValue & ~mask) | (newbits & mask);
-       
-       return OSCompareAndSwap(oldValue, newValue, value);
+       UInt32          mask        = 0x0000ffff;
+       UInt32          alignment   = ((UInt32) value16) & (sizeof(UInt32) - 1);
+    UInt32      shiftValues = (16 << 24) | (16 << 16);
+    UInt32             shift       = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
+       UInt32 *        value32     = (UInt32 *) (((UInt32) value16) - alignment);
+    UInt32      oldValue;
+    UInt32      newValue;
+
+    mask <<= shift;
+
+    oldValue = *value32;
+    oldValue = (oldValue & ~mask) | (oldValue16 << shift);
+    newValue = (oldValue & ~mask) | (newValue16 << shift);
+
+       return OSCompareAndSwap(oldValue, newValue, value32);
 }
 
 SInt16 OSIncrementAtomic16(SInt16 * value)