ExternalBytes.st
author werner
Fri, 02 Apr 2004 18:28:16 +0200
changeset 8288 0bc2cce47912
parent 7855 27f64d1beaa1
child 8901 824a89d0b5c7
permissions -rw-r--r--
+setSize
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     1
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     2
 COPYRIGHT (c) 1993 by Claus Gittinger
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     3
	      All Rights Reserved
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     5
 This software is furnished under a license and may be used
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     6
 only in accordance with the terms of that license and with the
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     8
 be provided or otherwise made available to, or used by, any
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     9
 other person.  No title to or ownership of the software is
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    10
 hereby transferred.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    11
"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
5378
bc9f9e427fa7 oops - must synchronize accesses to AllocatedInstances
ps
parents: 5281
diff changeset
    13
"{ Package: 'stx:libbasic' }"
bc9f9e427fa7 oops - must synchronize accesses to AllocatedInstances
ps
parents: 5281
diff changeset
    14
3208
2d71538b9fd5 now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents: 3206
diff changeset
    15
UninterpretedBytes subclass:#ExternalBytes
3211
ef7a5411afa1 remove methods which are provided by my (new) superClass
Claus Gittinger <cg@exept.de>
parents: 3208
diff changeset
    16
	instanceVariableNames:'address* size'
5378
bc9f9e427fa7 oops - must synchronize accesses to AllocatedInstances
ps
parents: 5281
diff changeset
    17
	classVariableNames:'AllocatedInstances Lobby DebugMalloc TraceMalloc AccessLock'
3211
ef7a5411afa1 remove methods which are provided by my (new) superClass
Claus Gittinger <cg@exept.de>
parents: 3208
diff changeset
    18
	poolDictionaries:''
