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