ExternalBytes.st
author Claus Gittinger <cg@exept.de>
Tue, 09 Jul 2019 20:55:17 +0200
changeset 24417 03b083548da2
parent 24056 a7b360193da5
child 24784 07fbb48bf6b5
permissions -rw-r--r--
#REFACTORING by exept class: Smalltalk class changed: #recursiveInstallAutoloadedClassesFrom:rememberIn:maxLevels:noAutoload:packageTop:showSplashInLevels: Transcript showCR:(... bindWith:...) -> Transcript showCR:... with:...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
     1
"{ Encoding: utf8 }"
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
     2
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     3
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     4
 COPYRIGHT (c) 1993 by Claus Gittinger
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     5
	      All Rights Reserved
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     7
 This software is furnished under a license and may be used
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
     8
 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
     9
 inclusion of the above copyright notice.   This software may not
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    10
 be provided or otherwise made available to, or used by, any
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    11
 other person.  No title to or ownership of the software is
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    12
 hereby transferred.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
    13
"
5378
bc9f9e427fa7 oops - must synchronize accesses to AllocatedInstances
ps
parents: 5281
diff changeset
    14
"{ Package: 'stx:libbasic' }"
bc9f9e427fa7 oops - must synchronize accesses to AllocatedInstances
ps
parents: 5281
diff changeset
    15
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
    16
"{ NameSpace: Smalltalk }"
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
    17
3208
2d71538b9fd5 now subclass of UIBytes - which contains common protocol
Claus Gittinger <cg@exept.de>
parents: 3206
diff changeset
    18
UninterpretedBytes subclass:#ExternalBytes
3211
ef7a5411afa1 remove methods which are provided by my (new) superClass
Claus Gittinger <cg@exept.de>
parents: 3208
diff changeset
    19
	instanceVariableNames:'address* size'
21406
c8b4c0f96850 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21378
diff changeset
    20
	classVariableNames:'AllocatedInstances DebugMalloc Lobby TraceMalloc'
3211
ef7a5411afa1 remove methods which are provided by my (new) superClass
Claus Gittinger <cg@exept.de>
parents: 3208
diff changeset
    21
	poolDictionaries:''
ef7a5411afa1 remove methods which are provided by my (new) superClass
Claus Gittinger <cg@exept.de>
parents: 3208
diff changeset
    22
	category:'System-Support'
848
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
14749
64fd0e91d2d7 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14632
diff changeset
    25
!ExternalBytes primitiveDefinitions!
64fd0e91d2d7 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14632
diff changeset
    26
%{
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    27
    #include <stdlib.h>
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    28
    #include <stdio.h>
19392
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    29
// cg: not needed
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    30
#if 0
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    31
# ifdef __osx__
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    32
#  include <sys/malloc.h>
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    33
# else
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    34
#  include <malloc.h>
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    35
# endif
4dca8e4bec84 compilable on mac
Claus Gittinger <cg@exept.de>
parents: 19376
diff changeset
    36
#endif
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    37
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    38
    extern char *__stx_malloc(size_t);
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    39
    extern char *__stx_calloc(size_t, size_t);
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    40
    extern char *__stx_realloc(char *, size_t);
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    41
    extern void __stx_free(char *);
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    42
    extern void __stx_mallocStatistics(void);
14749
64fd0e91d2d7 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14632
diff changeset
    43
%}
64fd0e91d2d7 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14632
diff changeset
    44
! !
64fd0e91d2d7 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14632
diff changeset
    45
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
!ExternalBytes primitiveFunctions!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
%{
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
struct mallocList {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    50
	char *chunk;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    51
	size_t size;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    52
	struct mallocList *next;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
};
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
static struct mallocList *mallocList = (struct mallocList *)0;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
    55
static INT mallocCount = 0;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    57
static void
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    58
removeFromMallocList(char *ptr)
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    59
{
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    60
    struct mallocList *this, *prev, *next;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    61
    int found = 0;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    62
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    63
    if (@global(DebugMalloc) != true) return;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    64
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    65
    if (ptr) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    66
	found = 0;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    67
	for (this=mallocList, prev=0; this; this=next) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    68
	    next = this->next;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    69
	    if (this->chunk == ptr) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    70
		if (prev) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    71
		    prev->next = next;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    72
		} else {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    73
		    mallocList = next;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    74
		}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    75
		free(this);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    76
		found++;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    77
		mallocCount--;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    78
	    } else {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    79
		prev = this;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    80
	    }
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    81
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    82
	if (! found) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    83
	    console_printf("ExternalBytes [warning]: **** free: alien %"_lx_" (allocated somewhere else ?))\n", (INT)ptr);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    84
	}
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    85
    }
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    86
}
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    87
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    88
static void
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
    89
addToMallocList(char *ptr, size_t nBytes)
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    90
{
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    91
    struct mallocList *e, *this;
9895
93328f425669 Fix declaration of malloc()
Stefan Vogel <sv@exept.de>
parents: 9389
diff changeset
    92
    void *malloc();
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    93
    int found;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    94
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    95
    if (@global(DebugMalloc) != true) return;
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    96
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
    97
    if (ptr) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    98
	found = 0;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
    99
	for (this=mallocList; this; this=this->next) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   100
	    if (this->chunk == ptr) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   101
		console_printf("ExternalBytes [warning]: **** %016"_lx_" already allocated (freed somewhere else ?)\n", (INT)ptr);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   102
		found++;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   103
	    }
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   104
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   105
	if (! found) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   106
	    e = (struct mallocList *) malloc(sizeof(struct mallocList));
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   107
	    e->next = mallocList;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   108
	    e->chunk = ptr;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   109
	    e->size = nBytes;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   110
	    mallocList = e;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   111
	    mallocCount++;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   112
	}
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   113
    }
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   114
}
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   115
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
char *
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   117
__stx_malloc(size_t nBytes) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   118
	char *ptr = malloc(nBytes);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   120
	if (@global(TraceMalloc) == true) {
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   121
	    console_printf("ExternalBytes [info]: allocated %ld bytes at: %p\n", (long)nBytes, ptr);
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   122
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   123
	addToMallocList(ptr, nBytes);
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   124
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   125
	return ptr;
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   126
}
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   127
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   128
char *
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   129
__stx_calloc(size_t n, size_t size) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   130
	char *ptr = __stx_malloc(n * size);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   131
	if (ptr != 0) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   132
	    bzero(ptr, (n * size));
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   133
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   134
	return ptr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
