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