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