char *
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   138
__stx_realloc(char *ptr, size_t nBytes)
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   139
{
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   140
	char *newPtr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   142
	removeFromMallocList(ptr);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   143
	newPtr = realloc(ptr, nBytes);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   144
	addToMallocList(newPtr, nBytes);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   146
	if (@global(TraceMalloc) == true) {
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   147
	    console_printf("ExternalBytes [info]: realloc %ld bytes for %p at: %p\n", (long)nBytes, ptr, newPtr);
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   148
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   149
	return newPtr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   152
void
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
   153
__stx_free(char *ptr)
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
{
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   155
	if (@global(TraceMalloc) == true) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   156
	    console_printf("ExternalBytes: free bytes at: %"_lx_"\n", (INT)ptr);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   157
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   158
	removeFromMallocList(ptr);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   160
	free(ptr);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   163
void
3320
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   164
__stx_mallocStatistics() {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   165
	struct mallocList *this;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   166
	int amount = 0;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   167
	int n = 0;
3320
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   168
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   169
	for (this=mallocList; this; this=this->next) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   170
	    n++;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   171
	    amount += this->size;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   172
	}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   173
	console_printf("ExternalBytes [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
   174
}
09eb32bbceaf *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3211
diff changeset
   175
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
%}
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
!ExternalBytes class methodsFor:'documentation'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   181
copyright
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   182
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   183
 COPYRIGHT (c) 1993 by Claus Gittinger
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   184
	      All Rights Reserved
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   186
 This software is furnished under a license and may be used
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   187
 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
   188
 inclusion of the above copyright notice.   This software may not
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   189
 be provided or otherwise made available to, or used by, any
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   190
 other person.  No title to or ownership of the software is
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   191
 hereby transferred.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   192
"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   195
documentation
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   196
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   197
    This class provides access to any memory in the system. Its main purpose
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   198
    is to provide a baseclass for objects referencing structured external data.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   199
    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
   200
    to Smalltalk AND grants smalltalk access to individual bytes afterwards.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   201
    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
   202
    ExternalAddress. See more info there. Also, have a look at ExternalFunction
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   203
    which is another similar class, but specialized to represent callable C-functions.
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   204
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   205
    Since the memory address of an instance stays fixed (once allocated),
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   206
    it can also be used to share data with external C-parts
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   207
    (which are not prepared for objects to change their address).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   208
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   209
    Use with great care - access is not always checked for out-of-bounds
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   210
    or valid addresses.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   211
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   212
    Since the data is allocated outside the garbage collected smalltalk space,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   213
    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
   214
    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
   215
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   216
    To help in avoiding memory bugs, instances created with #new: are
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   217
    registered in a local classvar and deregistered when the underlying memory
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   218
    is explicitely freed. Since a life reference (from that classvar) exists,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   219
    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
   220
    underlying memory stays allocated (at a fix address) forever.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   221
    To release the memory, either #free it or #unprotect it.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   222
    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
   223
    freeing until the next garbage collect occurs.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   224
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   225
    If you need memory which is automatically freed, create
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   226
    the instance via #unprotectedNew: right away; the underlying malloced-memory
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   227
    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
   228
    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
   229
    exist to this memory).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   230
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   231
    To release all memory call #releaseAllMemory which simply sets the
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   232
    AllocatedInstances class variable to nil (thus releasing those refs).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   233
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   234
    Example (automatic freeing as soon as ref to buffer is gone):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   235
	|buffer|
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   236
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   237
	buffer := ExternalBytes unprotectedNew:100.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   238
	...
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   239
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   241
    Example (manual freeing - never freed, if ref to buffer is gone):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   242
	|buffer|
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   243
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   244
	buffer := ExternalBytes new:100.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   245
	...
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   246
	buffer free
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   247
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
    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
   250
	|buffer|
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   251
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   252
	buffer := ExternalBytes new:100.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   253
	...
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   254
	buffer unregister
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   255
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   256
    This class only supports unstructured external data
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   257
    - see the companion class ExternalStructure for more.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   258
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   259
    Notice: support for external data is still being developed -
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   260
	    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
   261
	    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
   262
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   263
    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
   264
    in other smalltalk systems, making code using it very unportable.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   265
    It is provided for C interfacing only.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   266
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   267
    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
   268
    in the normal case - especially for short term temporary objects,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   269
    automatically reclaimed object memory is about 5-10 times faster than
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   270
    malloc/free.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   271
    Things may be different for huge byte-valued objects, which are to be
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   272
    reclaimed by the oldspace colletor.
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   273
    Anyway, for portability, we strongly warn from using this as a substitute
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   274
    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
   275
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   276
    Debugging:
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   277
	since all manual memory systems are subject of obscure errors,
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   278
	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
   279
	done here. To do this, evaluate: 'ExternalBytes mallocTrace:true'.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   280
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   281
	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
   282
	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
   283
	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
   284
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   285
	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
   286
	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
   287
	and recompile your external c-libraries with this.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   289
	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
   290
	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
   291
	leftOver. This may help to find trouble spots in your C-code.
1286
4270a0b4917d documentation
Claus Gittinger <cg@exept.de>
parents: 1267
diff changeset
   292
4270a0b4917d documentation
Claus Gittinger <cg@exept.de>
parents: 1267
diff changeset
   293
    [author:]
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   294
	Claus Gittinger
1317
cc737e0fdf48 examples
Claus Gittinger <cg@exept.de>
parents: 1286
diff changeset
   295
cc737e0fdf48 examples
Claus Gittinger <cg@exept.de>
parents: 1286
diff changeset
   296
    [see also:]
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   297
	ExternalAddress ExternalFunction
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   298
	ByteArray
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   299
	( how to write primitive code :html: programming/primitive.html )
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
"
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   301
!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   302
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   303
examples
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   304
"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   305
    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
   306
    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
   307
    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
   308
    at 1.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   309
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   310
    allocating memory in ST, passing it to C:
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   312
      in smalltalk:
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   313
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   314
	bytes := ExternalBytes new:100.
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   315
	...
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   317
      in C (assuming that the bytes-object has been passed):
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   318
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   319
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   320
	char *bytePtr;
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   321
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   322
	bytePtr = (char *)(__externalBytesAddress(bytes));
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   323
	if (bytePtr) {
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   324
	    ... do something with bytes at bytePtr ...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   325
	}
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
      freeing (in ST):
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   329
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   330
	bytes free.
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   331
	...
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
    allocating memory in C, passing it to ST:
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
      in C:
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   336
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   337
	char *bytePtr;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   339
	bytePtr = (char *)(malloc(100));
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   340
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   341
	RETURN (__MKEXTERNALBYTES(bytePtr));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
      in Smalltalk (extracting bytes, integers or strings):
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   345
	byteVal := bytes byteAt:1.
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   346
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   347
	intVal := bytes doubleWordAt:1 MSB:true.
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   348
	...
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   349
	string := bytes stringAt:20.
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   350
	...
7191
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
    |data bytes2|
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   354
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   355
    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
   356
    bytes2 := ExternalBytes new:30.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   357
    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
   358
    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
   359
    bytes2
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   360
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   361
    |data bytes1 bytes2|
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   362
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   363
    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
   364
    bytes1 := ExternalBytes new:30.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   365
    bytes2 := ExternalBytes new:30.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   366
    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
   367
    bytes2 atAllPut:99.
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   368
    bytes2 replaceBytesFrom:2 to:21 with:bytes1 startingAt:1.
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   369
    bytes2 asByteArray
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
   370
7192
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   371
    |data1 bytes1 data2|
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   372
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   373
    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
   374
    bytes1 := ExternalBytes new:30.
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   375
    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
   376
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   377
    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
   378
    data2 replaceBytesFrom:2 to:21 with:bytes1 startingAt:1.
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   379
    data2
7192
3a388e746946 tuned the replaceBytes to/from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7191
diff changeset
   380
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
"
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
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   384
!ExternalBytes class methodsFor:'initialization'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   385
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   386
initialize
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   387
    Lobby isNil ifTrue:[
11854
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
   388
	Lobby := Registry new.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   389
    ]
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   390
! !
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   391
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
!ExternalBytes class methodsFor:'instance creation'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   394
address:anAddressInteger
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   395
    "return a new ExternalBytes object to access bytes starting at anAddressInteger.
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   396
     The memory at anAddressInteger has been allocated elsewhere.
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   397
     The size is not known, therefore byte accesses will NOT be checked for valid index.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
     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
   399
     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
   400
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
     DANGER ALERT: this method allows very bad things to be done to the
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   402
		   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
   403
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   404
    ^ self basicNew setAddress:anAddressInteger size:nil
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   405
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   406
    "Modified (comment): / 31-03-2016 / 11:05:07 / cg"
848
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
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   409
address:anAddressInteger size:size
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   410
    "return a new ExternalBytes object to access bytes starting at anAddressInteger.
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   411
     The memory at anAddressInteger has been allocated elsewhere.
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   412
     The size is known, which allows byte accesses to be checked for valid index.
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   413
     Use this, if you get a pointer to a structure from some external source
848
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.
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   415
     The pointer is protected from GC
22805
bcf1140358c8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22335
diff changeset
   416
     (i.e. I will not free the heap memory,
bcf1140358c8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22335
diff changeset
   417
      once the returned reference is no longer in use).
bcf1140358c8 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 22335
diff changeset
   418
     Be careful to avoid memory leaks, when getting malloc'd memory from an external function.
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   419
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
     DANGER ALERT: this method allows very bad things to be done to the
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   421
		   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
   422
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   423
    ^ self basicNew setAddress:anAddressInteger size:size
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   424
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   425
    "Modified (comment): / 31-03-2016 / 11:04:27 / cg"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   428
new:numberOfBytes
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   429
    "allocate some memory usable for data;
15398
4c73c49852d6 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 15262
diff changeset
   430
     the memory safe from being finalized by the garbage collector.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   431
     Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   432
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   433
     Use this, if you have to pass a block of bytes to some
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   434
     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
   435
     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
   436
     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
   437
     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
   438
     may change their address.
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
     DANGER ALERT: the memory is NOT automatically freed until it is either
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   441
		   MANUALLY freed (see #free) or the returned externalBytes object
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   442
		   is unprotected or the classes releaseAllMemory method is called."
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
22224
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   444
    "/ ^ self protectedNew:numberOfBytes.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   445
    ^ self unprotectedNew:numberOfBytes.
848
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
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
     |bytes|
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
     bytes := ExternalBytes new:100.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
     bytes wordAt:1 put:1.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
     bytes doubleWordAt:3 put:16r12345678.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
     bytes inspect
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
    "
22224
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   455
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   456
    "Modified: / 29-08-2017 / 16:52:31 / cg"
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   457
!
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
10925
61c04a27097e add #newNullTerminatedFromString:
fm
parents: 10178
diff changeset
   459
newNullTerminatedFromString:aString
19422
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   460
    "allocate a null terminated string containing the chars of aString"
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   461
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   462
    |nChars extBytes|
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   463
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   464
    nChars := aString size.
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   465
    self assert:(aString bitsPerCharacter == 8).
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   466
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   467
    extBytes := self new:nChars+1.
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   468
    extBytes replaceBytesFrom:1 to:nChars with:aString startingAt:1.
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   469
    extBytes at:nChars+1 put:0.
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   470
    ^ extBytes
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   471
!
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   472
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   473
newNullTerminatedFromWideString:aString
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   474
    "allocate a null terminated wide string containing the U16-chars of aString"
19422
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   475
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   476
    |nChars extBytes|
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   477
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   478
    nChars := aString size.
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   479
    self assert:(aString bitsPerCharacter <= 16).
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   480
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   481
    extBytes := self new:((nChars+1)*2).
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   482
    1 to:nChars do:[:i |
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   483
	extBytes unsignedInt16At:(i*2) put:(aString at:i) codePoint.
19422
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   484
    ].
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   485
    extBytes unsignedInt16At:((nChars+1)*2) put:0.
a8a0b538f7da #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19419
diff changeset
   486
    ^ extBytes
19512
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   487
e31128e0b135 #DOCUMENTATION
Claus Gittinger <cg@exept.de>
parents: 19495
diff changeset
   488
    "Modified (comment): / 31-03-2016 / 11:05:37 / cg"
10925
61c04a27097e add #newNullTerminatedFromString:
fm
parents: 10178
diff changeset
   489
!
61c04a27097e add #newNullTerminatedFromString:
fm
parents: 10178
diff changeset
   490
22224
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   491
protectedNew:numberOfBytes
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   492
    "allocate some memory usable for data;
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   493
     the memory safe from being finalized by the garbage collector.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   494
     Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   495
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   496
     Use this, if you have to pass a block of bytes to some
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   497
     external destination (such as a C function) which does not copy the
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   498
     data, but instead keeps a reference to it. For example, many functions
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   499
     which expect strings simply keep a ref to the passed string - for those,
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   500
     an ST/X string-pointer is not the right thing to pass, since ST/X objects
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   501
     may change their address.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   502
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   503
     DANGER ALERT: the memory is NOT automatically freed until it is either
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   504
		   MANUALLY freed (see #free) or the returned externalBytes object
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   505
		   is unprotected or the classes releaseAllMemory method is called."
22224
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   506
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   507
    |newInst|
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   508
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   509
    newInst := self unprotectedNew:numberOfBytes.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   510
    newInst protectFromGC.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   511
    ^ newInst
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   512
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   513
    "
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   514
     |bytes|
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   515
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   516
     bytes := ExternalBytes new:100.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   517
     bytes wordAt:1 put:1.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   518
     bytes doubleWordAt:3 put:16r12345678.
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   519
     bytes inspect
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   520
    "
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   521
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   522
    "Created: / 29-08-2017 / 16:52:14 / cg"
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   523
!
914289d8db20 #BUGFIX by cg
Claus Gittinger <cg@exept.de>
parents: 22159
diff changeset
   524
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   525
unprotectedNew:numberOfBytes
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   526
    "allocate some memory usable for data;
15398
4c73c49852d6 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 15262
diff changeset
   527
     the memory is under the control of the garbage collector (i.e. the instance will
4c73c49852d6 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 15262
diff changeset
   528
     be finalized and the malloc'd memory will be freed, if the instance goes away).
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   529
     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
   530
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   531
     DANGER ALERT: the memory block as allocated will be automatically freed
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   532
		   as soon as the reference to the returned externalBytes object
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   533
		   is gone (by the next garbage collect).
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   534
		   If the memory has been passed to a C-function which
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   535
		   remembers this pointer, bad things may happen ...."
848
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
    |newInst|
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   539
    newInst := self basicNew allocateBytes:numberOfBytes.
9388
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
   540
    newInst registerForFinalization.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   541
    ^ newInst
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   542
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   543
    "
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   544
     |bytes|
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   545
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   546
     bytes := ExternalBytes unprotectedNew:100.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   547
     bytes wordAt:1 put:1.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   548
     bytes doubleWordAt:3 put:16r12345678.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   549
     bytes inspect
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
   550
    "
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   553
!ExternalBytes class methodsFor:'instance release'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   554
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   555
releaseAllMemory
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   556
    AllocatedInstances := nil
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   557
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   558
     ... the next GC will get 'em
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
! !
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   561
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
!ExternalBytes class methodsFor:'malloc debug'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   564
dumpMallocChunks
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   565
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   566
    struct mallocList *entry;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   568
    for (entry = mallocList; entry; entry=entry->next) {
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   569
	console_printf("  %p (%ld)\n", (entry->chunk), (long)(entry->size));
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   570
    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   571
%}
3321
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   572
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   573
     self dumpMallocChunks
3321
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   574
    "
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   577
freeAllMallocChunks
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   578
    "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
   579
     It simply walks through all chunks and frees them unconditionally.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   580
     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
   581
     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
   582
     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
   583
     malloc redefined to stx_malloc.
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
     Also, mallocDebug has to be on to do this."
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
    "first free my own memory ..."
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
    self releaseAllMemory.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
    ObjectMemory garbageCollect.
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
    struct mallocList *entry;
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
    while ((entry = mallocList) != (struct mallocList *)0) {
13704
Claus Gittinger <cg@exept.de>
parents: 13196
diff changeset
   594
	if (@global(TraceMalloc) == true ) {
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   595
	    console_printf("ExternalBytes [info]: **** forced free of %p (%ld)\n", entry->chunk, (long)(entry->size));
13704
Claus Gittinger <cg@exept.de>
parents: 13196
diff changeset
   596
	}
Claus Gittinger <cg@exept.de>
parents: 13196
diff changeset
   597
	__stx_free(entry->chunk);
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
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   602
mallocDebug:aBoolean
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
    DebugMalloc := aBoolean
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
     ExternalBytes mallocDebug:true
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   607
     ExternalBytes mallocDebug:false
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
    "
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
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   611
mallocStatistics
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   612
%{
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   613
    __stx_mallocStatistics();
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   614
%}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   615
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   616
     self mallocStatistics
3486
bbd47c3011e5 comment
Claus Gittinger <cg@exept.de>
parents: 3321
diff changeset
   617
    "
bbd47c3011e5 comment
Claus Gittinger <cg@exept.de>
parents: 3321
diff changeset
   618
!
bbd47c3011e5 comment
Claus Gittinger <cg@exept.de>
parents: 3321
diff changeset
   619
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   620
mallocTrace:aBoolean
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   621
    TraceMalloc := aBoolean
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   622
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   623
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   624
     ExternalBytes mallocTrace:true
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   625
     ExternalBytes mallocTrace:false
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   626
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   627
!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   628
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   629
numberOfAllocatedChunks
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   630
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   631
    RETURN ( __mkSmallInteger(mallocCount) );
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   632
%}
3321
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   633
    "
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   634
     self numberOfAllocatedChunks
4abefa756c45 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3320
diff changeset
   635
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   636
! !
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   637
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   638
!ExternalBytes class methodsFor:'queries'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   639
21053
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   640
charTypeIsSigned
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   641
    "return true, if the machine's native chars are signed"
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   642
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   643
%{  /* NOCONTEXT */
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   644
    char c;
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   645
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   646
    c = (char)128;
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   647
    RETURN ( (int)c < 0 ? true : false );
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   648
%}
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   649
    "
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   650
     ExternalBytes charTypeIsSigned
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   651
    "
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   652
!
e7a717fde76a #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20489
diff changeset
   653
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   654
doubleAlignment
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   655
    "return the alignement of longs in structs and unions"
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   656
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   657
%{  /* NOCONTEXT */
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   658
    struct {
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   659
	char c;
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   660
	double d;
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   661
    } dummy;
18582
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   662
    RETURN (__mkSmallInteger( (char *)&dummy.d - (char *)&dummy.c ));
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   663
%}
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   664
    "
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   665
     ExternalBytes doubleAlignment
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   666
    "
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   667
!
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   668
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   669
isBuiltInClass
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   670
    "return true if this class is known by the run-time-system.
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   671
     Here, true is returned."
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   672
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   673
    ^ self == ExternalBytes
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   674
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   675
    "Modified: / 11.6.1998 / 17:12:51 / cg"
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   676
!
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
   677
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   678
longAlignment
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   679
    "return the alignement of longs in structs and unions"
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   680
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   681
%{  /* NOCONTEXT */
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   682
    struct {
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   683
	char c;
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   684
	long l;
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   685
    } dummy;
18582
51ee4f8fa9f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18580
diff changeset
   686
    RETURN (__mkSmallInteger( (char *)&dummy.l - (char *)&dummy.c ));
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   687
%}
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   688
    "
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   689
     ExternalBytes longAlignment
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   690
    "
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   691
!
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
   692
22872
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   693
pointerAlignment
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   694
    "return the alignement of pointers in structs and unions"
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   695
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   696
%{  /* NOCONTEXT */
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   697
    struct {
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   698
	char c;
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   699
	void* p;
22872
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   700
    } dummy;
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   701
    RETURN (__mkSmallInteger( (char *)&dummy.p - (char *)&dummy.c ));
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   702
%}
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   703
    "
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   704
     ExternalBytes pointerAlignment
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   705
    "
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   706
!
eca075dc2a34 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22805
diff changeset
   707
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   708
sizeofDouble
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   709
    "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
   710
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   712
    RETURN (__mkSmallInteger( sizeof(double)));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
    "
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   715
     ExternalBytes sizeofDouble
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   716
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   717
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   718
20489
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   719
sizeofEnums
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   720
    "return the number of bytes used by the machine's native enums.
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   721
     Be aware, that this can be adjusted in some compilers via the __packed__ attribute;
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   722
     So better double check..."
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   723
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   724
%{  /* NOCONTEXT */
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   725
    enum foo { bla1, bla2 } foo;
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   726
    RETURN (__mkSmallInteger( sizeof(foo)));
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   727
%}
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   728
    "
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   729
     ExternalBytes sizeofEnums
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   730
    "
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   731
!
413138df0173 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 20481
diff changeset
   732
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   733
sizeofFloat
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   734
    "return the number of bytes used by the machine's native floats"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   735
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   736
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   737
    RETURN (__mkSmallInteger( sizeof(float)));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   738
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   739
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   740
     ExternalBytes sizeofFloat
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   741
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   742
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   743
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   744
sizeofInt
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   745
    "return the number of bytes used by the machine's native integer"
848
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
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   748
    RETURN (__mkSmallInteger( sizeof(int)));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   749
%}
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
     ExternalBytes sizeofInt
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   752
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   753
!
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   754
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   755
sizeofLong
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   756
    "return the number of bytes used by the machine's native longs"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   758
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   759
    RETURN (__mkSmallInteger( sizeof(long)));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
     ExternalBytes sizeofLong
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   766
sizeofLongDouble
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   767
    "return the number of bytes used by the machine's native longdouble.
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   768
     If the machine does not support them, return nil."
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   769
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   770
%{  /* NOCONTEXT */
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
   771
#if defined(__GNUC__) || defined(__CLANG__) || defined(__win32__)
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   772
    RETURN (__mkSmallInteger( sizeof(long double)));
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   773
#endif
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   774
%}.
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   775
    ^ nil
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   776
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   777
    "
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
   778
     ExternalBytes sizeofLongDouble
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   779
    "
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   780
!
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   781
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   782
sizeofLongLong
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   783
    "return the number of bytes used by the machine's native longlongs.
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   784
     If the machine does not support them, return nil."
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   785
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   786
%{  /* NOCONTEXT */
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   787
#ifdef HAS_LONGLONG
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   788
    RETURN (__mkSmallInteger( sizeof(long long)));
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   789
#endif
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   790
%}.
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   791
    ^ nil
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   792
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   793
    "
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   794
     ExternalBytes sizeofLongLong
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   795
    "
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   796
!
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   797
18878
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   798
sizeofNativeInt
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   799
    "return the number of bytes used by the machine's SmallInteger native values"
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   800
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   801
%{  /* NOCONTEXT */
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   802
    RETURN (__mkSmallInteger( sizeof(INT)));
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   803
%}
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   804
    "
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   805
     ExternalBytes sizeofNativeInt
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   806
    "
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   807
!
f370d1f65d08 #FEATURE
Stefan Vogel <sv@exept.de>
parents: 18829
diff changeset
   808
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   809
sizeofPointer
22898
c77cc993e3d8 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22872
diff changeset
   810
    "return the number of bytes used by the machine's native pointer.
c77cc993e3d8 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22872
diff changeset
   811
     Notice: this is inlined by the compiler(s) as a constant,
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   812
     therefore, queries like
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   813
	'ExternalAddress pointerSize == 8'
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   814
     cost nothing; they are compiled in as a constant
22898
c77cc993e3d8 #REFACTORING by cg
Claus Gittinger <cg@exept.de>
parents: 22872
diff changeset
   815
     (and even conditionals are eliminated)."
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   816
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   817
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   818
    RETURN (__mkSmallInteger( sizeof(char *)));
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   819
%}
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   820
    "
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   821
     ExternalBytes sizeofPointer
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   822
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   823
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   824
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   825
sizeofShort
17627
adba1b4d01c9 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 16739
diff changeset
   826
    "return the number of bytes used by the machine's native short"
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   827
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   828
%{  /* NOCONTEXT */
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   829
    RETURN (__mkSmallInteger( sizeof(short)));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   830
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   831
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   832
     ExternalBytes sizeofShort
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   833
    "
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   834
! !
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   835
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   836
!ExternalBytes methodsFor:'accessing'!
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   837
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   838
address
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   839
    "return the start address as an integer"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   840
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   841
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   842
11629
2816596e0b2e changed #address
Claus Gittinger <cg@exept.de>
parents: 11628
diff changeset
   843
    if (__INST(address_) != nil) {
11854
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
   844
	unsigned INT addr;
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   845
11854
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
   846
	addr = (unsigned INT)__INST(address_);
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
   847
	RETURN ( __MKUINT(addr));
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   848
    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   849
%}.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   850
    ^ nil
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   851
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   852
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   853
basicAt:index
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   854
    "return the byte at index, anInteger;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   855
     Indices are 1-based, therefore
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   856
     this is the byte at (address + index - 1)"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   857
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   858
%{  /* NOCONTEXT */
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   859
1822
e7b977512062 type casts
Claus Gittinger <cg@exept.de>
parents: 1317
diff changeset
   860
    unsigned char *cp = (unsigned char *)(__INST(address_));
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   861
    int idx;
1997
4c5a80c2d570 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1822
diff changeset
   862
    OBJ sz;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   863
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   864
    if (cp && __isSmallInteger(index)) {
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   865
	idx = __intVal(index);
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   866
	if (idx > 0) {
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   867
	    if (((sz = __INST(size)) == nil)
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   868
	     || (__intVal(sz) >= idx)) {
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   869
		cp = cp + idx - 1;
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8908
diff changeset
   870
		RETURN ( __mkSmallInteger((*cp)) );
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   871
	    }
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   872
	}
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   873
    }
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   874
%}.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   875
    (size notNil and:[self address notNil]) ifTrue:[
6242
3d1358719ab4 refactored debug malloc
Claus Gittinger <cg@exept.de>
parents: 5600
diff changeset
   876
	^ self subscriptBoundsError:index
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   877
    ].
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   878
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   879
     invalid index or unallocated
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   880
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   881
    self primitiveFailed
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   882
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   883
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   884
basicAt:index put:value
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   885
    "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
   886
     Returns value (sigh).
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   887
     Indices are 1-based, therefore
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   888
     this is the byte at (address + index - 1)"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   889
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   890
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   891
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   892
    unsigned char *cp = (unsigned char *)(__INST(address_));
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   893
    int val;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   894
    int idx;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   895
    OBJ sz;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   896
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   897
    if (__isSmallInteger(value)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   898
	val = __smallIntegerVal(value);
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
   899
    } else if (__isCharacter(value)) {
15262
5047292c9107 all stx macros begin with double underline (eg. __qClass instead of _qClass)
Claus Gittinger <cg@exept.de>
parents: 15003
diff changeset
   900
	val = __smallIntegerVal(__characterVal(value));
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   901
    } else
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   902
	goto badArg;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   903
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   904
    if (cp && __isSmallInteger(index)) {
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   905
	idx = __intVal(index);
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   906
	if (idx > 0) {
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   907
	    if (((sz = __INST(size)) == nil)
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   908
	     || (__intVal(sz) >= idx)) {
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   909
		if ((val & ~0xFF) == 0) /* i.e. (val >= 0) && (val <= 255) */  {
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   910
		    cp[idx-1] = val;
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   911
		    RETURN ( value );
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   912
		}
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   913
	    }
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   914
	}
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   915
    }
6302
32ede83e77e5 allow for character arg in #at:put:
Claus Gittinger <cg@exept.de>
parents: 6242
diff changeset
   916
32ede83e77e5 allow for character arg in #at:put:
Claus Gittinger <cg@exept.de>
parents: 6242
diff changeset
   917
badArg: ;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   918
%}.
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   919
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   920
    (size notNil and:[self address notNil]) ifTrue:[
6445
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   921
	(index between:1 and:size) ifTrue:[
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   922
	    ^ ElementOutOfBoundsSignal raise
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   923
	].
45d1c43350a1 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6444
diff changeset
   924
	^ self subscriptBoundsError:index
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   925
    ].
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   926
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   927
     invalid index, invalid value or unallocated
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   928
    "
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   929
    self primitiveFailed
1220
99990bbb561f commentary
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   930
99990bbb561f commentary
Claus Gittinger <cg@exept.de>
parents: 1133
diff changeset
   931
    "Modified: 19.4.1996 / 11:15:05 / cg"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   932
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   933
13196
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   934
copyCStringFromHeap
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   935
    "fetch a 0-terminated string from my pointed-to address"
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
   936
12594
d7267a747c2f changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 11854
diff changeset
   937
    |idx byte s|
11739
d04e05026d39 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11629
diff changeset
   938
12594
d7267a747c2f changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 11854
diff changeset
   939
    idx := 1.
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   940
    s := WriteStream on:(String new:10).
12594
d7267a747c2f changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 11854
diff changeset
   941
    [(byte := self at:idx) ~~ 0] whileTrue:[
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   942
        s nextPut:(Character value:byte).
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   943
        idx := idx + 1.
11739
d04e05026d39 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11629
diff changeset
   944
    ].
13196
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   945
    ^ s contents
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   946
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   947
    "Modified: / 11-04-2019 / 08:49:50 / Claus Gittinger"
13196
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   948
!
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   949
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   950
copyUnicodeStringFromHeap
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   951
    "fetch a 0-terminated wide-string (16bit)  from my pointed-to address"
13196
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   952
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   953
    |idx word s|
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   954
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   955
    idx := 1.
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   956
    s := WriteStream on:(Unicode16String new:10).
19419
362d601fee24 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19392
diff changeset
   957
    [(word := self unsignedInt16At:idx) ~~ 0] whileTrue:[
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   958
        s nextPut:(Character value:word).
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   959
        idx := idx + 2.
13196
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   960
    ].
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   961
    ^ s contents
24056
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   962
a7b360193da5 #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 24043
diff changeset
   963
    "Modified (comment): / 11-04-2019 / 08:55:29 / Claus Gittinger"
13196
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   964
!
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   965
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   966
instVarAt:index
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   967
    "redefined to suppress direct access to my address, which is a non-object"
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   968
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   969
    index == 1 ifTrue:[^ self address].
a8e595628fb5 added: #copyUnicodeStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 13076
diff changeset
   970
    ^ super instVarAt:index
11739
d04e05026d39 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11629
diff changeset
   971
! !
d04e05026d39 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11629
diff changeset
   972
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   973
!ExternalBytes methodsFor:'converting'!
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   974
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   975
asExternalAddress
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   976
    "return the start address as an external address"
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   977
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   978
%{  /* NOCONTEXT */
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   979
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   980
    RETURN(__MKEXTERNALADDRESS(__INST(address_)));
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   981
%}.
19419
362d601fee24 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19392
diff changeset
   982
    self primitiveFailed
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   983
!
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   984
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   985
asExternalBytes
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   986
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
   987
    ^ self
20138
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   988
!
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   989
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   990
asString
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   991
    "speed up string conversions"
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   992
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   993
    |size|
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   994
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   995
    self class == ExternalBytes ifTrue:[
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   996
	size := self size.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
   997
	^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1.
20138
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   998
    ].
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
   999
    ^ super asString.
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
  1000
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
  1001
    "
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
  1002
      #[16r41 16r42 16r43] asExternalBytes asString
cd6fdc7534e4 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 19860
diff changeset
  1003
    "
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1004
! !
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1005
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1006
!ExternalBytes methodsFor:'filling & replacing'!
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1007
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1008
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1009
    "replace elements from another collection, which must be a ByteArray-
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1010
     like collection.
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1011
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1012
     Notice: This operation modifies the receiver, NOT a copy;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1013
     therefore the change may affect all others referencing the receiver."
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1014
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1015
%{  /* NOCONTEXT */
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1016
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1017
    int nIndex, repNIndex;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1018
    int startIndex, stopIndex;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1019
    REGISTER unsigned char *src;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1020
    REGISTER int repStartIndex;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1021
    int repStopIndex, count;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1022
    REGISTER unsigned char *dst;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1023
    OBJ cls;
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1024
14076
5f68cf5ece9c changed: #replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 13704
diff changeset
  1025
    if ((__isBytes(aCollection) || __isWords(aCollection) || __isExternalBytesLike(aCollection))
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1026
     && __bothSmallInteger(start, stop)
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1027
     && __isSmallInteger(repStart)) {
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1028
	startIndex = __intVal(start) - 1;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1029
	if (startIndex >= 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1030
	    dst = (unsigned char *)__INST(address_) + startIndex;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1031
	    nIndex = __smallIntegerVal(__INST(size));
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1032
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1033
	    stopIndex = __intVal(stop) - 1;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1034
	    count = stopIndex - startIndex + 1;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1035
	    if (count == 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1036
		RETURN ( self );
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1037
	    }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1038
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1039
	    if ((count > 0) && (stopIndex < nIndex)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1040
		repStartIndex = __intVal(repStart) - 1;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1041
		if (repStartIndex >= 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1042
		    if (__isExternalBytesLike(aCollection)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1043
			OBJ sz;
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
  1044
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1045
			src = __externalBytesVal(aCollection);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1046
			if (src == 0) goto fallBack;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1047
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1048
			sz = __externalBytesSize(aCollection);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1049
			if (__isSmallInteger(sz)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1050
			    repNIndex = __smallIntegerVal(sz);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1051
			} else {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1052
			    repNIndex = -1; /* unknown */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1053
			}
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1054
			src = src + repStartIndex;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1055
		    } else {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1056
			repNIndex = __qSize(aCollection) - OHDR_SIZE;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1057
			src = __byteArrayVal(aCollection) + repStartIndex;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1058
			if ((cls = __qClass(aCollection)) != @global(ByteArray)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1059
			    int nInst;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1060
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1061
			    nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars));
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1062
			    src += nInst;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1063
			    repNIndex -= nInst;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1064
			}
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1065
		    }
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
  1066
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1067
		    repStopIndex = repStartIndex + (stopIndex - startIndex);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1068
		    if (repStopIndex < repNIndex) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1069
			if (aCollection == self) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1070
			    /* take care of overlapping copy */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1071
			    if (src < dst) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1072
				/* must do a reverse copy */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1073
				src += count;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1074
				dst += count;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1075
				while (count-- > 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1076
				    *--dst = *--src;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1077
				}
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1078
				RETURN ( self );
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1079
			    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1080
			}
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1081
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1082
#ifdef memcpy4
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1083
			if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1084
			    int nW;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1085
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1086
			    /* copy unaligned part */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1087
			    while (count && ((unsigned INT)src & 3)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1088
				*dst++ = *src++;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1089
				count--;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1090
			    }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1091
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1092
			    if (count > 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1093
				/* copy aligned part */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1094
				nW = count >> 2;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1095
				memcpy4(dst, src, nW);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1096
				if ((count = count & 3) != 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1097
				    /* copy any remaining part */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1098
				    src += (nW<<2);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1099
				    dst += (nW<<2);
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1100
				    while (count--) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1101
					*dst++ = *src++;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1102
				    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1103
				}
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1104
			    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1105
			    RETURN ( self );
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1106
			}
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1107
#else
8901
824a89d0b5c7 alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents: 8288
diff changeset
  1108
# if __POINTER_SIZE__ == 8
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1109
			if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1110
			    /* copy unaligned part */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1111
			    while (count && ((unsigned INT)src & 7)) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1112
				*dst++ = *src++;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1113
				count--;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1114
			    }
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1115
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1116
			    /* copy aligned part */
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1117
			    while (count >= 8) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1118
				((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0];
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1119
				dst += 8;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1120
				src += 8;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1121
				count -= 8;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1122
			    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1123
			    while (count--) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1124
				*dst++ = *src++;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1125
			    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1126
			    RETURN ( self );
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1127
			}
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1128
# endif /* 64bit */
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1129
#endif /* memcpy4 */
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1130
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1131
#ifdef FAST_MEMCPY
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1132
			memcpy(dst, src, count);
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1133
#else
8919
707a9ff7f9b2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1134
# ifdef __UNROLL_LOOPS__
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1135
			while (count >= 8) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1136
			    dst[0] = src[0]; dst[1] = src[1];
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1137
			    dst[2] = src[2]; dst[3] = src[3];
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1138
			    dst[4] = src[4]; dst[5] = src[5];
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1139
			    dst[6] = src[6]; dst[7] = src[7];
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1140
			    dst += 8; src += 8;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1141
			    count -= 8;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1142
			}
8919
707a9ff7f9b2 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 8913
diff changeset
  1143
# endif /* __UNROLL_LOOPS__ */
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1144
			while (count-- > 0) {
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1145
			    *dst++ = *src++;
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1146
			}
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1147
#endif
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1148
			RETURN ( self );
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1149
		    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1150
		}
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1151
	    }
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1152
	}
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1153
    }
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
  1154
