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