ByteArray.st
changeset 19048 0929f428048d
parent 18966 3bc73a4cdae6
child 19054 80cbbad08d0c
child 19134 eaa91cb0ef1b
--- a/ByteArray.st	Tue Jan 19 18:57:38 2016 +0100
+++ b/ByteArray.st	Wed Jan 20 02:04:16 2016 +0100
@@ -164,7 +164,6 @@
 ! !
 
 
-
 !ByteArray class methodsFor:'queries'!
 
 elementByteSize
@@ -185,7 +184,6 @@
     "Modified: 23.4.1996 / 15:56:25 / cg"
 ! !
 
-
 !ByteArray methodsFor:'Compatibility-Squeak'!
 
 bitXor:aByteArray
@@ -673,6 +671,9 @@
 wordAt:index
     "return the 2-bytes starting at index as an (unsigned) Integer.
      The value is retrieved in the machines natural byte order
+     Notice: 
+        the index is a byte index; thus, this allows for unaligned access to
+        words on any boundary.
      Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)"
 
 %{  /* NOCONTEXT */
@@ -680,38 +681,38 @@
     REGISTER int indx;
     int nIndex;
     union {
-	unsigned char u_char[2];
-	unsigned short u_ushort;
+        unsigned char u_char[2];
+        unsigned short u_ushort;
     } val;
     unsigned char *byteP;
 
     if (__isSmallInteger(index)) {
-	indx = __intVal(index);
-	if (indx > 0) {
-	    if (!__isByteArrayLike(self))
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
-	    nIndex = __byteArraySize(self);
-	    if ((indx+1) <= nIndex) {
-		byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
+        indx = __intVal(index);
+        if (indx > 0) {
+            if (!__isByteArrayLike(self))
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
+            nIndex = __byteArraySize(self);
+            if ((indx+1) <= nIndex) {
+                byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
 #if defined(__i386__) || defined(UNALIGNED_FETCH_OK)
-		val.u_ushort = ((unsigned short *)byteP)[0];
+                val.u_ushort = ((unsigned short *)byteP)[0];
 #else
-		/*
-		 * mhmh to be measured:
-		 *   the if may hurt more than the additional
-		 *   memory cycles on some machines ...
-		 */
-		if (((INT)byteP & 1) == 0) {
-		    /* aligned */
-		    val.u_ushort = ((unsigned short *)byteP)[0];
-		} else {
-		    val.u_char[0] = byteP[0];
-		    val.u_char[1] = byteP[1];
-		}
+                /*
+                 * mhmh to be measured:
+                 *   the if may hurt more than the additional
+                 *   memory cycles on some machines ...
+                 */
+                if (((INT)byteP & 1) == 0) {
+                    /* aligned */
+                    val.u_ushort = ((unsigned short *)byteP)[0];
+                } else {
+                    val.u_char[0] = byteP[0];
+                    val.u_char[1] = byteP[1];
+                }
 #endif
-		RETURN ( __mkSmallInteger((val.u_ushort)) );
-	    }
-	}
+                RETURN ( __mkSmallInteger((val.u_ushort)) );
+            }
+        }
     }
 %}.
     ^ super wordAt:index
@@ -721,6 +722,9 @@
     "return the 2-bytes starting at index as an (unsigned) Integer.
      The value is retrieved MSB (high 8 bits at lower index) if msb is true;
      LSB-first (i.e. low 8-bits at lower byte index) if its false.
