ExternalBytes.st
changeset 19860 324edacff5cc
parent 19512 e31128e0b135
child 19863 513bd7237fe7
child 20138 cd6fdc7534e4
--- a/ExternalBytes.st	Tue May 17 00:56:14 2016 +0200
+++ b/ExternalBytes.st	Tue May 17 02:04:25 2016 +0200
@@ -36,7 +36,7 @@
 #endif
 
     extern char *__stx_malloc(size_t);
-    extern char *__stx_calloc(size_t, size_t); 
+    extern char *__stx_calloc(size_t, size_t);
     extern char *__stx_realloc(char *, size_t);
     extern void __stx_free(char *);
     extern void __stx_mallocStatistics(void);
@@ -47,9 +47,9 @@
 %{
 
 struct mallocList {
-        char *chunk;
-        size_t size;
-        struct mallocList *next;
+	char *chunk;
+	size_t size;
+	struct mallocList *next;
 };
 static struct mallocList *mallocList = (struct mallocList *)0;
 static INT mallocCount = 0;
@@ -63,25 +63,25 @@
     if (@global(DebugMalloc) != true) return;
 
     if (ptr) {
-        found = 0;
-        for (this=mallocList, prev=0; this; this=next) {
-            next = this->next;
-            if (this->chunk == ptr) {
-                if (prev) {
-                    prev->next = next;
-                } else {
-                    mallocList = next;
-                }
-                free(this);
-                found++;
-                mallocCount--;
-            } else {
-                prev = this;
-            }
-        }
-        if (! found) {
-            console_printf("ExternalBytes [warning]: **** free: alien %"_lx_" (allocated somewhere else ?))\n", (INT)ptr);
-        }
+	found = 0;
+	for (this=mallocList, prev=0; this; this=next) {
+	    next = this->next;
+	    if (this->chunk == ptr) {
+		if (prev) {
+		    prev->next = next;
+		} else {
+		    mallocList = next;
+		}
+		free(this);
+		found++;
+		mallocCount--;
+	    } else {
+		prev = this;
+	    }
+	}
+	if (! found) {
+	    console_printf("ExternalBytes [warning]: **** free: alien %"_lx_" (allocated somewhere else ?))\n", (INT)ptr);
+	}
     }
 }
 
@@ -95,82 +95,82 @@
     if (@global(DebugMalloc) != true) return;
 
     if (ptr) {
-        found = 0;
-        for (this=mallocList; this; this=this->next) {
-            if (this->chunk == ptr) {
-                console_printf("ExternalBytes [warning]: **** %016"_lx_" already allocated (freed somewhere else ?)\n", (INT)ptr);
-                found++;
-            }
-        }
-        if (! found) {
-            e = (struct mallocList *) malloc(sizeof(struct mallocList));
-            e->next = mallocList;
-            e->chunk = ptr;
-            e->size = nBytes;
-            mallocList = e;
-            mallocCount++;
-        }
+	found = 0;
+	for (this=mallocList; this; this=this->next) {
+	    if (this->chunk == ptr) {
+		console_printf("ExternalBytes [warning]: **** %016"_lx_" already allocated (freed somewhere else ?)\n", (INT)ptr);
+		found++;
+	    }
+	}
+	if (! found) {
+	    e = (struct mallocList *) malloc(sizeof(struct mallocList));
+	    e->next = mallocList;
+	    e->chunk = ptr;
+	    e->size = nBytes;
+	    mallocList = e;
+	    mallocCount++;
+	}
     }
 }
 
 char *
 __stx_malloc(size_t nBytes) {
-        char *ptr = malloc(nBytes);
+	char *ptr = malloc(nBytes);
 
-        if (@global(TraceMalloc) == true) {
-            console_printf("ExternalBytes [info]: allocated %d bytes at: %016"_lx_"\n", nBytes, (INT)ptr);
-        }
-        addToMallocList(ptr, nBytes);
+	if (@global(TraceMalloc) == true) {
+	    console_printf("ExternalBytes [info]: allocated %d bytes at: %016"_lx_"\n", nBytes, (INT)ptr);
+	}
+	addToMallocList(ptr, nBytes);
 
-        return ptr;
+	return ptr;
 }
 
 char *
 __stx_calloc(size_t n, size_t size) {
-        char *ptr = __stx_malloc(n * size);
-        if (ptr != 0) {
-            bzero(ptr, (n * size));
-        }
-        return ptr;
+	char *ptr = __stx_malloc(n * size);
+	if (ptr != 0) {
+	    bzero(ptr, (n * size));
+	}
+	return ptr;
 }
 
 char *
 __stx_realloc(char *ptr, size_t nBytes)
 {
-        char *newPtr;
+	char *newPtr;
 
-        removeFromMallocList(ptr);
-        newPtr = realloc(ptr, nBytes);
-        addToMallocList(newPtr, nBytes);
+	removeFromMallocList(ptr);
+	newPtr = realloc(ptr, nBytes);
+	addToMallocList(newPtr, nBytes);
 
-        if (@global(TraceMalloc) == true) {
-            console_printf("ExternalBytes [info]: realloc %d bytes for %"_lx_" at: %"_lx_"\n", nBytes, (INT)ptr, (INT)newPtr);
-        }
-        return newPtr;
+	if (@global(TraceMalloc) == true) {
+	    console_printf("ExternalBytes [info]: realloc %d bytes for %"_lx_" at: %"_lx_"\n", nBytes, (INT)ptr, (INT)newPtr);
+	}
+	return newPtr;
 }
 
 void
 __stx_free(char *ptr)
 {
-        if (@global(TraceMalloc) == true) {
-            console_printf("ExternalBytes: free bytes at: %"_lx_"\n", (INT)ptr);
-        }
-        removeFromMallocList(ptr);
+	if (@global(TraceMalloc) == true) {
+	    console_printf("ExternalBytes: free bytes at: %"_lx_"\n", (INT)ptr);
+	}
+	removeFromMallocList(ptr);
 
-        free(ptr);
+	free(ptr);
 }
 
 void
 __stx_mallocStatistics() {
-        struct mallocList *this;
-        int amount = 0;
-        int n = 0;
+	struct mallocList *this;
+	int amount = 0;
+	int n = 0;
 
-        for (this=mallocList; this; this=this->next) {
-            n++;
-            amount += this->size;
-        }
-        console_printf("ExternalBytes [info]: allocated %d blocks with %d bytes overall\n", n, amount);
+	for (this=mallocList; this; this=this->next) {
+	    n++;
+	    amount += this->size;
+	}
+	console_printf("ExternalBytes [info]: allocated %d blocks with %d bytes overall\n", n, amount);
 }
 
 %}