fallBack: ;
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1155
%}.
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1156
    "
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1157
     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
  1158
    "
7191
82bddba980b6 tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents: 7184
diff changeset
  1159
    ^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1160
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1161
    "
14125
6aec556d2bbf comment changed: #replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 14080
diff changeset
  1162
     ((ExternalBytes unprotectedNew:16)
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1163
	    replaceBytesFrom:1 to:8
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1164
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1165
	    startingAt:1) copy
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1166
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1167
     (ExternalBytes unprotectedNew:16)
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1168
	    replaceBytesFrom:3 to:10
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1169
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1170
	    startingAt:4
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1171
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1172
     (ExternalBytes unprotectedNew:16)
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1173
	    replaceBytesFrom:3 to:4
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1174
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1175
	    startingAt:1
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1176
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1177
     (ExternalBytes unprotectedNew:16)
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1178
	    replaceBytesFrom:0 to:9
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1179
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1180
	    startingAt:1
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1181
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1182
     (ExternalBytes unprotectedNew:16)
14632
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1183
	    replaceBytesFrom:1 to:10
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1184
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
6fe0dc1d5377 64bit mingw changes
Claus Gittinger <cg@exept.de>
parents: 14125
diff changeset
  1185
	    startingAt:0
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1186
    "
19168
f09f654da659 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18878
diff changeset
  1187