ef7a5411afa1 remove methods which are provided by my (new) superClass
Claus Gittinger <cg@exept.de>
parents: 3208
diff changeset
    19
	category:'System-Support'
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
!ExternalBytes primitiveFunctions!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
%{
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
struct mallocList {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    26
	char *chunk;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    27
	unsigned size;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    28
	struct mallocList *next;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
};
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
static struct mallocList *mallocList = (struct mallocList *)0;
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
static mallocCount = 0;
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    33
static void
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    34
removeFromMallocList(ptr)
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    35
    char *ptr;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    36
{
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    37
    struct mallocList *this, *prev, *next;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    38
    int found = 0;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    39
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    40
    if (@global(DebugMalloc) != true) return;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    41
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    42
    if (ptr) {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    43
	found = 0;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    44
	for (this=mallocList, prev=0; this; this=next) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    45
	    next = this->next;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    46
	    if (this->chunk == ptr) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    47
		if (prev) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    48
		    prev->next = next;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    49
		} else {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    50
		    mallocList = next;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    51
		}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    52
		free(this);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    53
		found++;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    54
		mallocCount--;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    55
	    } else {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    56
		prev = this;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    57
	    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    58
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    59
	if (! found) {
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    60
#ifdef alpha64
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    61
	    printf("EXTBYTES [warning]: **** free: alien %lx (allocated somewhere else ?))\n", ptr);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    62
#else
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    63
	    printf("EXTBYTES [warning]: **** free: alien %x (allocated somewhere else ?))\n", ptr);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    64
#endif
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    65
	}
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    66
    }
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    67
}
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    68
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    69
static void
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    70
addToMallocList(ptr, nBytes)
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    71
    char *ptr;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    72
{
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    73
    struct mallocList *e, *this;
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
    74
    char *malloc();
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    75
    int found;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    76
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    77
    if (@global(DebugMalloc) != true) return;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    78
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    79
    if (ptr) {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    80
	found = 0;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    81
	for (this=mallocList; this; this=this->next) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    82
	    if (this->chunk == ptr) {
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    83
#ifdef alpha64
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    84
		printf("EXTBYTES [warning]: **** %016lx already allocated (freed somewhere else ?)\n", ptr);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    85
#else
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    86
		printf("EXTBYTES [warning]: **** %08x already allocated (freed somewhere else ?)\n", ptr);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    87
#endif
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    88
		found++;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    89
	    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    90
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    91
	if (! found) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    92
	    e = (struct mallocList *) malloc(sizeof(struct mallocList));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    93
	    e->next = mallocList;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    94
	    e->chunk = ptr;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    95
	    e->size = nBytes;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    96
	    mallocList = e;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    97
	    mallocCount++;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    98
	}
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    99
    }
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   100
}
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   101
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
char *
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
__stx_malloc(nBytes) {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   104
	char *ptr;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   105
	char *malloc();
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   107
	ptr = malloc(nBytes);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   108
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   109
	if (@global(TraceMalloc) == true) {
2788
e4436755d153 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   110
#ifdef alpha64
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   111
	    printf("EXTBYTES [info]: allocated %d bytes at: %016lx\n", nBytes, ptr);
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
   112
#else
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   113
	    printf("EXTBYTES [info]: allocated %d bytes at: %08x\n", nBytes, ptr);
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
   114
#endif
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   115
	}
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   116
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   117
	addToMallocList(ptr, nBytes);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   118
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   119
	return ptr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
char *
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
__stx_calloc(n, size) {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   124
	char *ptr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   126
	ptr = __stx_malloc(n * size);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   127
	if (ptr != (char *)0) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   128
	    bzero(ptr, (n * size));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   129
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   130
	return ptr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
char *
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
__stx_realloc(ptr, nBytes)
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
    char *ptr;
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
{
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   137
	char *newPtr;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   138
	char *realloc();
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   140
	removeFromMallocList(ptr);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   141
	newPtr = realloc(ptr, nBytes);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   142
	addToMallocList(newPtr, nBytes);
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
   143
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   144
	if (@global(TraceMalloc) == true) {
2788
e4436755d153 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   145
#ifdef alpha64
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   146
	    printf("EXTBYTES [info]: realloc %d bytes for %016lx at: %016lx\n", nBytes, ptr, newPtr);
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
   147
#else
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   148
	    printf("EXTBYTES [info]: realloc %d bytes for %08x at: %08x\n", nBytes, ptr, newPtr);
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
   149
#endif
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   150
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   151
	return newPtr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
__stx_free(ptr)
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
    char *ptr;
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
{
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   157
	if (@global(TraceMalloc) == true) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   158
	    printf("EXTBYTES: free bytes at: %08x\n", ptr);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   159
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   160
	removeFromMallocList(ptr);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   162
	return free(ptr);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
3320
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   165
__stx_mallocStatistics() {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   166
	struct mallocList *this;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   167
	int amount = 0;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   168
	int n = 0;
3320
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   169
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   170
	for (this=mallocList; this; this=this->next) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   171
	    n++;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   172
	    amount += this->size;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   173
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   174
	printf("EXTBYTES [info]: allocated %d blocks with %d bytes overall\n", n, amount);
3320
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   175
}
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   176
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
!ExternalBytes class methodsFor:'documentation'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   182
copyright
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   183
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   184
 COPYRIGHT (c) 1993 by Claus Gittinger
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   185
	      All Rights Reserved
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   187
 This software is furnished under a license and may be used
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   188
 only in accordance with the terms of that license and with the
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   189
 inclusion of the above copyright notice.   This software may not
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   190
 be provided or otherwise made available to, or used by, any
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   191
 other person.  No title to or ownership of the software is
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   192
 hereby transferred.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   193
"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   196
documentation
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   197
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   198
    This class provides access to any memory in the system. Its main purpose
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   199
    is to provide a baseclass for objects referencing structured external data. 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   200
    Normally, instances are created by primitive code which wants to pass C-data
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   201
    to Smalltalk AND grants smalltalk access to individual bytes afterwards.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   202
    Primitives which do not want to grant this access should return instances of
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   203
    ExternalAddress. See more info there. Also, have a look at ExternalFunction
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   204
    which is another similar class, but specialized to represent callable C-functions.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   205
 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   206
    Since the memory address of an instance stays fixed (once allocated),
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   207
    it can also be used to share data with external C-parts 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   208
    (which are not prepared for objects to change their address).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   209
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   210
    Use with great care - access is not always checked for out-of-bounds 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   211
    or valid addresses.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   212
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   213
    Since the data is allocated outside the garbage collected smalltalk space,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   214
    its address stays fix. Thus, it can be passed to external C-functions without
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   215
    any danger. However, you have to take care for freeing the memory yourself.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   216
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   217
    To help in avoiding memory bugs, instances created with #new: are 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   218
    registered in a local classvar and deregistered when the underlying memory
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   219
    is explicitely freed. Since a life reference (from that classvar) exists,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   220
    the garbage collector will never find these to be reclaimable, and the
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   221
    underlying memory stays allocated (at a fix address) forever.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   222
    To release the memory, either #free it or #unprotect it.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   223
    The first will immediately release the memory, while the second will delay
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   224
    freeing until the next garbage collect occurs.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   225
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   226
    If you need memory which is automatically freed, create
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   227
    the instance via #unprotectedNew: right away; the underlying malloced-memory
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   228
    will be released as soon as no smalltalk reference to the ExtBytes object
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   229
    exists any more (however, you have to know for sure, that no C-references
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   230
    exist to this memory).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   231
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   232
    To release all memory call #releaseAllMemory which simply sets the 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   233
    AllocatedInstances class variable to nil (thus releasing those refs).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   234
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   235
    Example (automatic freeing as soon as ref to buffer is gone):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   236
	|buffer|
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   237
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   238
	buffer := ExternalBytes unprotectedNew:100.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   239
	...
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   240
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   242
    Example (manual freeing - never freed, if ref to buffer is gone):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   243
	|buffer|
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   244
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   245
	buffer := ExternalBytes new:100.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   246
	...
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   247
	buffer free
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   248
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   249
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   250
    Example (delayed automatic freeing as soon as ref to buffer is gone):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   251
	|buffer|
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   252
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   253
	buffer := ExternalBytes new:100.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   254
	...
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   255
	buffer unregister
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   256
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   257
    This class only supports unstructured external data 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   258
    - see the companion class ExternalStructure for more.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   259
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   260
    Notice: support for external data is still being developed -
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   261
	    a parser for C structure syntax and typedefs is on the way,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   262
	    making shared data with C programs much easier in the future.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   264
    Also notice, that this class may not be available or behave different
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   265
    in other smalltalk systems, making code using it very unportable.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   266
    It is provided for C interfacing only.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   267
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   268
    Finally note, that ST/X's memory system is much faster than malloc/free
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   269
    in the normal case - especially for short term temporary objects,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   270
    automatically reclaimed object memory is about 5-10 times faster than
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   271
    malloc/free.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   272
    Things may be different for huge byte-valued objects, which are to be
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   273
    reclaimed by the oldspace colletor. 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   274
    Anyway, for portability, we strongly warn from using this as a substitute 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   275
    for byteArrays; it is meant for shared data with external C-functions ONLY.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   276
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   277
    Debugging: 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   278
	since all manual memory systems are subject of obscure errors,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   279
	you may want to turn malloc-tracing on; this traces all allocations/frees
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   280
	done here. To do this, evaluate: 'ExternalBytes mallocTrace:true'.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   281
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   282
	In addition, you may turn on full debugging (with 'ExternalBytes mallocDebug:true');
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   283
	if turned on, all malloc/realloc requests are remembered and later free / realloc
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   284
	requests validated against this list (i.e. to detect freeing unallocated chunks).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   285
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   286
	To benefit from this in C-code, we recommend you use __stx_malloc() / __stx_free()
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   287
	instead of malloc() / free(). To do so, redefine them in a header file (or cc comand line)
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   288
	and recompile your external c-libraries with this.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   290
	I used this here to find memory leaks in the Xt libraries (there are still some in
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   291
	the HTML widget ...). If mallocDebug is on, #dumpMallocChunks will print out what is
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   292
	leftOver. This may help to find trouble spots in your C-code.
1286
4270a0b4917d documentation
Claus Gittinger <cg@exept.de>
parents: 1267
diff changeset
   293
4270a0b4917d documentation
Claus Gittinger <cg@exept.de>
parents: 1267
diff changeset
   294
    [author:]
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   295
	Claus Gittinger
1317
cc737e0fdf48 examples
Claus Gittinger <cg@exept.de>
parents: 1286
diff changeset
   296
cc737e0fdf48 examples
Claus Gittinger <cg@exept.de>
parents: 1286
diff changeset
   297
    [see also:]
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   298
	ExternalAddress ExternalFunction
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   299
	ByteArray
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   300
	( how to write primitive code :html: programming/primitive.html )
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
"
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   302
!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   303
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   304
examples 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   305
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   306
    These examples below are not directly executable;
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   307
    some require primitive code to be compiled, or are simply given as sceletton.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   308
    Notice, that in C, indexing is 0-based, while in Smalltalk, indices start
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   309
    at 1.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   310
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   311
    allocating memory in ST, passing it to C:
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   313
      in smalltalk:
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   314
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   315
        bytes := ExternalBytes new:100.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   316
        ...
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   318
      in C (assuming that the bytes-object has been passed):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   319
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   320
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   321
        char *bytePtr;
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   322
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   323
        bytePtr = (char *)(__externalBytesAddress(bytes));
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   324
        if (bytePtr) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   325
            ... do something with bytes at bytePtr ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   326
        }
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
      freeing (in ST):
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   330
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   331
        bytes free.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   332
        ...
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
    allocating memory in C, passing it to ST:
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
      in C:
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   337
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   338
        char *bytePtr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   340
        bytePtr = (char *)(malloc(100));
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   341
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   342
        RETURN (__MKEXTERNALBYTES(bytePtr));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
      in Smalltalk (extracting bytes, integers or strings):
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   346
        byteVal := bytes byteAt:1.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   347
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   348
        intVal := bytes doubleWordAt:1 MSB:true.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   349
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   350
        string := bytes stringAt:20.    
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   351
        ...
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   352
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   353
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   354
    |data bytes2|
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   355
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   356
    data := #[0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0] copy.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   357
    bytes2 := ExternalBytes new:30.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   358
    bytes2 replaceBytesFrom:1 to:20 with:data startingAt:1.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   359
    data replaceBytesFrom:2 to:20 with:bytes2 startingAt:1.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   360
    bytes2
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   361
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   362
    |data bytes1 bytes2|
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   363
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   364
    data := #[0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0].
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   365
    bytes1 := ExternalBytes new:30.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   366
    bytes2 := ExternalBytes new:30.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   367
    bytes1 replaceBytesFrom:1 to:20 with:data startingAt:1.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   368
    bytes2 atAllPut:99.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   369
    bytes2 replaceBytesFrom:2 to:21 with:bytes1 startingAt:1.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   370
    bytes2 asByteArray  
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   371
7192
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   372
    |data1 bytes1 data2|
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   373
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   374
    data1 := #[0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0].
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   375
    bytes1 := ExternalBytes new:30.
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   376
    bytes1 replaceBytesFrom:1 to:20 with:data1 startingAt:1.
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   377
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   378
    data2 := ByteArray new:30 withAll:99.
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   379
    data2 replaceBytesFrom:2 to:21 with:bytes1 startingAt:1.
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   380
    data2    
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   381
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
"
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   385
!ExternalBytes class methodsFor:'initialization'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   386
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   387
initialize
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   388
    Lobby isNil ifTrue:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   389
	Lobby := Registry new.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   390
	AccessLock := Semaphore forMutualExclusion.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   391
    ]
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   392
! !
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   393
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
!ExternalBytes class methodsFor:'instance creation'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   396
address:aNumber
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   397
    "return a new ExternalBytes object to access bytes starting at aNumber.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
     The memory at aNumber has been allocated elsewhere. The size is not known,
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
     therefore byte accesses will NOT be checked for valid index.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
     Use this, if you get a pointer from some external source (such as a
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
     C-callBack function) and you have to extract bytes from that.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
     DANGER ALERT: this method allows very bad things to be done to the
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   404
		   system - use with GREAT care (better: do not use it)"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
    ^ self basicNew setAddress:aNumber size:nil
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   409
address:aNumber size:size
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   410
    "return a new ExternalBytes object to access bytes starting at aNumber.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
     The memory at aNumber has been allocated elsewhere. The size is known,
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
     which allows byte accesses to be checked for valid index.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
     Use this, if you get a pointer to a structure from some external source 
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
     (such as a C-callBack function) and you have to extract things from that.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
     DANGER ALERT: this method allows very bad things to be done to the
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   417
		   system - use with GREAT care (better: do not use it)"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
    ^ self basicNew setAddress:aNumber size:size 
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   422
new:numberOfBytes
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   423
    "allocate some memory usable for data;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   424
     the memory is not controlled by the garbage collector.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   425
     Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   426
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   427
     Use this, if you have to pass a block of bytes to some 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   428
     external destination (such as a C function) which does not copy the
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   429
     data, but instead keeps a reference to it. For example, many functions
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   430
     which expect strings simply keep a ref to the passed string - for those,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   431
     an ST/X string-pointer is not the right thing to pass, since ST/X objects
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
     may change their address.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
     DANGER ALERT: the memory is NOT automatically freed until it is either
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   435
		   MANUALLY freed (see #free) or the returned externalBytes object
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   436
		   is unprotected or the classes releaseAllMemory method is called."
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
    |newInst|
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
    newInst := self unprotectedNew:numberOfBytes.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
    newInst protectFromGC.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
    ^ newInst
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
     |bytes|
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
     bytes := ExternalBytes new:100.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
     bytes wordAt:1 put:1.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
     bytes doubleWordAt:3 put:16r12345678.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
     bytes inspect
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   452
!
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   454
unprotectedNew:numberOfBytes
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   455
    "allocate some memory usable for data; 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   456
     the memory is under the control of the garbage collector.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   457
     Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   459
     DANGER ALERT: the memory block as allocated will be automatically freed
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   460
		   as soon as the reference to the returned externalBytes object
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   461
		   is gone (by the next garbage collect).
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   462
		   If the memory has been passed to a C-function which 
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   463
		   remembers this pointer, bad things may happen ...."
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
    |newInst|
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   467
    newInst := self basicNew allocateBytes:numberOfBytes.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
    Lobby register:newInst.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
    ^ newInst
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   470
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   471
    "
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   472
     |bytes|
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   473
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   474
     bytes := ExternalBytes unprotectedNew:100.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   475
     bytes wordAt:1 put:1.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   476
     bytes doubleWordAt:3 put:16r12345678.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   477
     bytes inspect
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   478
    "
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   481
!ExternalBytes class methodsFor:'instance release'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   482
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   483
releaseAllMemory
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   484
    AllocatedInstances := nil
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   485
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   486
     ... the next GC will get 'em
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   487
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   488
! !
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   489
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
!ExternalBytes class methodsFor:'malloc debug'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   492
dumpMallocChunks
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   493
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   494
    struct mallocList *entry;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   496
    for (entry = mallocList; entry; entry=entry->next) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   497
#ifdef alpha64
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   498
	printf("  %016lx (%d)\n", entry->chunk, entry->size);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   499
#else
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   500
	printf("  %08x (%d)\n", entry->chunk, entry->size);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   501
#endif
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   502
    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   503
%}
3321
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   504
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   505
     self dumpMallocChunks
3321
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   506
    "
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   509
freeAllMallocChunks
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   510
    "free all stx_malloc'd memory. Be careful, this does no validation at all;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   511
     It simply walks through all chunks and frees them unconditionally.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   512
     This may be helpful during debugging memory-leaks, to release memory which
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   513
     was not correctly freed by C-code. Howeve, only memory which was allocated
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   514
     by __stx_malloc() is freed here - so you better compile your primitive code with
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   515
     malloc redefined to stx_malloc.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
     Also, mallocDebug has to be on to do this."
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
    "first free my own memory ..."
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
    self releaseAllMemory.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
    ObjectMemory garbageCollect.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
%{
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
    struct mallocList *entry;
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
    while ((entry = mallocList) != (struct mallocList *)0) {
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   526
	if (@global(TraceMalloc) == true ) {
2788
e4436755d153 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 2779
diff changeset
   527
#ifdef alpha64
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   528
	    printf("EXTBYTES [info]: **** forced free of %016lx (%d)\n", entry->chunk, entry->size);
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
   529
#else
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   530
	    printf("EXTBYTES [info]: **** forced free of %08x (%d)\n", entry->chunk, entry->size);
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
   531
#endif
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   532
	}
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   533
	__stx_free(entry->chunk);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   534
    }
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   535
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   536
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   538
mallocDebug:aBoolean
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   539
    DebugMalloc := aBoolean
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   540
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   541
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
     ExternalBytes mallocDebug:true
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
     ExternalBytes mallocDebug:false 
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   544
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   545
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   546
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   547
mallocStatistics
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   548
%{
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   549
    __stx_mallocStatistics();
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   550
%}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   551
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   552
     self mallocStatistics
3486
bbd47c3011e5 comment
Claus Gittinger <cg@exept.de>
parents: 3321
diff changeset
   553
    "
bbd47c3011e5 comment
Claus Gittinger <cg@exept.de>
parents: 3321
diff changeset
   554
!
bbd47c3011e5 comment
Claus Gittinger <cg@exept.de>
parents: 3321
diff changeset
   555
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   556
mallocTrace:aBoolean
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   557
    TraceMalloc := aBoolean
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   558
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   559
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   560
     ExternalBytes mallocTrace:true
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   561
     ExternalBytes mallocTrace:false
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   562
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   563
!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   564
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   565
numberOfAllocatedChunks
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   566
%{  /* NOCONTEXT */
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
    RETURN ( __MKSMALLINT(mallocCount) );
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
%}
3321
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   569
    "
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   570
     self numberOfAllocatedChunks
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   571
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   572
! !
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   574
!ExternalBytes class methodsFor:'queries'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   575
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   576
isBuiltInClass
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   577
    "return true if this class is known by the run-time-system.
1267
e285a3a94d9e commentary
Claus Gittinger <cg@exept.de>
parents: 1242
diff changeset
   578
     Here, true is returned."
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
3553
0c9663682120 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3486
diff changeset
   580
    ^ self == ExternalBytes
1267
e285a3a94d9e commentary
Claus Gittinger <cg@exept.de>
parents: 1242
diff changeset
   581
3553
0c9663682120 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3486
diff changeset
   582
    "Modified: / 11.6.1998 / 17:12:51 / cg"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   585
sizeofDouble
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   586
    "return the number of bytes used by the machines native doubles"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
%{  /* NOCONTEXT */
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
    RETURN (__MKSMALLINT( sizeof(double)));
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
     ExternalBytes sizeofDouble   
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   596
sizeofFloat
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   597
    "return the number of bytes used by the machines native floats"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
%{  /* NOCONTEXT */
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
    RETURN (__MKSMALLINT( sizeof(float)));
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
     ExternalBytes sizeofFloat
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   607
sizeofInt
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   608
    "return the number of bytes used by the machines native integer"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
%{  /* NOCONTEXT */
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
    RETURN (__MKSMALLINT( sizeof(int)));
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
     ExternalBytes sizeofInt
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   616
!
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   618
sizeofLong
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   619
    "return the number of bytes used by the machines native longs"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   621
%{  /* NOCONTEXT */
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   622
    RETURN (__MKSMALLINT( sizeof(long)));
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   623
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   624
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   625
     ExternalBytes sizeofLong
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   627
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   628
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   629
sizeofPointer
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   630
    "return the number of bytes used by the machines native pointer"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   631
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   632
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   633
    RETURN (__MKSMALLINT( sizeof(char *)));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   634
%}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   635
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   636
     ExternalBytes sizeofPointer
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   637
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   638
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   639
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   640
sizeofShort
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   641
    "return the number of bytes used by the machines native short"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   642
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   643
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   644
    RETURN (__MKSMALLINT( sizeof(short)));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   645
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   646
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   647
     ExternalBytes sizeofShort
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   648
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   649
! !
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   650
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   651
!ExternalBytes methodsFor:'accessing'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   652
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   653
address
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   654
    "return the start address as an integer"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   655
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   656
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   657
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   658
    int addr;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   659
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   660
    if (__INST(address_) != nil) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   661
	addr = (INT)__INST(address_);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   662
	if (addr <= _MAX_INT) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   663
	    RETURN ( __MKSMALLINT(addr) );
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   664
	}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   665
	RETURN ( __MKUINT(addr));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   666
    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   667
%}.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   668
    ^ nil
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   669
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   670
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   671
basicAt:index
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   672
    "return the byte at index, anInteger;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   673
     Indices are 1-based, therefore
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   674
     this is the byte at (address + index - 1)"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
%{  /* NOCONTEXT */
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   677
1822
e7b977512062 type casts
Claus Gittinger <cg@exept.de>
parents: 1317
diff changeset
   678
    unsigned char *cp = (unsigned char *)(__INST(address_));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
    int idx;
1997
4c5a80c2d570 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1822
diff changeset
   680
    OBJ sz;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
    if (cp && __isSmallInteger(index)) {
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   683
	idx = __intVal(index);
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   684
	if (idx > 0) {
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   685
	    if (((sz = __INST(size)) == nil)
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   686
	     || (__intVal(sz) >= idx)) {
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   687
		cp = cp + idx - 1;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   688
		RETURN ( __MKSMALLINT(*cp) );
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   689
	    }
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   690
	}
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
    }
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
%}.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
    (size notNil and:[self address notNil]) ifTrue:[
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   694
	^ self subscriptBoundsError:index
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
    ].
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
     invalid index or unallocated
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
    self primitiveFailed
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   702
basicAt:index put:value
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   703
    "set the byte at index, anInteger to value which must be 0..255.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   704
     Returns value (sigh).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   705
     Indices are 1-based, therefore
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   706
     this is the byte at (address + index - 1)"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   707
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   708
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   709
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   710
    unsigned char *cp = (unsigned char *)(__INST(address_));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   711
    int val;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   712
    int idx;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   713
    OBJ sz;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   714
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   715
    if (__isSmallInteger(value)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   716
	val = __smallIntegerVal(value);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   717
    } else if (__isCharacter(value)) {  
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   718
	val = __smallIntegerVal(_characterVal(value));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   719
    } else
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   720
	goto badArg;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   721
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   722
    if (cp && __isSmallInteger(index)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   723
	idx = __intVal(index);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   724
	if (idx > 0) {
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   725
	    if (((sz = __INST(size)) == nil)
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   726
	     || (__intVal(sz) >= idx)) {
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   727
		if ((val & ~0xFF) == 0) /* i.e. (val >= 0) && (val <= 255) */  {
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   728
		    cp[idx-1] = val;
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   729
		    RETURN ( value );
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   730
		}
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   731
	    }
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   732
	}
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   733
    }
6302
32ede83e77e5 allow for character arg in #at:put:
Claus Gittinger <cg@exept.de>
parents: 6242
diff changeset
   734
32ede83e77e5 allow for character arg in #at:put:
Claus Gittinger <cg@exept.de>
parents: 6242
diff changeset
   735
badArg: ;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   736
%}.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   737
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   738
    (size notNil and:[self address notNil]) ifTrue:[
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   739
	(index between:1 and:size) ifTrue:[
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   740
	    ^ ElementOutOfBoundsSignal raise
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   741
	].
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   742
	^ self subscriptBoundsError:index
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   743
    ].
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   744
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   745
     invalid index, invalid value or unallocated
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   746
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   747
    self primitiveFailed
1220
99990bbb561f commentary
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   748
99990bbb561f commentary
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   749
    "Modified: 19.4.1996 / 11:15:05 / cg"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   750
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   751
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   752
byteAt:index
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   753
    "return the unsigned byte at index, anInteger.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   754
     Indices are 1-based, therefore
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   755
     this is the byte at (address + index - 1)"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   757
    ^ self basicAt:index
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   759
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   760
byteAt:index put:aByteValuedInteger
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   761
    "set the byte at index.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   762
     Indices are 1-based, therefore
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   763
     this is the byte at (address + index - 1)"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   764
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   765
    ^ self basicAt:index put:aByteValuedInteger
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   766
!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   767
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   768
instVarAt:index
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   769
    "redefined to suppress direct access to my address, which is a non-object"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   770
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   771
    index == 1 ifTrue:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   772
	^ self address
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
   773
    ].
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   774
    ^ super instVarAt:index
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   775
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   777
!ExternalBytes methodsFor:'converting'!
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   778
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   779
asExternalAddress
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   780
    "return the start address as an external address"
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   781
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   782
%{  /* NOCONTEXT */
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   783
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   784
    RETURN(__MKEXTERNALADDRESS(__INST(address_)));
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   785
%}.
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   786
!
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   787
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   788
asExternalBytes
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   789
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   790
    ^ self
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   791
! !
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   792
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   793
!ExternalBytes methodsFor:'filling & replacing'!
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   794
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   795
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   796
    "replace elements from another collection, which must be a ByteArray-
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   797
     like collection.
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   798
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   799
     Notice: This operation modifies the receiver, NOT a copy;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   800
     therefore the change may affect all others referencing the receiver."
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   801
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   802
%{  /* NOCONTEXT */
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   803
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   804
    int nIndex, repNIndex;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   805
    int startIndex, stopIndex;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   806
    REGISTER unsigned char *src;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   807
    REGISTER int repStartIndex;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   808
    int repStopIndex, count;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   809
    REGISTER unsigned char *dst;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   810
    OBJ cls;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   811
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   812
    if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection))
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   813
     && __bothSmallInteger(start, stop)
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   814
     && __isSmallInteger(repStart)) {
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   815
        startIndex = __intVal(start) - 1;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   816
        if (startIndex >= 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   817
            dst = (unsigned char *)__INST(address_) + startIndex;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   818
            nIndex = __smallIntegerVal(__INST(size));
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   819
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   820
            stopIndex = __intVal(stop) - 1;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   821
            count = stopIndex - startIndex + 1;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   822
            if (count == 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   823
                RETURN ( self );
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   824
            }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   825
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   826
            if ((count > 0) && (stopIndex < nIndex)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   827
                repStartIndex = __intVal(repStart) - 1;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   828
                if (repStartIndex >= 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   829
                    if (__isExternalBytesLike(aCollection)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   830
                        OBJ sz;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   831
7386
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
   832
                        src = __externalBytesVal(aCollection);
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   833
                        if (src == 0) goto fallBack;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   834
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   835
                        sz = __externalBytesSize(aCollection);
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   836
                        if (__isSmallInteger(sz)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   837
                            repNIndex = __smallIntegerVal(sz);
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   838
                        } else {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   839
                            repNIndex = -1; /* unknown */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   840
                        }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   841
                        src = src + repStartIndex;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   842
                    } else {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   843
                        repNIndex = __qSize(aCollection) - OHDR_SIZE;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   844
                        src = __byteArrayVal(aCollection) + repStartIndex;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   845
                        if ((cls = __qClass(aCollection)) != @global(ByteArray)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   846
                            int nInst;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   847
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   848
                            nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   849
                            src += nInst;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   850
                            repNIndex -= nInst;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   851
                        }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   852
                    }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   853
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   854
                    repStopIndex = repStartIndex + (stopIndex - startIndex);
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   855
                    if (repStopIndex < repNIndex) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   856
                        if (aCollection == self) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   857
                            /* take care of overlapping copy */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   858
                            if (src < dst) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   859
                                /* must do a reverse copy */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   860
                                src += count;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   861
                                dst += count;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   862
                                while (count-- > 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   863
                                    *--dst = *--src;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   864
                                }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   865
                                RETURN ( self );
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   866
                            }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   867
                        }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   868
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   869
#ifdef memcpy4
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   870
                        if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   871
                            int nW;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   872
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   873
                            /* copy unaligned part */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   874
                            while (count && ((unsigned INT)src & 3)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   875
                                *dst++ = *src++;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   876
                                count--;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   877
                            }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   878
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   879
                            if (count > 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   880
                                /* copy aligned part */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   881
                                nW = count >> 2;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   882
                                memcpy4(dst, src, nW);
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   883
                                if ((count = count & 3) != 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   884
                                    /* copy any remaining part */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   885
                                    src += (nW<<2);
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   886
                                    dst += (nW<<2);
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   887
                                    while (count--) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   888
                                        *dst++ = *src++;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   889
                                    }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   890
                                }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   891
                            }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   892
                            RETURN ( self );
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   893
                        }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   894
#else
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   895
# ifdef alpha64
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   896
                        if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   897
                            /* copy unaligned part */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   898
                            while (count && ((unsigned INT)src & 7)) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   899
                                *dst++ = *src++;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   900
                                count--;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   901
                            }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   902
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   903
                            /* copy aligned part */
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   904
                            while (count >= 8) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   905
                                ((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0];
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   906
                                dst += 8;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   907
                                src += 8;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   908
                                count -= 8;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   909
                            }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   910
                            while (count--) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   911
                                *dst++ = *src++;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   912
                            }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   913
                            RETURN ( self );
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   914
                        }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   915