@@ -393,13 +393,13 @@
 
 address:anAddressInteger
     "return a new ExternalBytes object to access bytes starting at anAddressInteger.
-     The memory at anAddressInteger has been allocated elsewhere. 
+     The memory at anAddressInteger has been allocated elsewhere.
      The size is not known, therefore byte accesses will NOT be checked for valid index.
      Use this, if you get a pointer from some external source (such as a
      C-callBack function) and you have to extract bytes from that.
 
      DANGER ALERT: this method allows very bad things to be done to the
-                   system - use with GREAT care (better: do not use it)"
+		   system - use with GREAT care (better: do not use it)"
 
     ^ self basicNew setAddress:anAddressInteger size:nil
 
@@ -408,13 +408,13 @@
 
 address:anAddressInteger size:size
     "return a new ExternalBytes object to access bytes starting at anAddressInteger.
-     The memory at anAddressInteger has been allocated elsewhere. 
+     The memory at anAddressInteger has been allocated elsewhere.
      The size is known, which allows byte accesses to be checked for valid index.
      Use this, if you get a pointer to a structure from some external source
      (such as a C-callBack function) and you have to extract things from that.
 
      DANGER ALERT: this method allows very bad things to be done to the
-                   system - use with GREAT care (better: do not use it)"
+		   system - use with GREAT care (better: do not use it)"
 
     ^ self basicNew setAddress:anAddressInteger size:size
 
@@ -477,7 +477,7 @@
 
     extBytes := self new:((nChars+1)*2).
     1 to:nChars do:[:i |
-        extBytes unsignedInt16At:(i*2) put:(aString at:i) codePoint.
+	extBytes unsignedInt16At:(i*2) put:(aString at:i) codePoint.
     ].
     extBytes unsignedInt16At:((nChars+1)*2) put:0.
     ^ extBytes
@@ -688,7 +688,7 @@
      If the machine does not support them, return nil."
 
 %{  /* NOCONTEXT */
-#if defined(__GNUC__) || defined(WIN32)
+#if defined(__GNUC__) || defined(__CLANG__) || defined(__win32__)
     RETURN (__mkSmallInteger( sizeof(long double)));
 #endif
 %}.
@@ -885,8 +885,8 @@
     idx := 1.
     s := WriteStream on:Unicode16String new.
     [(word := self unsignedInt16At:idx) ~~ 0] whileTrue:[
-        s nextPut:(Character value:word).
-        idx := idx + 2.
+	s nextPut:(Character value:word).
+	idx := idx + 2.
     ].
     ^ s contents
 !
@@ -1102,7 +1102,7 @@
     "replace elements from aString, and add a 0-byte at the end"
 
     |nChars|
-    
+
     nChars := aString size.
     self replaceBytesFrom:1 to:nChars with:aString startingAt:1.
     self at:nChars+1 put:0.
@@ -1145,7 +1145,7 @@
 %{  /* NOCONTEXT */
     char *mem = (char *)__INST(address_);
     if (mem && (OBJ)mem != nil) {
-        __stx_free(mem);
+	__stx_free(mem);
     }
     __INST(address_) = __INST(size) = nil;
 %}