!
f09f654da659 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18878
diff changeset
  1188
f09f654da659 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18878
diff changeset
  1189
replaceNullTerminatedFromString:aString
f09f654da659 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18878
diff changeset
  1190
    "replace elements from aString, and add a 0-byte at the end"
f09f654da659 #FEATURE
Claus Gittinger <cg@exept.de>
parents: 18878
diff changeset
  1191
19169
a1efd2d6d7ca #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19168
diff changeset
  1192
    |nChars|
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1193
19169
a1efd2d6d7ca #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19168
diff changeset
  1194
    nChars := aString size.
a1efd2d6d7ca #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19168
diff changeset
  1195
    self replaceBytesFrom:1 to:nChars with:aString startingAt:1.
a1efd2d6d7ca #FEATURE
Claus Gittinger <cg@exept.de>
parents: 19168
diff changeset
  1196
    self at:nChars+1 put:0.
6483
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1197
! !
ec599166f169 replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents: 6471
diff changeset
  1198
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1199
!ExternalBytes methodsFor:'finalization'!
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1200
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
  1201
executor
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1202
    "redefined to return a lightweight copy
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1203
     - 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
  1204
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1205
%{ /* NOCONTEXT */
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
  1206
7386
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
  1207
    OBJ theCopy, cls;
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1208
7855
27f64d1beaa1 Have to protect self, otherwise bad things happen in the VM.
Stefan Vogel <sv@exept.de>
parents: 7386
diff changeset
  1209
    __PROTECT__(self);
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1210
    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
  1211
    __UNPROTECT__(self);
7386
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
  1212
    __InstPtr(theCopy)->o_class = cls = __InstPtr(self)->o_class;
0996b933cbf1 Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents: 7257
diff changeset
  1213
    __STORE(theCopy, cls);
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1214
    RETURN (theCopy);
6491
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1215
%}
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1216
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1217
    "
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1218
      (ExternalBytes unprotectedNew:10) executor