# endif /* alpha64 */
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   916
#endif /* memcpy4 */
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   917
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   918
#ifdef FAST_MEMCPY
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   919
                        memcpy(dst, src, count);
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   920
#else
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   921
# ifdef UNROLL_LOOPS
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   922
                        while (count >= 8) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   923
                            dst[0] = src[0]; dst[1] = src[1];
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   924
                            dst[2] = src[2]; dst[3] = src[3];
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   925
                            dst[4] = src[4]; dst[5] = src[5];
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   926
                            dst[6] = src[6]; dst[7] = src[7];
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   927
                            dst += 8; src += 8;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   928
                            count -= 8;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   929
                        }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   930
# endif /* UNROLL_LOOPS */
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   931
                        while (count-- > 0) {
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   932
                            *dst++ = *src++;
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   933
                        }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   934
#endif
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   935
                        RETURN ( self );
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   936
                    }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   937
                }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   938
            }
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   939
        }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   940
    }
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   941
fallBack: ;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   942
%}.
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   943
    "
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   944
     fall back for the error report if any index is invalid
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   945
    "
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   946
    ^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   947
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   948
    "
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   949
     (ExternalBytes unprotectedNew:16)
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   950
            replaceFrom:1 to:8 
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   951
            with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   952
            startingAt:1
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   953
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   954
     (ExternalBytes unprotectedNew:16)
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   955
            replaceFrom:3 to:10 
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   956
            with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   957
            startingAt:4
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   958
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   959
     (ExternalBytes unprotectedNew:16)
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   960
            replaceFrom:3 to:4 
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   961
            with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   962
            startingAt:1
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   963
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   964
     (ExternalBytes unprotectedNew:16)
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   965
            replaceFrom:0 to:9 
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   966
            with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   967
            startingAt:1
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   968
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   969
     (ExternalBytes unprotectedNew:16)
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   970
            replaceFrom:1 to:10 
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   971
            with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   972
            startingAt:0
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   973
    "
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   974
! !
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
   975
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   976
!ExternalBytes methodsFor:'finalization'!
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   977
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
   978
