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