6976d9b69e01 Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents: 6484
diff changeset
  1219
    "
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
  1220
!
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
  1221
6462
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1222
finalizationLobby
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1223
    "answer the registry used for finalization.
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1224
     ExternalBytes have their own Registry"
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1225
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1226
    ^ Lobby
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1227
!
245e99c09df0 Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents: 6459
diff changeset
  1228
6440
eb9cdc352a7c Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents: 6425
diff changeset
  1229
finalize
2779
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
  1230
    "some ExternalBytes object was finalized;
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
  1231
     free the associated heap memory with it"
e895876895d3 64bit changes
Claus Gittinger <cg@exept.de>
parents: 2769
diff changeset
  1232
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1233
%{  /* NOCONTEXT */
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
  1234
    char *mem = (char *)__INST(address_);
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1235
    if (mem && (OBJ)mem != nil) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1236
	__stx_free(mem);
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1237
    }
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 913
diff changeset
  1238
    __INST(address_) = __INST(size) = nil;
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1239
%}
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1240
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1241
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1242
!ExternalBytes methodsFor:'freeing'!
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1243
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1244
free
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1245
    "free a previously allocated piece of memory - be very careful, there
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1246
     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
  1247
     are present here ..."
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1248
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1249
    "at least, we check for double freeing the same chunk"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1250
    self address isNil ifTrue:[
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1251
	self error:'freeing memory twice'.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1252
	^ self
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1253
    ].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1254
    Lobby unregister:self.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1255
    self finalize.  "/ does what we need here ..
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1256
    self unprotectFromGC
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1257
!
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1258
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1259
register
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1260
    "register the receiver to be automatically finalized by the GC"
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1261
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1262
    Lobby register:self.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1263