executor
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
   979
    "redefined to return a lightweight copy 
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
   980
     - all we need is the memory handle and the size."
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   981
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   982
%{ /* NOCONTEXT */
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
   983
7386
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
   984
    OBJ theCopy, cls;
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
   985
7855
27f64d1beaa1 Have to protect self, otherwise bad things happen in the VM.
Stefan Vogel <sv@exept.de>
parents: 7386
diff changeset
   986
    __PROTECT__(self);
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
   987
    theCopy = __MKEXTERNALBYTES_N(__INST(address_), __smallIntegerVal(__INST(size)));
7855
27f64d1beaa1 Have to protect self, otherwise bad things happen in the VM.
Stefan Vogel <sv@exept.de>
parents: 7386
diff changeset
   988
    __UNPROTECT__(self);
7386
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
   989
    __InstPtr(theCopy)->o_class = cls = __InstPtr(self)->o_class;
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
   990
    __STORE(theCopy, cls);
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
   991
    RETURN (theCopy);
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   992
%}
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   993
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   994
    "
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   995
      (ExternalBytes unprotectedNew:10) executor
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   996
    "
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
   997
!
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
   998
6462
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
   999
finalizationLobby
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1000
    "answer the registry used for finalization.
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1001
     ExternalBytes have their own Registry"
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1002
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1003
    ^ Lobby
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1004
!
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1005
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
  1006
