--- a/ExternalBytes.st Fri Mar 18 07:45:27 2016 +0000
+++ b/ExternalBytes.st Mon Mar 21 07:50:50 2016 +0000
@@ -9,6 +9,8 @@
other person. No title to or ownership of the software is
hereby transferred.
"
+'From Smalltalk/X, Version:6.2.5.0 on 18-03-2016 at 10:24:59' !
+
"{ Package: 'stx:libbasic' }"
"{ NameSpace: Smalltalk }"
@@ -22,8 +24,15 @@
!ExternalBytes primitiveDefinitions!
%{
-#include <stdlib.h>
-#include <stdio.h>
+ #include <stdlib.h>
+ #include <stdio.h>
+ #include <malloc.h>
+
+ extern char *__stx_malloc(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);
%}
! !
@@ -31,16 +40,15 @@
%{
struct mallocList {
- char *chunk;
- unsigned size;
- struct mallocList *next;
+ char *chunk;
+ size_t size;
+ struct mallocList *next;
};
static struct mallocList *mallocList = (struct mallocList *)0;
static INT mallocCount = 0;
static void
-removeFromMallocList(ptr)
- char *ptr;
+removeFromMallocList(char *ptr)
{
struct mallocList *this, *prev, *next;
int found = 0;
@@ -48,31 +56,30 @@
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);
+ }
}
}
static void
-addToMallocList(ptr, nBytes)
- char *ptr;
+addToMallocList(char *ptr, size_t nBytes)
{
struct mallocList *e, *this;
void *malloc();
@@ -81,90 +88,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(nBytes) {
- char *ptr;
- void *malloc();
+__stx_malloc(size_t nBytes) {
+ char *ptr = malloc(nBytes);
- 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);
- }
+ return ptr;
+}
- addToMallocList(ptr, nBytes);
-
- 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 *
-__stx_calloc(n, size) {
- char *ptr;
-
- ptr = __stx_malloc(n * size);
- if (ptr != (char *)0) {
- bzero(ptr, (n * size));
- }
- return ptr;
-}
+__stx_realloc(char *ptr, size_t nBytes)
+{
+ char *newPtr;
-char *
-__stx_realloc(ptr, nBytes)
- char *ptr;
-{
- 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(ptr)
- char *ptr;
+__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);
}
%}
@@ -1106,10 +1105,9 @@
free the associated heap memory with it"
%{ /* NOCONTEXT */
-
- char *mem = (char *)(__INST(address_));
+ char *mem = (char *)__INST(address_);
if (mem && (OBJ)mem != nil) {
- __stx_free(mem);
+ __stx_free(mem);
}
__INST(address_) = __INST(size) = nil;
%}
@@ -1261,34 +1259,30 @@
|mallocFailure|
%{
- char *space;
- unsigned int nBytes;
- char *__stx_malloc();
-
/*
* Fail if already allocated
*/
if (__INST(address_) == nil && __isSmallInteger(numberOfBytes)) {
- nBytes = __smallIntegerVal(numberOfBytes);
- if (nBytes > 0) {
- 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.
].
! !
@@ -1412,37 +1406,35 @@
size == numberOfBytes ifTrue:[^ self].
%{
- char *space, *prevSpace;
- unsigned int nBytes;
- char *__stx_realloc();
+ if (__isSmallInteger(numberOfBytes)) {
+ INT nBytes = __smallIntegerVal(numberOfBytes);
+ if (nBytes > 0) {
+ char *space;
+ char *prevSpace = (char *)__INST(address_);
- if (__isSmallInteger(numberOfBytes)) {
- nBytes = __smallIntegerVal(numberOfBytes);
- if (nBytes > 0) {
- 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.
]].
! !