! !
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1264
20188
e237016b38a7 #OTHER by mawalch
mawalch
parents: 20138
diff changeset
  1265
!ExternalBytes methodsFor:'pointer arithmetic'!
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1266
6502
87f587d1542e rename #from:to to #referenceToBytesFrom:to
Stefan Vogel <sv@exept.de>
parents: 6491
diff changeset
  1267
referenceToBytesFrom:start to:stop
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1268
   "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
  1269
    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
  1270
    contents is undefined"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1271
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1272
%{ /* NOCONTEXT */
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1273
6471
186b2bb08a0a compilable under bcc55
Claus Gittinger <cg@exept.de>
parents: 6462
diff changeset
  1274
    char *addr;
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1275
    int size;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1276
    int __start, __stop;
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1277
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1278
    if (__bothSmallInteger(start, stop) && __INST(address_) != nil) {
6503
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1279
	__start = __smallIntegerVal(start);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1280
	__stop = __smallIntegerVal(stop);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1281
	if (__start > 0 && __start <= __stop && __stop <= __smallIntegerVal(__INST(size))) {
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1282
	    addr = (char *)(__INST(address_)) + (__start - 1);
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1283
	    size = __stop - __start + 1;
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1284
	    RETURN( __MKEXTERNALBYTES_N(addr, size) );
bb1899d47c7b Fix types.
Stefan Vogel <sv@exept.de>
parents: 6502
diff changeset
  1285
	}
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1286
    }
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1287
%}.
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1288
   ^ self primitiveFailed
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1289
! !
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1290
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1291
!ExternalBytes methodsFor:'printing & storing'!
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1292
13076
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1293
displayOn:aGCOrStream
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1294
    "return a printed representation of the receiver for displaying"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1295
13076
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1296
    |addr|
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1297
13076
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1298
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
21548
892d1a455001 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21406
diff changeset
  1299
    "/ old ST80 means: draw-yourself on a GC.