finalize
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
  1007
    "some ExternalBytes object was finalized;
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
  1008
     free the associated heap memory with it"
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
  1009
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1010
%{  /* NOCONTEXT */
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1011
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1012
    char *mem = (char *)(__INST(address_));
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1013
    if (mem && (OBJ)mem != nil) {
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1014
	__stx_free(mem);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1015
    }
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
  1016
    __INST(address_) = __INST(size) = nil;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1017
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1018
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1019
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1020
!ExternalBytes methodsFor:'freeing'!
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1021
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1022
free
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1023
    "free a previously allocated piece of memory - be very careful, there
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1024
     are no checks done here. All dangers you usually have with malloc/free
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1025
     are present here ..."
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1026
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1027
    "at least, we check for double freeing the same chunk"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1028
    self address isNil ifTrue:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1029
	self error:'freeing memory twice'.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1030
	^ self
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1031
    ].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1032
    Lobby unregister:self.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1033
    self finalize.  "/ does what we need here ..
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1034
    self unprotectFromGC
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1035
!
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1036
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1037
register
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1038
    "register the receiver to be automatically finalized by the GC"
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1039
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1040
    Lobby register:self.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1041
! !
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1042
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1043
!ExternalBytes methodsFor:'pointer arithmethic'!
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1044
6502
87f587d1542e rename #from:to to #referenceToBytesFrom:to
Stefan Vogel <sv@exept.de>
parents: 6491
diff changeset
  1045
