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