16739
5e8800295759 displayOn: cleanup
Claus Gittinger <cg@exept.de>
parents: 16705
diff changeset
  1300
    (aGCOrStream isStream) ifFalse:[
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1301
	^ super displayOn:aGCOrStream
13076
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1302
    ].
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1303
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1304
    aGCOrStream nextPutAll:self className.
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1305
    addr := self address.
29b8a558bcf2 Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 12864
diff changeset
  1306
    addr isNil ifTrue:[
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1307
	aGCOrStream nextPutAll:'[free]'.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1308
    ] ifFalse:[
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1309
	size notNil ifTrue:[
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1310
	    aGCOrStream nextPutAll:'[sz:'.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1311
	    size printOn:aGCOrStream.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1312
	    aGCOrStream space.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1313
	] ifFalse:[
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1314
	    aGCOrStream nextPut:$[.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1315
	].
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1316
	aGCOrStream nextPutAll:'@'.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1317
	addr printOn:aGCOrStream base:16.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1318
	aGCOrStream nextPut:$].
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1319
    ].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1320
18829
1f56d744775b #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 18653
diff changeset
  1321
    "
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1322
	self new printString
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1323
	(self new:5) displayString
18829
1f56d744775b #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 18653
diff changeset
  1324
    "
1f56d744775b #BUGFIX
Stefan Vogel <sv@exept.de>
parents: 18653
diff changeset
  1325
21548
892d1a455001 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21406
diff changeset
  1326
    "Modified: / 24-02-2000 / 19:02:19 / cg"
892d1a455001 #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 21406
diff changeset
  1327
    "Modified (comment): / 22-02-2017 / 16:54:08 / cg"
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1328
! !
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1329
7257
b9f0fb923c72 method category rename
Claus Gittinger <cg@exept.de>
parents: 7192
diff changeset
  1330
!ExternalBytes methodsFor:'private-accessing'!
6458
c89a6c9b041c checkin from browser
Stefan Vogel <sv@exept.de>
parents: 6457
diff changeset
  1331
9388
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1332
invalidateReference
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1333
    "clear the start address and size"
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1334
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1335
%{  /* NOCONTEXT */
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1336
    __INST(address_) = nil;
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1337
    __INST(size) = nil;
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1338
%}
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1339
!
84f25350bb00 *** empty log message ***
ca
parents: 9345
diff changeset
  1340
21406
c8b4c0f96850 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21378
diff changeset
  1341
setAddress:aNumberOrExternalAddress size:sz
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1342
    "set the start address and size"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1343
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1344
%{  /* NOCONTEXT */
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1345
    if (__INST(address_) == nil) {
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1346
	if (aNumberOrExternalAddress == nil) {
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1347
	    __INST(address_) = nil;
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1348
	} else {
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1349
	    if (__isSmallInteger(aNumberOrExternalAddress)) {
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1350
		__INST(address_) = (OBJ) __intVal(aNumberOrExternalAddress);
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1351
	    } else if(__isInteger(aNumberOrExternalAddress)) {
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1352
		__INST(address_) = (OBJ) __unsignedLongIntVal(aNumberOrExternalAddress);
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1353
	    } else if(__isExternalAddressLike(aNumberOrExternalAddress)) {
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1354
		__INST(address_) = __externalAddressVal(aNumberOrExternalAddress);
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1355
	    }
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1356
	}
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1357
	__INST(size) = sz;
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1358
	RETURN (self);
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1359
    }
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1360
%}.
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1361
    ^ self error:'cannot change address'
21406
c8b4c0f96850 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21378
diff changeset
  1362
c8b4c0f96850 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21378
diff changeset
  1363
    "Modified: / 12-02-2017 / 16:22:47 / cg"
8288
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1364
!
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1365
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1366
setSize:sz
8288
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1367
    "set the size - warning: dangerous if wrong"
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1368
0bc2cce47912 +setSize
werner
parents: 7855
diff changeset
  1369
    size := sz
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1370
! !
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1371
7257
b9f0fb923c72 method category rename
Claus Gittinger <cg@exept.de>
parents: 7192
diff changeset
  1372
!ExternalBytes methodsFor:'private-allocation'!
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1373
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1374
allocateBytes:numberOfBytes
11854
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1375
    "allocate (malloc) numberOfBytes; if doClear is true, the allocated memory is cleared.
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1376
     Fail if already allocated.
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1377
     Raise MallocFailure if malloc fails to allocate enough memory"
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1378
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1379
    ^ self allocateBytes:numberOfBytes clear:true
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1380
!
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1381
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1382
allocateBytes:numberOfBytes clear:doClear
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1383
    "allocate (malloc) numberOfBytes; if doClear is true, the allocated memory is cleared.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1384
     Fail if already allocated.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1385
     Raise MallocFailure if malloc fails to allocate enough memory"
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1386
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1387
    |mallocFailure|
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1388
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1389
%{
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1390
    /*
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1391
     * Fail if already allocated
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1392
     */
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1393
    if (__INST(address_) == nil && __isSmallInteger(numberOfBytes)) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1394
	INT nBytes = __smallIntegerVal(numberOfBytes);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1395
	if (nBytes > 0) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1396
	    char *space = __stx_malloc(nBytes);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1397
	    if (space) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1398
		if (doClear == true) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1399
		    bzero(space, nBytes);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1400
		}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1401
		__INST(address_) = (OBJ)space;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1402
		__INST(size) = numberOfBytes;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1403
		RETURN(self);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1404
	    } else {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1405
		mallocFailure = true;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1406
	    }
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1407
	}
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1408
    }
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1409
%}.
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1410
    mallocFailure == true ifTrue:[
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1411
	^ MallocFailure raiseRequestWith:numberOfBytes.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1412
    ] ifFalse:[
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1413
	self primitiveFailed.
6444
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1414
    ].
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1415
! !
04becccdbd70 Optimize instance creation
Stefan Vogel <sv@exept.de>
parents: 6443
diff changeset
  1416
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1417
!ExternalBytes methodsFor:'queries'!
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1418
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1419
basicSize
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1420
    "we do not know how many bytes are valid"
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1421
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1422
    size isNil ifTrue:[^ 0].
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1423
    ^ size
6443
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1424
!
84673f4549ce New method #from:to:
Stefan Vogel <sv@exept.de>
parents: 6440
diff changeset
  1425
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1426
containsNon7BitAscii
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1427
    "return true, if any byte in the receiver has the 7th bit on.
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1428
     This my look as a too specific operation to be put here,
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1429
     put it is very helpful for UTF8 string reading (Java class reader),
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1430
     to quickly determine, if UTF8 decoding is needed or not.
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1431
     As most strings in a class file are in fact only containing 7bit ascii,
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1432
     this should speedup class file reading considerably"
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1433
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1434
%{  /* NOCONTEXT */
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1435
    unsigned char *cp = (unsigned char *)(__INST(address_));
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1436
    unsigned int size = __intVal(__INST(size));
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1437
    unsigned char *endP;
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1438
19328
c107ddd38c90 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 19169
diff changeset
  1439
    if (cp == NULL || size == 0) {
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1440
	RETURN(false);
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1441
    }
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1442
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1443
    endP = cp + size;
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1444
#if __POINTER_SIZE__ == 8
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1445
    while (cp+8 < endP) {
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1446
	if ( ((unsigned INT *)cp)[0] & 0x8080808080808080) RETURN( true );
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1447
	cp += 8;
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1448
    }
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1449
#endif
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1450
    while (cp+4 < endP) {
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1451
	if ( ((unsigned int *)cp)[0] & 0x80808080) RETURN( true );
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1452
	cp += 4;
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1453
    }
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1454
    while (cp < endP) {
18653
51b75b45b563 compiler warning
Claus Gittinger <cg@exept.de>
parents: 18607
diff changeset
  1455
	if (*cp++ & 0x80) RETURN( true );
18607
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1456
    }
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1457
    RETURN ( false );
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1458
%}
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1459
.
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1460
    ^ self contains:[:b | b bitTest:16r80].
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1461
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1462
    "
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1463
     #[1 2 3 1 2 3 1 2 127 ] asExternalBytes containsNon7BitAscii
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1464
     #[1 2 3 1 2 3 1 2 250 251 250 251 255] asExternalBytes containsNon7BitAscii
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1465
    "
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1466
!
90941e1c74c8 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 18583
diff changeset
  1467
22159
c8f19ee3f888 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22110
diff changeset
  1468
isNull
c8f19ee3f888 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22110
diff changeset
  1469
    ^ self address == 0