referenceToBytesFrom:start to:stop
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1046
   "answer a new ExternalBytes referencing a range within the receiver.
6502
87f587d1542e rename #from:to to #referenceToBytesFrom:to
Stefan Vogel <sv@exept.de>
parents: 6491
diff changeset
  1047
    BE CAREFUL: after the receiver has been freed, the new ExternalBytes
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1048
    contents is undefined"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1049
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1050
%{ /* NOCONTEXT */ 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1051
6471
186b2bb08a0a compilable under bcc55
Claus Gittinger <cg@exept.de>
parents: 6462
diff changeset
  1052
    char *addr;
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1053
    int size;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1054
    int __start, __stop;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1055
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1056
    if (__bothSmallInteger(start, stop) && __INST(address_) != nil) {
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1057
	__start = __smallIntegerVal(start);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1058
	__stop = __smallIntegerVal(stop);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1059
	if (__start > 0 && __start <= __stop && __stop <= __smallIntegerVal(__INST(size))) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1060
	    addr = (char *)(__INST(address_)) + (__start - 1);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1061
	    size = __stop - __start + 1;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1062
	    RETURN( __MKEXTERNALBYTES_N(addr, size) );
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1063
	}
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1064
    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1065
%}.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1066
   ^ self primitiveFailed
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1067
! !
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1068
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1069
!ExternalBytes methodsFor:'printing & storing'!
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1070
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1071
displayString
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1072
    "return a printed representation of the receiver for displaying"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1073
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1074
    |addr addrString|
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1075
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1076
    (addr := self address) isNil ifTrue:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1077
	addrString := '[free]'
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1078
    ] ifFalse:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1079
	addrString := '[', size printString, ' at:' , (self address printStringRadix:16), ']'
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1080
    ].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1081
    ^ self class name , addrString
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1082
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1083
    "Modified: / 24.2.2000 / 19:02:19 / cg"
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1084
! !
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1085
7257
b9f0fb923c72 method category rename
Claus Gittinger <cg@exept.de>
parents: 7192
diff changeset
  1086