+     Notice: 
+        the index is a byte index; thus, this allows for unaligned access to
+        words on any boundary.
      Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)"
 
 %{  /* NOCONTEXT */
@@ -731,59 +735,59 @@
     unsigned char *byteP;
 
     if (__isSmallInteger(index)) {
-	indx = __intVal(index);
-	if (indx > 0) {
-	    if (!__isByteArrayLike(self))
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
-	    nIndex = __byteArraySize(self);
-	    if ((indx+1) <= nIndex) {
-		byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
-		if (msb == true) {
-		    /*
-		     * most significant byte first (i.e sparc order)
-		     */
+        indx = __intVal(index);
+        if (indx > 0) {
+            if (!__isByteArrayLike(self))
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
+            nIndex = __byteArraySize(self);
+            if ((indx+1) <= nIndex) {
+                byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
+                if (msb == true) {
+                    /*
+                     * most significant byte first (i.e sparc order)
+                     */
 #if defined(__MSBFIRST__)
-		    /*
-		     * mhmh to be measured:
-		     *   the if may hurt more than the additional
-		     *   memory cycles on some machines ...
-		     */
-		    if (((INT)byteP & 1) == 0) {
-			/* aligned */
-			val = ((unsigned short *)byteP)[0];
-		    } else
+                    /*
+                     * mhmh to be measured:
+                     *   the if may hurt more than the additional
+                     *   memory cycles on some machines ...
+                     */
+                    if (((INT)byteP & 1) == 0) {
+                        /* aligned */
+                        val = ((unsigned short *)byteP)[0];
+                    } else
 #endif
-		    {
-			val = byteP[0];
-			val = (val << 8) + byteP[1];
-		    }
-		} else {
-		    /*
-		     * least significant byte first (i.e i386/alpha order)
-		     */
+                    {
+                        val = byteP[0];
+                        val = (val << 8) + byteP[1];
+                    }
+                } else {
+                    /*
+                     * least significant byte first (i.e i386/alpha order)
+                     */
 #if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK))
-		    val = ((unsigned short *)byteP)[0];
+                    val = ((unsigned short *)byteP)[0];
 #else
 # if defined(__LSBFIRST__)
-		    /*
-		     * mhmh to be measured:
-		     *   the if may hurt more than the additional
-		     *   memory cycles on some machines ...
-		     */
-		    if (((INT)byteP & 1) == 0) {
-			/* aligned */
-			val = ((unsigned short *)byteP)[0];
-		    } else
+                    /*
+                     * mhmh to be measured:
+                     *   the if may hurt more than the additional
+                     *   memory cycles on some machines ...
+                     */
+                    if (((INT)byteP & 1) == 0) {
+                        /* aligned */
+                        val = ((unsigned short *)byteP)[0];
+                    } else
 # endif
-		    {
-			val = byteP[1];
-			val = (val << 8) + byteP[0];
-		    }
+                    {
+                        val = byteP[1];
+                        val = (val << 8) + byteP[0];
+                    }
 #endif
-		}
-		RETURN ( __mkSmallInteger(val) );
-	    }
-	}
+                }
+                RETURN ( __mkSmallInteger(val) );
+            }
+        }
     }
 %}.
     ^ super wordAt:index MSB:msb
@@ -795,6 +799,9 @@
      The value is stored in the machines natural byteorder,
      i.e. this method should only be used to fill byteArrays which are
      used internally (not passed to other machines).
+     Notice: 
+        the index is a byte index; thus, this allows for unaligned access to
+        words on any boundary.
      Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)"
 
 %{  /* NOCONTEXT */
@@ -803,41 +810,41 @@
     int nIndex;
     int v;
     union {
-	unsigned char u_char[2];
-	unsigned short u_ushort;
+        unsigned char u_char[2];
+        unsigned short u_ushort;
     } val;
     unsigned char *byteP;
 
     if (__bothSmallInteger(index, value)) {
-	indx = __intVal(index);
-	if (indx > 0) {
-	    if (!__isByteArrayLike(self))
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
-	    nIndex = __byteArraySize(self);
-	    if ((indx+1) <= nIndex) {
-		val.u_ushort = v = __intVal(value);
-		if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) {
-		    byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
+        indx = __intVal(index);
+        if (indx > 0) {
+            if (!__isByteArrayLike(self))
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
+            nIndex = __byteArraySize(self);
+            if ((indx+1) <= nIndex) {
+                val.u_ushort = v = __intVal(value);
+                if ((v & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) {
+                    byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
 #if defined(__i386__) || defined(UNALIGNED_FETCH_OK)
-		    ((unsigned short *)byteP)[0] = val.u_ushort;
+                    ((unsigned short *)byteP)[0] = val.u_ushort;
 #else
-		    /*
-		     * mhmh to be measured:
-		     *   the if may hurt more than the additional
-		     *   memory cycles on some machines ...
-		     */
-		    if (((INT)byteP & 1) == 0) {
-			/* aligned */
-			((unsigned short *)byteP)[0] = val.u_ushort;
-		    } else {
-			byteP[0] = val.u_char[0];
-			byteP[1] = val.u_char[1];
-		    }
+                    /*
+                     * mhmh to be measured:
+                     *   the if may hurt more than the additional
+                     *   memory cycles on some machines ...
+                     */
+                    if (((INT)byteP & 1) == 0) {
+                        /* aligned */
+                        ((unsigned short *)byteP)[0] = val.u_ushort;
+                    } else {
+                        byteP[0] = val.u_char[0];
+                        byteP[1] = val.u_char[1];
+                    }
 #endif
-		    RETURN ( value );
-		}
-	    }
-	}
+                    RETURN ( value );
+                }
+            }
+        }
     }
 %}.
     ^ super wordAt:index put:value