c8f19ee3f888 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22110
diff changeset
  1470
c8f19ee3f888 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22110
diff changeset
  1471
    "Created: / 03-08-2017 / 15:12:32 / cg"
c8f19ee3f888 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22110
diff changeset
  1472
!
c8f19ee3f888 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 22110
diff changeset
  1473
9389
709a29f850a1 isValid
ca
parents: 9388
diff changeset
  1474
isValid
19495
885347ee3b18 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19422
diff changeset
  1475
    "true if I have an address"
9389
709a29f850a1 isValid
ca
parents: 9388
diff changeset
  1476
709a29f850a1 isValid
ca
parents: 9388
diff changeset
  1477
%{  /* NOCONTEXT */
19495
885347ee3b18 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 19422
diff changeset
  1478
    RETURN ((__INST(address_) == 0) ? false : true );
9389
709a29f850a1 isValid
ca
parents: 9388
diff changeset
  1479
%}
709a29f850a1 isValid
ca
parents: 9388
diff changeset
  1480
!
709a29f850a1 isValid
ca
parents: 9388
diff changeset
  1481
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1482
species
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1483
    "when copying, or concatenating, return instances of this class"
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1484
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1485
    ^ ByteArray
5557
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1486
! !
f5f8d236027c category change
Claus Gittinger <cg@exept.de>
parents: 5399
diff changeset
  1487
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1488
!ExternalBytes methodsFor:'registration'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1489
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1490
forgetMemory
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1491
    "forget the underlying memory - i.e. it will NOT be freed by me,
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1492
     and actually no reference to the underlying memory is kept.
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1493
     Warning:
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1494
	 Unless freed by someone else (typically a C-program/client),
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1495
	 this leads to a memory leak.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1496
	 Use this only, if memory which was allocated by me
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1497
	 is given to a C-program which frees the memory."
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1498
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1499
    Lobby unregister:self.      "/ prevents finalization
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1500
    self unprotectFromGC.       "/ no longer remembered
22335
6cef913b016d #DOCUMENTATION by mawalch
mawalch
parents: 22224
diff changeset
  1501
    self setAddress:0 size:0.   "/ no longer accessible
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1502
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1503
    "Created: / 26-07-2017 / 11:20:41 / cg"
22335
6cef913b016d #DOCUMENTATION by mawalch
mawalch
parents: 22224
diff changeset
  1504
    "Modified (comment): / 09-11-2017 / 09:13:34 / mawalch"
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1505
!
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1506
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1507
protectFromGC
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1508
    "enter a reference to the receiver into the AllocatedInstances
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1509
     class variable - this prevents it from ever being finalized by
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1510
     the garbage collector, thus protecting the underlying memory."
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1511
10137
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1512
    |wasBlocked|
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1513
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1514
11854
7fefd527563d *** empty log message ***
sr
parents: 11739
diff changeset
  1515
    "using a Semaphore can cause a deadlock, since unprotectFromGC may be called by
10137
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1516
     a finalization method"
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1517
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1518
    wasBlocked := OperatingSystem blockInterrupts.
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1519
    "/    AccessLock critical:[
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1520
	AllocatedInstances isNil ifTrue:[
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1521
	    AllocatedInstances := IdentitySet new
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1522
	].
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1523
	AllocatedInstances add:self.
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1524
    "/    ]
10137
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1525
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1526
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1527
    "Modified (format): / 26-07-2017 / 11:21:46 / cg"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1528
!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1529
8908
68017b13590b 64bit cleanup
Claus Gittinger <cg@exept.de>
parents: 8901
diff changeset
  1530
unprotectFromGC
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1531
    "remove the receiver from the AllocatedInstances
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1532
     class variable - if there is no other reference to the receiver,
15398
4c73c49852d6 class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 15262
diff changeset
  1533
     and this was ever allocated by me (i.e. not by the outside world),
6459
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1534
     the next garbage collect will finalize the receiver and the underlying
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1535
     memory be freed."
e2ebcff99af2 *** empty log message ***
Stefan Vogel <sv@exept.de>
parents: 6458
diff changeset
  1536
10137
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1537
    |wasBlocked|
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1538
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1539
    "using a Semaphore can cause a deadlock, since protectFromGC may be interrupted by me
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1540
     being called by a finalization method"
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1541
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1542
    wasBlocked := OperatingSystem blockInterrupts.
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1543
    "/ AccessLock critical:[
24043
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1544
	AllocatedInstances notNil ifTrue:[
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1545
	    AllocatedInstances remove:self ifAbsent:nil.
b37704e992e1 compiler warnings
Claus Gittinger <cg@exept.de>
parents: 23932
diff changeset
  1546
	].
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1547
    "/ ]
10137
7bfdc51fcb65 Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents: 9895
diff changeset
  1548
    wasBlocked ifFalse:[OperatingSystem unblockInterrupts].
22110
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1549
f91906125b39 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21548
diff changeset
  1550
    "Modified (format): / 26-07-2017 / 11:21:59 / cg"
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1551
! !
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1552
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1553
!ExternalBytes methodsFor:'resizing'!
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1554
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1555
grow:numberOfBytes
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1556
    "reallocate (realloc) numberOfBytes.
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1557
     Raise MallocFailure if realloc fails to allocate enough memory"
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1558
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1559
    |mallocStatus|
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1560
16705
2f799eb4276d class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 15398
diff changeset
  1561
    size == numberOfBytes ifTrue:[^ self].
2f799eb4276d class: ExternalBytes
Claus Gittinger <cg@exept.de>
parents: 15398
diff changeset
  1562
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1563
%{
19376
5dc7266efb72 Cleanup for 64bit.
Stefan Vogel <sv@exept.de>
parents: 19328
diff changeset
  1564
    if (__isSmallInteger(numberOfBytes)) {
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1565
	INT nBytes = __smallIntegerVal(numberOfBytes);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1566
	if (nBytes > 0) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1567
	    char *space;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1568
	    char *prevSpace = (char *)__INST(address_);
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1569
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1570
	    if (prevSpace == (char *)nil)
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1571
		prevSpace = 0;  /* allocate from scratch */
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1572
	    space = __stx_realloc(prevSpace, nBytes);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1573
	    if (space) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1574
		__INST(address_) = (OBJ)space;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1575
		__INST(size) = numberOfBytes;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1576
		if (space == prevSpace) {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1577
		    /* same address, no re-registration */
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1578
		    RETURN(self);
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1579
		}
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1580
		mallocStatus = true;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1581
	    } else {
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1582
		mallocStatus = false;
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1583
	    }
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1584
	}
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1585
    }
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1586
%}.
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1587
    mallocStatus == true ifTrue:[
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1588
	Lobby registerChange:self.
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1589
    ] ifFalse:[mallocStatus == false ifTrue:[
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1590
	^ MallocFailure raiseRequestWith:numberOfBytes.
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1591
    ] ifFalse:[
19860
324edacff5cc unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents: 19512
diff changeset
  1592
	self primitiveFailed.
6457
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1593
    ]].
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1594
! !
123f5c82fcb7 Fix mallocTrace
Stefan Vogel <sv@exept.de>
parents: 6446
diff changeset
  1595
14080
051ed8e51d65 added: #isExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14076
diff changeset
  1596
!ExternalBytes methodsFor:'testing'!
051ed8e51d65 added: #isExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14076
diff changeset
  1597
051ed8e51d65 added: #isExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14076
diff changeset
  1598
isExternalBytes
051ed8e51d65 added: #isExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14076
diff changeset
  1599
    ^ true
051ed8e51d65 added: #isExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14076
diff changeset
  1600
! !
051ed8e51d65 added: #isExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14076
diff changeset
  1601
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1602
!ExternalBytes class methodsFor:'documentation'!
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1603
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1604
version
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
  1605
    ^ '$Header$'
12594
d7267a747c2f changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 11854
diff changeset
  1606
!
d7267a747c2f changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 11854
diff changeset
  1607
d7267a747c2f changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents: 11854
diff changeset
  1608
version_CVS
18580
5bdec8c6f5f2 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 17627
diff changeset
  1609
    ^ '$Header$'
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1610
! !
7184
1191a63dcd26 +register
penk
parents: 6503
diff changeset
  1611
14749
64fd0e91d2d7 class: ExternalBytes
Stefan Vogel <sv@exept.de>
parents: 14632
diff changeset
  1612
848
76a83f34c26a moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff changeset
  1613
ExternalBytes initialize!