@@ -1210,29 +1210,29 @@
     "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
     "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
     (aGCOrStream isStream) ifFalse:[
-        ^ super displayOn:aGCOrStream
+	^ super displayOn:aGCOrStream
     ].
 
     aGCOrStream nextPutAll:self className.
     addr := self address.
     addr isNil ifTrue:[
-        aGCOrStream nextPutAll:'[free]'.
+	aGCOrStream nextPutAll:'[free]'.
     ] ifFalse:[
-        size notNil ifTrue:[
-            aGCOrStream nextPutAll:'[sz:'.
-            size printOn:aGCOrStream.
-            aGCOrStream space.
-        ] ifFalse:[
-            aGCOrStream nextPut:$[.
-        ].
-        aGCOrStream nextPutAll:'@'.
-        addr printOn:aGCOrStream base:16.
-        aGCOrStream nextPut:$].
+	size notNil ifTrue:[
+	    aGCOrStream nextPutAll:'[sz:'.
+	    size printOn:aGCOrStream.
+	    aGCOrStream space.
+	] ifFalse:[
+	    aGCOrStream nextPut:$[.
+	].
+	aGCOrStream nextPutAll:'@'.
+	addr printOn:aGCOrStream base:16.
+	aGCOrStream nextPut:$].
     ].
 
     "
-        self new printString
-        (self new:5) displayString
+	self new printString
+	(self new:5) displayString
     "
 
 
@@ -1301,26 +1301,26 @@
      * Fail if already allocated
      */
     if (__INST(address_) == nil && __isSmallInteger(numberOfBytes)) {
-        INT nBytes = __smallIntegerVal(numberOfBytes);
-        if (nBytes > 0) {
-            char *space = __stx_malloc(nBytes);
-            if (space) {
-                if (doClear == true) {
-                    bzero(space, nBytes);
-                }
-                __INST(address_) = (OBJ)space;
-                __INST(size) = numberOfBytes;
-                RETURN(self);
-            } else {
-                mallocFailure = true;
-            }
-        }
+	INT nBytes = __smallIntegerVal(numberOfBytes);
+	if (nBytes > 0) {
+	    char *space = __stx_malloc(nBytes);
+	    if (space) {
+		if (doClear == true) {
+		    bzero(space, nBytes);
+		}
+		__INST(address_) = (OBJ)space;
+		__INST(size) = numberOfBytes;
+		RETURN(self);
+	    } else {
+		mallocFailure = true;
+	    }
+	}
     }
 %}.
     mallocFailure == true ifTrue:[
-        ^ MallocFailure raiseRequestWith:numberOfBytes.
+	^ MallocFailure raiseRequestWith:numberOfBytes.
     ] ifFalse:[
-        self primitiveFailed.
+	self primitiveFailed.
     ].
 ! !
 
@@ -1445,34 +1445,34 @@
 
 %{
     if (__isSmallInteger(numberOfBytes)) {
-        INT nBytes = __smallIntegerVal(numberOfBytes);
-        if (nBytes > 0) {
-            char *space;
-            char *prevSpace = (char *)__INST(address_);
+	INT nBytes = __smallIntegerVal(numberOfBytes);
+	if (nBytes > 0) {
+	    char *space;
+	    char *prevSpace = (char *)__INST(address_);
 
-            if (prevSpace == (char *)nil)
-                prevSpace = 0;  /* allocate from scratch */
-            space = __stx_realloc(prevSpace, nBytes);
-            if (space) {
-                __INST(address_) = (OBJ)space;
-                __INST(size) = numberOfBytes;
-                if (space == prevSpace) {
-                    /* same address, no re-registration */
-                    RETURN(self);
-                }
-                mallocStatus = true;
-            } else {
-                mallocStatus = false;
-            }
-        }
+	    if (prevSpace == (char *)nil)
+		prevSpace = 0;  /* allocate from scratch */
+	    space = __stx_realloc(prevSpace, nBytes);
+	    if (space) {
+		__INST(address_) = (OBJ)space;
+		__INST(size) = numberOfBytes;
+		if (space == prevSpace) {
+		    /* same address, no re-registration */
+		    RETURN(self);
+		}
+		mallocStatus = true;
+	    } else {
+		mallocStatus = false;
+	    }
+	}
     }
 %}.
     mallocStatus == true ifTrue:[
-        Lobby registerChange:self.
+	Lobby registerChange:self.
     ] ifFalse:[mallocStatus == false ifTrue:[
-        ^ MallocFailure raiseRequestWith:numberOfBytes.
+	^ MallocFailure raiseRequestWith:numberOfBytes.
     ] ifFalse:[
-        self primitiveFailed.
+	self primitiveFailed.
     ]].
 ! !