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