@@ -856,6 +863,9 @@
      The stored value must be in the range 0 .. 16rFFFF.
      The value is stored LSB-first (i.e. the low 8bits are stored at the
      lower index) if msb is false, MSB-first otherwise.
+     Notice: 
+        the index is a byte index; thus, this allows for unaligned access to
+        words on any boundary.
      Question: should it accept signed values ? (see ByteArray>>signedWordAt:put:)"
 
 %{  /* NOCONTEXT */
@@ -867,62 +877,62 @@
     unsigned char *byteP;
 
     if (__bothSmallInteger(index, value)) {
-	indx = __intVal(index);
-	if (indx > 0) {
-	    if (!__isByteArrayLike(self))
-		indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
-	    nIndex = __byteArraySize(self);
-	    if ((indx+1) <= nIndex) {
-		val = __intVal(value);
-		if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) {
-		    byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
-		    if (msb == true) {
-			/*
-			 * most significant byte first (i.e sparc order)
-			 */
+        indx = __intVal(index);
+        if (indx > 0) {
+            if (!__isByteArrayLike(self))
+                indx += __OBJS2BYTES__(__intVal(__ClassInstPtr(__qClass(self))->c_ninstvars));
+            nIndex = __byteArraySize(self);
+            if ((indx+1) <= nIndex) {
+                val = __intVal(value);
+                if ((val & ~0xFFFF) == 0 /* i.e. (val >= 0) && (val <= 0xFFFF) */) {
+                    byteP = (unsigned char *)(__ByteArrayInstPtr(self)->ba_element) + indx - 1;
+                    if (msb == true) {
+                        /*
+                         * most significant byte first (i.e sparc order)
+                         */
 #if defined(__MSBFIRST__)
-			/*
-			 * mhmh to be measured:
-			 *   the if may hurt more than the additional
-			 *   memory cycles on some machines ...
-			 */
-			if (((INT)byteP & 1) == 0) {
-			    /* aligned */
-			    ((unsigned short *)byteP)[0] = val;
-			} else
+                        /*
+                         * mhmh to be measured:
+                         *   the if may hurt more than the additional
+                         *   memory cycles on some machines ...
+                         */
+                        if (((INT)byteP & 1) == 0) {
+                            /* aligned */
+                            ((unsigned short *)byteP)[0] = val;
+                        } else
 #endif
-			{
-			    byteP[1] = val & 0xFF;
-			    byteP[0] = (val>>8) & 0xFF;
-			}
-		    } else {
-			/*
-			 * least significant byte first (i.e i386/alpha order)
-			 */
+                        {
+                            byteP[1] = val & 0xFF;
+                            byteP[0] = (val>>8) & 0xFF;
+                        }
+                    } else {
+                        /*
+                         * least significant byte first (i.e i386/alpha order)
+                         */
 #if defined(__i386__) || (defined(__LSBFIRST__) && defined(UNALIGNED_FETCH_OK))
-			((unsigned short *)byteP)[0] = val;
+                        ((unsigned short *)byteP)[0] = val;
 #else
 # if defined(__LSBFIRST__)
-			/*
-			 * mhmh to be measured:
-			 *   the if may hurt more than the additional
-			 *   memory cycles on some machines ...
-			 */
-			if (((INT)byteP & 1) == 0) {
-			    /* aligned */
-			    ((unsigned short *)byteP)[0] = val;
-			} else
+                        /*
+                         * mhmh to be measured:
+                         *   the if may hurt more than the additional
+                         *   memory cycles on some machines ...
+                         */
+                        if (((INT)byteP & 1) == 0) {
+                            /* aligned */
+                            ((unsigned short *)byteP)[0] = val;
+                        } else
 # endif
-			{
-			    byteP[0] = val & 0xFF;
-			    byteP[1] = (val>>8) & 0xFF;
-			}
+                        {
+                            byteP[0] = val & 0xFF;
+                            byteP[1] = (val>>8) & 0xFF;
+                        }
 #endif
-		    }
-		    RETURN ( value );
-		}
-	    }
-	}
+                    }
+                    RETURN ( value );
+                }
+            }
+        }
     }
 %}.
     ^ super wordAt:index put:value MSB:msb
@@ -2923,7 +2933,6 @@
     "
 ! !
 
-
 !ByteArray methodsFor:'searching'!
 
 indexOf:aByte startingAt:start
@@ -2989,7 +2998,6 @@
     "
 ! !
 
-
 !ByteArray methodsFor:'testing'!
 
 isByteArray