!ExternalBytes methodsFor:'private-accessing'!
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1087
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1088
setAddress:aNumber size:sz 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1089
    "set the start address and size"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1090
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1091
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1092
    if (__INST(address_) == nil) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1093
	if (aNumber == nil) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1094
	    __INST(address_) = nil;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1095
	} else {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1096
	    if (__isSmallInteger(aNumber)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1097
		__INST(address_) = (OBJ) __intVal(aNumber);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1098
	    } else if(__isInteger(aNumber)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1099
		__INST(address_) = (OBJ) __unsignedLongIntVal(aNumber);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1100
	    } else if(__isExternalAddress(aNumber)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1101
		__INST(address_) = __externalAddressVal(aNumber);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1102
	    }
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
  1103
	}
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1104
	__INST(size) = sz;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1105
	RETURN (self);
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1106
    }
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1107
%}.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1108
    ^ self error:'cannot change address'
8288
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1109
!
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1110
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1111
setSize:sz 
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1112
    "set the size - warning: dangerous if wrong"
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1113
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1114
    size := sz
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1115
! !
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1116
7257
b9f0fb923c72 method category rename
Claus Gittinger <cg@exept.de>
parents: 7192
diff changeset
  1117
!ExternalBytes methodsFor:'private-allocation'!
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1118
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1119
allocateBytes:numberOfBytes
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1120
    "allocate (malloc) numberOfBytes.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1121
     Fail if already allocated.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1122
     Raise MallocFailure if malloc fails to allocate enough memory"
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1123
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1124
    |mallocFailure|
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1125
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1126
%{
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1127
    char *space;
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1128
    unsigned int nBytes;
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1129
    char *__stx_malloc();
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1130
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1131
    /*
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1132
     * Fail if already allocated 
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1133
     */
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1134
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1135
    if (__INST(address_) == nil && __isSmallInteger(numberOfBytes)) {
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1136
	nBytes = __smallIntegerVal(numberOfBytes);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1137
	if (nBytes > 0) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1138
	    space = __stx_malloc(nBytes);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1139
	    if (space) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1140
		__INST(address_) = (OBJ)space;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1141
		__INST(size) = numberOfBytes;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1142
		RETURN(self);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1143
	    } else {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1144
		mallocFailure = true;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1145
	    }
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1146
	}
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1147
    }
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1148
%}.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1149
    mallocFailure == true ifTrue:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1150
	^ MallocFailure raiseRequestWith:numberOfBytes.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1151
    ] ifFalse:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1152
	self primitiveFailed.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1153
    ].
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1154
! !
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1155
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1156
!ExternalBytes methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1157
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1158
basicSize
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1159
    "we do not know how many bytes are valid"
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1160
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1161
    size isNil ifTrue:[^ 0].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1162
    ^ size
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1163
!
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1164
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1165
species
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1166
    "when copying, or concatenating, return instances of this class"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1167
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1168
    ^ ByteArray
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1169
! !
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1170
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1171
!ExternalBytes methodsFor:'registration'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1172
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1173
protectFromGC 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1174
    "enter a reference to the receiver into the AllocatedInstances
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1175
     class variable - this prevents it from ever being finalized by
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1176
     the garbage collector, thus protecting the underlying memory."
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1177
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1178
    AccessLock critical:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1179
	AllocatedInstances isNil ifTrue:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1180
	    AllocatedInstances := IdentitySet new
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1181
	].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1182
	AllocatedInstances add:self
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1183
    ]
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1184
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1185
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1186
unprotectFromGC 
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1187
    "remove the receiver from the AllocatedInstances
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1188
     class variable - if there is no other reference to the receiver,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1189
     the next garbage collect will finalize the receiver and the underlying
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1190
     memory be freed."
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1191
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1192
    AllocatedInstances notNil ifTrue:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1193
	AccessLock critical:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1194
	    AllocatedInstances remove:self ifAbsent:nil
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1195
	]
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1196
    ]
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1197
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1198
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1199
!ExternalBytes methodsFor:'resizing'!
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1200
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1201
grow:numberOfBytes
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1202
    "reallocate (realloc) numberOfBytes.
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1203
     Raise MallocFailure if realloc fails to allocate enough memory"
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1204
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1205
    |mallocStatus|
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1206
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1207
%{
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1208
    char *space, *prevSpace;
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1209
    unsigned int nBytes;
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1210
    char *__stx_realloc();
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1211
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1212
    if (__isSmallInteger(numberOfBytes)) {
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1213
	nBytes = __smallIntegerVal(numberOfBytes);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1214
	if (nBytes > 0) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1215
	    prevSpace = (char *)__INST(address_);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1216
	    if (prevSpace == (char *)nil)
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1217
		prevSpace = 0;  /* allocate from scratch */
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1218
	    space = __stx_realloc(prevSpace, nBytes);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1219
	    if (space) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1220
		__INST(address_) = (OBJ)space;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1221
		__INST(size) = numberOfBytes;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1222
		if (space == prevSpace) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1223
		    /* same address, no re-registration */
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1224
		    RETURN(self);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1225
		}
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1226
		mallocStatus = true;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1227
	    } else {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1228
		mallocStatus = false;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1229
	    }
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1230
	}
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1231
    }
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1232
%}.
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1233
    mallocStatus == true ifTrue:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1234
	Lobby registerChange:self.
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1235
    ] ifFalse:[mallocStatus == false ifTrue:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1236
	^ MallocFailure raiseRequestWith:numberOfBytes.
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1237
    ] ifFalse:[
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1238
	self primitiveFailed.
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1239
    ]].
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1240
! !
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1241
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1242
!ExternalBytes class methodsFor:'documentation'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1243
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1244
version
8288
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1245
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalBytes.st,v 1.56 2004-04-02 16:28:16 werner Exp $'
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1246
! !
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1247
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1248
ExternalBytes initialize!