author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 11739 | d04e05026d39 |
child 17728 | bbc5fa73dfab |
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 |
|
11076
b35229a4126f
you shalt not return void values.
Stefan Vogel <sv@exept.de>
parents:
10925
diff
changeset
|
161 |
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:[ |
|
10178 | 388 |
Lobby := Registry new. |
6459 | 389 |
] |
390 |
! ! |
|
391 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
392 |
!ExternalBytes class methodsFor:'instance creation'! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
393 |
|
6459 | 394 |
address:aNumber |
395 |
"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
|
396 |
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
|
397 |
therefore byte accesses will NOT be checked for valid index. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
398 |
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
|
399 |
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
|
400 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
401 |
DANGER ALERT: this method allows very bad things to be done to the |
6242 | 402 |
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
|
403 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
404 |
^ self basicNew setAddress:aNumber size:nil |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
405 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
406 |
|
6459 | 407 |
address:aNumber size:size |
408 |
"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
|
409 |
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
|
410 |
which allows byte accesses to be checked for valid index. |
8908 | 411 |
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
|
412 |
(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
|
413 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
414 |
DANGER ALERT: this method allows very bad things to be done to the |
6242 | 415 |
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
|
416 |
|
8908 | 417 |
^ self basicNew setAddress:aNumber size:size |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
418 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
419 |
|
6459 | 420 |
new:numberOfBytes |
421 |
"allocate some memory usable for data; |
|
422 |
the memory is not controlled by the garbage collector. |
|
423 |
Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails). |
|
424 |
||
8908 | 425 |
Use this, if you have to pass a block of bytes to some |
6459 | 426 |
external destination (such as a C function) which does not copy the |
427 |
data, but instead keeps a reference to it. For example, many functions |
|
428 |
which expect strings simply keep a ref to the passed string - for those, |
|
429 |
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
|
430 |
may change their address. |
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 |
DANGER ALERT: the memory is NOT automatically freed until it is either |
6445 | 433 |
MANUALLY freed (see #free) or the returned externalBytes object |
434 |
is unprotected or the classes releaseAllMemory method is called." |
|
848
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 |
|newInst| |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
437 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
438 |
newInst := self unprotectedNew:numberOfBytes. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
439 |
newInst protectFromGC. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
440 |
^ newInst |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
441 |
|
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 |
|bytes| |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
444 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
445 |
bytes := ExternalBytes new:100. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
446 |
bytes wordAt:1 put:1. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
447 |
bytes doubleWordAt:3 put:16r12345678. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
448 |
bytes inspect |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
449 |
" |
6459 | 450 |
! |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
451 |
|
10925 | 452 |
newNullTerminatedFromString:aString |
453 |
^ ((self new:aString size+1) |
|
454 |
replaceBytesFrom:1 to:aString size with:aString startingAt:1) |
|
455 |
at:aString size+1 put:0; |
|
456 |
yourself |
|
457 |
! |
|
458 |
||
6459 | 459 |
unprotectedNew:numberOfBytes |
8908 | 460 |
"allocate some memory usable for data; |
6459 | 461 |
the memory is under the control of the garbage collector. |
462 |
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
|
463 |
|
6459 | 464 |
DANGER ALERT: the memory block as allocated will be automatically freed |
9388 | 465 |
as soon as the reference to the returned externalBytes object |
466 |
is gone (by the next garbage collect). |
|
467 |
If the memory has been passed to a C-function which |
|
468 |
remembers this pointer, bad things may happen ...." |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
469 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
470 |
|newInst| |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
471 |
|
6444 | 472 |
newInst := self basicNew allocateBytes:numberOfBytes. |
9388 | 473 |
newInst registerForFinalization. |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
474 |
^ newInst |
6444 | 475 |
|
476 |
" |
|
477 |
|bytes| |
|
478 |
||
479 |
bytes := ExternalBytes unprotectedNew:100. |
|
480 |
bytes wordAt:1 put:1. |
|
481 |
bytes doubleWordAt:3 put:16r12345678. |
|
482 |
bytes inspect |
|
483 |
" |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
484 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
485 |
|
6459 | 486 |
!ExternalBytes class methodsFor:'instance release'! |
487 |
||
488 |
releaseAllMemory |
|
489 |
AllocatedInstances := nil |
|
490 |
" |
|
491 |
... the next GC will get 'em |
|
492 |
" |
|
493 |
! ! |
|
494 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
495 |
!ExternalBytes class methodsFor:'malloc debug'! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
496 |
|
6459 | 497 |
dumpMallocChunks |
498 |
%{ /* NOCONTEXT */ |
|
499 |
struct mallocList *entry; |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
500 |
|
6459 | 501 |
for (entry = mallocList; entry; entry=entry->next) { |
8908 | 502 |
#ifdef __NEED_L_MODIFIER_IN_PRINTF__ |
6459 | 503 |
printf(" %016lx (%d)\n", entry->chunk, entry->size); |
504 |
#else |
|
505 |
printf(" %08x (%d)\n", entry->chunk, entry->size); |
|
506 |
#endif |
|
507 |
} |
|
508 |
%} |
|
3321 | 509 |
" |
6459 | 510 |
self dumpMallocChunks |
3321 | 511 |
" |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
512 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
513 |
|
6459 | 514 |
freeAllMallocChunks |
515 |
"free all stx_malloc'd memory. Be careful, this does no validation at all; |
|
516 |
It simply walks through all chunks and frees them unconditionally. |
|
517 |
This may be helpful during debugging memory-leaks, to release memory which |
|
518 |
was not correctly freed by C-code. Howeve, only memory which was allocated |
|
519 |
by __stx_malloc() is freed here - so you better compile your primitive code with |
|
520 |
malloc redefined to stx_malloc. |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
521 |
Also, mallocDebug has to be on to do this." |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
522 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
523 |
"first free my own memory ..." |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
524 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
525 |
self releaseAllMemory. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
526 |
ObjectMemory garbageCollect. |
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 |
struct mallocList *entry; |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
529 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
530 |
while ((entry = mallocList) != (struct mallocList *)0) { |
6242 | 531 |
if (@global(TraceMalloc) == true ) { |
8908 | 532 |
#ifdef __NEED_L_MODIFIER_IN_PRINTF__ |
6242 | 533 |
printf("EXTBYTES [info]: **** forced free of %016lx (%d)\n", entry->chunk, entry->size); |
2779 | 534 |
#else |
6242 | 535 |
printf("EXTBYTES [info]: **** forced free of %08x (%d)\n", entry->chunk, entry->size); |
2779 | 536 |
#endif |
6242 | 537 |
} |
538 |
__stx_free(entry->chunk); |
|
848
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 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
542 |
|
6459 | 543 |
mallocDebug:aBoolean |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
544 |
DebugMalloc := aBoolean |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
545 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
546 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
547 |
ExternalBytes mallocDebug:true |
8908 | 548 |
ExternalBytes mallocDebug:false |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
549 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
550 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
551 |
|
6459 | 552 |
mallocStatistics |
553 |
%{ |
|
554 |
__stx_mallocStatistics(); |
|
555 |
%} |
|
556 |
" |
|
557 |
self mallocStatistics |
|
3486 | 558 |
" |
559 |
! |
|
560 |
||
6459 | 561 |
mallocTrace:aBoolean |
562 |
TraceMalloc := aBoolean |
|
563 |
||
564 |
" |
|
565 |
ExternalBytes mallocTrace:true |
|
566 |
ExternalBytes mallocTrace:false |
|
567 |
" |
|
568 |
! |
|
569 |
||
570 |
numberOfAllocatedChunks |
|
571 |
%{ /* NOCONTEXT */ |
|
8913 | 572 |
RETURN ( __mkSmallInteger(mallocCount) ); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
573 |
%} |
3321 | 574 |
" |
575 |
self numberOfAllocatedChunks |
|
576 |
" |
|
6459 | 577 |
! ! |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
578 |
|
6459 | 579 |
!ExternalBytes class methodsFor:'queries'! |
580 |
||
581 |
isBuiltInClass |
|
582 |
"return true if this class is known by the run-time-system. |
|
1267 | 583 |
Here, true is returned." |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
584 |
|
3553 | 585 |
^ self == ExternalBytes |
1267 | 586 |
|
3553 | 587 |
"Modified: / 11.6.1998 / 17:12:51 / cg" |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
588 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
589 |
|
6459 | 590 |
sizeofDouble |
591 |
"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
|
592 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
593 |
%{ /* NOCONTEXT */ |
8913 | 594 |
RETURN (__mkSmallInteger( sizeof(double))); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
595 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
596 |
" |
8908 | 597 |
ExternalBytes sizeofDouble |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
598 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
599 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
600 |
|
6459 | 601 |
sizeofFloat |
602 |
"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
|
603 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
604 |
%{ /* NOCONTEXT */ |
8913 | 605 |
RETURN (__mkSmallInteger( sizeof(float))); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
606 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
607 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
608 |
ExternalBytes sizeofFloat |
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 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
611 |
|
6459 | 612 |
sizeofInt |
613 |
"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
|
614 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
615 |
%{ /* NOCONTEXT */ |
8913 | 616 |
RETURN (__mkSmallInteger( sizeof(int))); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
617 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
618 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
619 |
ExternalBytes sizeofInt |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
620 |
" |
6459 | 621 |
! |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
622 |
|
6459 | 623 |
sizeofLong |
624 |
"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
|
625 |
|
6459 | 626 |
%{ /* NOCONTEXT */ |
8913 | 627 |
RETURN (__mkSmallInteger( sizeof(long))); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
628 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
629 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
630 |
ExternalBytes sizeofLong |
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 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
633 |
|
6459 | 634 |
sizeofPointer |
635 |
"return the number of bytes used by the machines native pointer" |
|
636 |
||
637 |
%{ /* NOCONTEXT */ |
|
8913 | 638 |
RETURN (__mkSmallInteger( sizeof(char *))); |
6459 | 639 |
%} |
640 |
" |
|
641 |
ExternalBytes sizeofPointer |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
642 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
643 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
644 |
|
6459 | 645 |
sizeofShort |
646 |
"return the number of bytes used by the machines native short" |
|
647 |
||
648 |
%{ /* NOCONTEXT */ |
|
8913 | 649 |
RETURN (__mkSmallInteger( sizeof(short))); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
650 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
651 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
652 |
ExternalBytes sizeofShort |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
653 |
" |
6459 | 654 |
! ! |
655 |
||
656 |
!ExternalBytes methodsFor:'accessing'! |
|
657 |
||
658 |
address |
|
659 |
"return the start address as an integer" |
|
660 |
||
661 |
%{ /* NOCONTEXT */ |
|
662 |
||
11629 | 663 |
if (__INST(address_) != nil) { |
664 |
unsigned INT addr; |
|
6459 | 665 |
|
11628 | 666 |
addr = (unsigned INT)__INST(address_); |
11627
100e6a95fa3f
address: do not loose the high bit (unsigned int issue)
Claus Gittinger <cg@exept.de>
parents:
11076
diff
changeset
|
667 |
RETURN ( __MKUINT(addr)); |
6459 | 668 |
} |
669 |
%}. |
|
670 |
^ nil |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
671 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
672 |
|
6459 | 673 |
basicAt:index |
674 |
"return the byte at index, anInteger; |
|
675 |
Indices are 1-based, therefore |
|
676 |
this is the byte at (address + index - 1)" |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
677 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
678 |
%{ /* NOCONTEXT */ |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
679 |
|
1822 | 680 |
unsigned char *cp = (unsigned char *)(__INST(address_)); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
681 |
int idx; |
1997 | 682 |
OBJ sz; |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
683 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
684 |
if (cp && __isSmallInteger(index)) { |
6242 | 685 |
idx = __intVal(index); |
686 |
if (idx > 0) { |
|
687 |
if (((sz = __INST(size)) == nil) |
|
688 |
|| (__intVal(sz) >= idx)) { |
|
689 |
cp = cp + idx - 1; |
|
8913 | 690 |
RETURN ( __mkSmallInteger((*cp)) ); |
6242 | 691 |
} |
692 |
} |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
693 |
} |
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 |
(size notNil and:[self address notNil]) ifTrue:[ |
6242 | 696 |
^ self subscriptBoundsError:index |
848
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 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
699 |
invalid index or unallocated |
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 |
self primitiveFailed |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
702 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
703 |
|
6459 | 704 |
basicAt:index put:value |
705 |
"set the byte at index, anInteger to value which must be 0..255. |
|
706 |
Returns value (sigh). |
|
707 |
Indices are 1-based, therefore |
|
708 |
this is the byte at (address + index - 1)" |
|
709 |
||
710 |
%{ /* NOCONTEXT */ |
|
711 |
||
712 |
unsigned char *cp = (unsigned char *)(__INST(address_)); |
|
713 |
int val; |
|
714 |
int idx; |
|
715 |
OBJ sz; |
|
716 |
||
717 |
if (__isSmallInteger(value)) { |
|
718 |
val = __smallIntegerVal(value); |
|
8908 | 719 |
} else if (__isCharacter(value)) { |
6459 | 720 |
val = __smallIntegerVal(_characterVal(value)); |
721 |
} else |
|
722 |
goto badArg; |
|
723 |
||
724 |
if (cp && __isSmallInteger(index)) { |
|
725 |
idx = __intVal(index); |
|
726 |
if (idx > 0) { |
|
6445 | 727 |
if (((sz = __INST(size)) == nil) |
728 |
|| (__intVal(sz) >= idx)) { |
|
729 |
if ((val & ~0xFF) == 0) /* i.e. (val >= 0) && (val <= 255) */ { |
|
730 |
cp[idx-1] = val; |
|
731 |
RETURN ( value ); |
|
732 |
} |
|
733 |
} |
|
734 |
} |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
735 |
} |
6302
32ede83e77e5
allow for character arg in #at:put:
Claus Gittinger <cg@exept.de>
parents:
6242
diff
changeset
|
736 |
|
32ede83e77e5
allow for character arg in #at:put:
Claus Gittinger <cg@exept.de>
parents:
6242
diff
changeset
|
737 |
badArg: ; |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
738 |
%}. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
739 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
740 |
(size notNil and:[self address notNil]) ifTrue:[ |
6445 | 741 |
(index between:1 and:size) ifTrue:[ |
742 |
^ ElementOutOfBoundsSignal raise |
|
743 |
]. |
|
744 |
^ self subscriptBoundsError:index |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
745 |
]. |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
746 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
747 |
invalid index, invalid value or unallocated |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
748 |
" |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
749 |
self primitiveFailed |
1220 | 750 |
|
751 |
"Modified: 19.4.1996 / 11:15:05 / cg" |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
752 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
753 |
|
6459 | 754 |
byteAt:index |
755 |
"return the unsigned byte at index, anInteger. |
|
756 |
Indices are 1-based, therefore |
|
757 |
this is the byte at (address + index - 1)" |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
758 |
|
6459 | 759 |
^ self basicAt:index |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
760 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
761 |
|
6459 | 762 |
byteAt:index put:aByteValuedInteger |
763 |
"set the byte at index. |
|
764 |
Indices are 1-based, therefore |
|
765 |
this is the byte at (address + index - 1)" |
|
766 |
||
767 |
^ self basicAt:index put:aByteValuedInteger |
|
768 |
! |
|
769 |
||
770 |
instVarAt:index |
|
771 |
"redefined to suppress direct access to my address, which is a non-object" |
|
772 |
||
773 |
index == 1 ifTrue:[ |
|
774 |
^ self address |
|
6458 | 775 |
]. |
6459 | 776 |
^ super instVarAt:index |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
777 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
778 |
|
11739 | 779 |
!ExternalBytes methodsFor:'compatibility-VW'! |
780 |
||
781 |
copyCStringFromHeap |
|
782 |
|s| |
|
783 |
||
784 |
s := String new:(self indexOf:0)-1. |
|
785 |
1 to:s size do:[:idx | |
|
786 |
s at:idx put:(Character value:(self at:idx)) |
|
787 |
]. |
|
788 |
^ s. |
|
789 |
! ! |
|
790 |
||
6491
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
791 |
!ExternalBytes methodsFor:'converting'! |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
792 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
793 |
asExternalAddress |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
794 |
"return the start address as an external address" |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
795 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
796 |
%{ /* NOCONTEXT */ |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
797 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
798 |
RETURN(__MKEXTERNALADDRESS(__INST(address_))); |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
799 |
%}. |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
800 |
! |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
801 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
802 |
asExternalBytes |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
803 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
804 |
^ self |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
805 |
! ! |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
806 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
807 |
!ExternalBytes methodsFor:'filling & replacing'! |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
808 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
809 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
810 |
"replace elements from another collection, which must be a ByteArray- |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
811 |
like collection. |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
812 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
813 |
Notice: This operation modifies the receiver, NOT a copy; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
814 |
therefore the change may affect all others referencing the receiver." |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
815 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
816 |
%{ /* NOCONTEXT */ |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
817 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
818 |
int nIndex, repNIndex; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
819 |
int startIndex, stopIndex; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
820 |
REGISTER unsigned char *src; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
821 |
REGISTER int repStartIndex; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
822 |
int repStopIndex, count; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
823 |
REGISTER unsigned char *dst; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
824 |
OBJ cls; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
825 |
|
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
826 |
if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection)) |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
827 |
&& __bothSmallInteger(start, stop) |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
828 |
&& __isSmallInteger(repStart)) { |
8908 | 829 |
startIndex = __intVal(start) - 1; |
830 |
if (startIndex >= 0) { |
|
831 |
dst = (unsigned char *)__INST(address_) + startIndex; |
|
832 |
nIndex = __smallIntegerVal(__INST(size)); |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
833 |
|
8908 | 834 |
stopIndex = __intVal(stop) - 1; |
835 |
count = stopIndex - startIndex + 1; |
|
836 |
if (count == 0) { |
|
837 |
RETURN ( self ); |
|
838 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
839 |
|
8908 | 840 |
if ((count > 0) && (stopIndex < nIndex)) { |
841 |
repStartIndex = __intVal(repStart) - 1; |
|
842 |
if (repStartIndex >= 0) { |
|
843 |
if (__isExternalBytesLike(aCollection)) { |
|
844 |
OBJ sz; |
|
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
845 |
|
8908 | 846 |
src = __externalBytesVal(aCollection); |
847 |
if (src == 0) goto fallBack; |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
848 |
|
8908 | 849 |
sz = __externalBytesSize(aCollection); |
850 |
if (__isSmallInteger(sz)) { |
|
851 |
repNIndex = __smallIntegerVal(sz); |
|
852 |
} else { |
|
853 |
repNIndex = -1; /* unknown */ |
|
854 |
} |
|
855 |
src = src + repStartIndex; |
|
856 |
} else { |
|
857 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
858 |
src = __byteArrayVal(aCollection) + repStartIndex; |
|
859 |
if ((cls = __qClass(aCollection)) != @global(ByteArray)) { |
|
860 |
int nInst; |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
861 |
|
8908 | 862 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
863 |
src += nInst; |
|
864 |
repNIndex -= nInst; |
|
865 |
} |
|
866 |
} |
|
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
867 |
|
8908 | 868 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
869 |
if (repStopIndex < repNIndex) { |
|
870 |
if (aCollection == self) { |
|
871 |
/* take care of overlapping copy */ |
|
872 |
if (src < dst) { |
|
873 |
/* must do a reverse copy */ |
|
874 |
src += count; |
|
875 |
dst += count; |
|
876 |
while (count-- > 0) { |
|
877 |
*--dst = *--src; |
|
878 |
} |
|
879 |
RETURN ( self ); |
|
880 |
} |
|
881 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
882 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
883 |
#ifdef memcpy4 |
8908 | 884 |
if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) { |
885 |
int nW; |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
886 |
|
8908 | 887 |
/* copy unaligned part */ |
888 |
while (count && ((unsigned INT)src & 3)) { |
|
889 |
*dst++ = *src++; |
|
890 |
count--; |
|
891 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
892 |
|
8908 | 893 |
if (count > 0) { |
894 |
/* copy aligned part */ |
|
895 |
nW = count >> 2; |
|
896 |
memcpy4(dst, src, nW); |
|
897 |
if ((count = count & 3) != 0) { |
|
898 |
/* copy any remaining part */ |
|
899 |
src += (nW<<2); |
|
900 |
dst += (nW<<2); |
|
901 |
while (count--) { |
|
902 |
*dst++ = *src++; |
|
903 |
} |
|
904 |
} |
|
905 |
} |
|
906 |
RETURN ( self ); |
|
907 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
908 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8288
diff
changeset
|
909 |
# if __POINTER_SIZE__ == 8 |
8908 | 910 |
if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) { |
911 |
/* copy unaligned part */ |
|
912 |
while (count && ((unsigned INT)src & 7)) { |
|
913 |
*dst++ = *src++; |
|
914 |
count--; |
|
915 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
916 |
|
8908 | 917 |
/* copy aligned part */ |
918 |
while (count >= 8) { |
|
919 |
((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0]; |
|
920 |
dst += 8; |
|
921 |
src += 8; |
|
922 |
count -= 8; |
|
923 |
} |
|
924 |
while (count--) { |
|
925 |
*dst++ = *src++; |
|
926 |
} |
|
927 |
RETURN ( self ); |
|
928 |
} |
|
929 |
# endif /* 64bit */ |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
930 |
#endif /* memcpy4 */ |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
931 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
932 |
#ifdef FAST_MEMCPY |
8908 | 933 |
memcpy(dst, src, count); |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
934 |
#else |
8919 | 935 |
# ifdef __UNROLL_LOOPS__ |
8908 | 936 |
while (count >= 8) { |
937 |
dst[0] = src[0]; dst[1] = src[1]; |
|
938 |
dst[2] = src[2]; dst[3] = src[3]; |
|
939 |
dst[4] = src[4]; dst[5] = src[5]; |
|
940 |
dst[6] = src[6]; dst[7] = src[7]; |
|
941 |
dst += 8; src += 8; |
|
942 |
count -= 8; |
|
943 |
} |
|
8919 | 944 |
# endif /* __UNROLL_LOOPS__ */ |
8908 | 945 |
while (count-- > 0) { |
946 |
*dst++ = *src++; |
|
947 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
948 |
#endif |
8908 | 949 |
RETURN ( self ); |
950 |
} |
|
951 |
} |
|
952 |
} |
|
953 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
954 |
} |
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
955 |
fallBack: ; |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
956 |
%}. |
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 |
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
|
959 |
" |
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
960 |
^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
961 |
|
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:1 to:8 |
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:3 to:10 |
970 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
971 |
startingAt:4 |
|
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 |
(ExternalBytes unprotectedNew:16) |
8908 | 974 |
replaceFrom:3 to:4 |
975 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
976 |
startingAt:1 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
977 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
978 |
(ExternalBytes unprotectedNew:16) |
8908 | 979 |
replaceFrom:0 to:9 |
980 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
981 |
startingAt:1 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
982 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
983 |
(ExternalBytes unprotectedNew:16) |
8908 | 984 |
replaceFrom:1 to:10 |
985 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
986 |
startingAt:0 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
987 |
" |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
988 |
! ! |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
989 |
|
6444 | 990 |
!ExternalBytes methodsFor:'finalization'! |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
991 |
|
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
992 |
executor |
8908 | 993 |
"redefined to return a lightweight copy |
7184 | 994 |
- 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
|
995 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
996 |
%{ /* NOCONTEXT */ |
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
997 |
|
7386
0996b933cbf1
Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents:
7257
diff
changeset
|
998 |
OBJ theCopy, cls; |
7184 | 999 |
|
7855
27f64d1beaa1
Have to protect self, otherwise bad things happen in the VM.
Stefan Vogel <sv@exept.de>
parents:
7386
diff
changeset
|
1000 |
__PROTECT__(self); |
7184 | 1001 |
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
|
1002 |
__UNPROTECT__(self); |
7386
0996b933cbf1
Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents:
7257
diff
changeset
|
1003 |
__InstPtr(theCopy)->o_class = cls = __InstPtr(self)->o_class; |
0996b933cbf1
Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents:
7257
diff
changeset
|
1004 |
__STORE(theCopy, cls); |
7184 | 1005 |
RETURN (theCopy); |
6491
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
1006 |
%} |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
1007 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
1008 |
" |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
1009 |
(ExternalBytes unprotectedNew:10) executor |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
1010 |
" |
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
1011 |
! |
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
1012 |
|
6462
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1013 |
finalizationLobby |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1014 |
"answer the registry used for finalization. |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1015 |
ExternalBytes have their own Registry" |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1016 |
|
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1017 |
^ Lobby |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1018 |
! |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
1019 |
|
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
1020 |
finalize |
2779 | 1021 |
"some ExternalBytes object was finalized; |
1022 |
free the associated heap memory with it" |
|
1023 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1024 |
%{ /* NOCONTEXT */ |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1025 |
|
6457 | 1026 |
char *mem = (char *)(__INST(address_)); |
1027 |
if (mem && (OBJ)mem != nil) { |
|
6459 | 1028 |
__stx_free(mem); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1029 |
} |
1133 | 1030 |
__INST(address_) = __INST(size) = nil; |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1031 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1032 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1033 |
|
6444 | 1034 |
!ExternalBytes methodsFor:'freeing'! |
1035 |
||
6459 | 1036 |
free |
1037 |
"free a previously allocated piece of memory - be very careful, there |
|
1038 |
are no checks done here. All dangers you usually have with malloc/free |
|
1039 |
are present here ..." |
|
1040 |
||
1041 |
"at least, we check for double freeing the same chunk" |
|
1042 |
self address isNil ifTrue:[ |
|
1043 |
self error:'freeing memory twice'. |
|
1044 |
^ self |
|
1045 |
]. |
|
1046 |
Lobby unregister:self. |
|
1047 |
self finalize. "/ does what we need here .. |
|
1048 |
self unprotectFromGC |
|
7184 | 1049 |
! |
1050 |
||
1051 |
register |
|
1052 |
"register the receiver to be automatically finalized by the GC" |
|
1053 |
||
1054 |
Lobby register:self. |
|
6444 | 1055 |
! ! |
1056 |
||
6443 | 1057 |
!ExternalBytes methodsFor:'pointer arithmethic'! |
1058 |
||
6502
87f587d1542e
rename #from:to to #referenceToBytesFrom:to
Stefan Vogel <sv@exept.de>
parents:
6491
diff
changeset
|
1059 |
referenceToBytesFrom:start to:stop |
6459 | 1060 |
"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
|
1061 |
BE CAREFUL: after the receiver has been freed, the new ExternalBytes |
6459 | 1062 |
contents is undefined" |
1063 |
||
8908 | 1064 |
%{ /* NOCONTEXT */ |
6459 | 1065 |
|
6471 | 1066 |
char *addr; |
6459 | 1067 |
int size; |
1068 |
int __start, __stop; |
|
1069 |
||
1070 |
if (__bothSmallInteger(start, stop) && __INST(address_) != nil) { |
|
6503 | 1071 |
__start = __smallIntegerVal(start); |
1072 |
__stop = __smallIntegerVal(stop); |
|
1073 |
if (__start > 0 && __start <= __stop && __stop <= __smallIntegerVal(__INST(size))) { |
|
1074 |
addr = (char *)(__INST(address_)) + (__start - 1); |
|
1075 |
size = __stop - __start + 1; |
|
1076 |
RETURN( __MKEXTERNALBYTES_N(addr, size) ); |
|
1077 |
} |
|
6459 | 1078 |
} |
1079 |
%}. |
|
1080 |
^ self primitiveFailed |
|
6458 | 1081 |
! ! |
6443 | 1082 |
|
6458 | 1083 |
!ExternalBytes methodsFor:'printing & storing'! |
6443 | 1084 |
|
6459 | 1085 |
displayString |
1086 |
"return a printed representation of the receiver for displaying" |
|
1087 |
||
1088 |
|addr addrString| |
|
6443 | 1089 |
|
6459 | 1090 |
(addr := self address) isNil ifTrue:[ |
9345 | 1091 |
addrString := '[free]' |
6459 | 1092 |
] ifFalse:[ |
9345 | 1093 |
size notNil ifTrue:[ |
11739 | 1094 |
addrString := '[sz:', size printString, ' ' |
9345 | 1095 |
] ifFalse:[ |
11739 | 1096 |
addrString := '[' |
9345 | 1097 |
]. |
11739 | 1098 |
addrString := addrString , 'at:' , (addr printStringRadix:16), ']' |
6459 | 1099 |
]. |
1100 |
^ self class name , addrString |
|
1101 |
||
1102 |
"Modified: / 24.2.2000 / 19:02:19 / cg" |
|
6458 | 1103 |
! ! |
1104 |
||
7257 | 1105 |
!ExternalBytes methodsFor:'private-accessing'! |
6458 | 1106 |
|
9388 | 1107 |
invalidateReference |
1108 |
"clear the start address and size" |
|
1109 |
||
1110 |
%{ /* NOCONTEXT */ |
|
1111 |
__INST(address_) = nil; |
|
1112 |
__INST(size) = nil; |
|
1113 |
%} |
|
1114 |
! |
|
1115 |
||
8908 | 1116 |
setAddress:aNumber size:sz |
6459 | 1117 |
"set the start address and size" |
1118 |
||
1119 |
%{ /* NOCONTEXT */ |
|
1120 |
if (__INST(address_) == nil) { |
|
1121 |
if (aNumber == nil) { |
|
1122 |
__INST(address_) = nil; |
|
1123 |
} else { |
|
1124 |
if (__isSmallInteger(aNumber)) { |
|
1125 |
__INST(address_) = (OBJ) __intVal(aNumber); |
|
1126 |
} else if(__isInteger(aNumber)) { |
|
1127 |
__INST(address_) = (OBJ) __unsignedLongIntVal(aNumber); |
|
1128 |
} else if(__isExternalAddress(aNumber)) { |
|
1129 |
__INST(address_) = __externalAddressVal(aNumber); |
|
1130 |
} |
|
6445 | 1131 |
} |
6459 | 1132 |
__INST(size) = sz; |
1133 |
RETURN (self); |
|
6443 | 1134 |
} |
1135 |
%}. |
|
6459 | 1136 |
^ self error:'cannot change address' |
8288 | 1137 |
! |
1138 |
||
8908 | 1139 |
setSize:sz |
8288 | 1140 |
"set the size - warning: dangerous if wrong" |
1141 |
||
1142 |
size := sz |
|
6443 | 1143 |
! ! |
1144 |
||
7257 | 1145 |
!ExternalBytes methodsFor:'private-allocation'! |
6444 | 1146 |
|
1147 |
allocateBytes:numberOfBytes |
|
1148 |
"allocate (malloc) numberOfBytes. |
|
1149 |
Fail if already allocated. |
|
1150 |
Raise MallocFailure if malloc fails to allocate enough memory" |
|
1151 |
||
1152 |
|mallocFailure| |
|
1153 |
||
6457 | 1154 |
%{ |
6444 | 1155 |
char *space; |
1156 |
unsigned int nBytes; |
|
1157 |
char *__stx_malloc(); |
|
1158 |
||
1159 |
/* |
|
8908 | 1160 |
* Fail if already allocated |
6444 | 1161 |
*/ |
1162 |
||
1163 |
if (__INST(address_) == nil && __isSmallInteger(numberOfBytes)) { |
|
6503 | 1164 |
nBytes = __smallIntegerVal(numberOfBytes); |
1165 |
if (nBytes > 0) { |
|
1166 |
space = __stx_malloc(nBytes); |
|
1167 |
if (space) { |
|
1168 |
__INST(address_) = (OBJ)space; |
|
1169 |
__INST(size) = numberOfBytes; |
|
1170 |
RETURN(self); |
|
1171 |
} else { |
|
1172 |
mallocFailure = true; |
|
1173 |
} |
|
1174 |
} |
|
6444 | 1175 |
} |
1176 |
%}. |
|
1177 |
mallocFailure == true ifTrue:[ |
|
6503 | 1178 |
^ MallocFailure raiseRequestWith:numberOfBytes. |
6444 | 1179 |
] ifFalse:[ |
6503 | 1180 |
self primitiveFailed. |
6444 | 1181 |
]. |
1182 |
! ! |
|
1183 |
||
5557 | 1184 |
!ExternalBytes methodsFor:'queries'! |
1185 |
||
6459 | 1186 |
basicSize |
1187 |
"we do not know how many bytes are valid" |
|
5557 | 1188 |
|
6459 | 1189 |
size isNil ifTrue:[^ 0]. |
1190 |
^ size |
|
6443 | 1191 |
! |
1192 |
||
9389 | 1193 |
isValid |
1194 |
"true if I gave an address" |
|
1195 |
||
1196 |
%{ /* NOCONTEXT */ |
|
1197 |
RETURN ((__INST(address_) == nil) ? false : true ); |
|
1198 |
%} |
|
1199 |
! |
|
1200 |
||
6459 | 1201 |
species |
1202 |
"when copying, or concatenating, return instances of this class" |
|
1203 |
||
1204 |
^ ByteArray |
|
5557 | 1205 |
! ! |
1206 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1207 |
!ExternalBytes methodsFor:'registration'! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1208 |
|
8908 | 1209 |
protectFromGC |
6459 | 1210 |
"enter a reference to the receiver into the AllocatedInstances |
1211 |
class variable - this prevents it from ever being finalized by |
|
1212 |
the garbage collector, thus protecting the underlying memory." |
|
1213 |
||
10137
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1214 |
|wasBlocked| |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1215 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1216 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1217 |
"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
|
1218 |
a finalization method" |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1219 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1220 |
wasBlocked := OperatingSystem blockInterrupts. |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1221 |
"/ AccessLock critical:[ |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1222 |
AllocatedInstances isNil ifTrue:[ |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1223 |
AllocatedInstances := IdentitySet new |
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 |
AllocatedInstances add:self. |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1226 |
"/ ] |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1227 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1228 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1229 |
|
8908 | 1230 |
unprotectFromGC |
6459 | 1231 |
"remove the receiver from the AllocatedInstances |
1232 |
class variable - if there is no other reference to the receiver, |
|
1233 |
the next garbage collect will finalize the receiver and the underlying |
|
1234 |
memory be freed." |
|
1235 |
||
10137
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1236 |
|wasBlocked| |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1237 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1238 |
"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
|
1239 |
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
|
1240 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1241 |
wasBlocked := OperatingSystem blockInterrupts. |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1242 |
"/ AccessLock critical:[ |
10138 | 1243 |
AllocatedInstances notNil ifTrue:[ |
1244 |
AllocatedInstances remove:self ifAbsent:nil. |
|
1245 |
]. |
|
10137
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1246 |
"/ ] |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1247 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1248 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1249 |
|
6457 | 1250 |
!ExternalBytes methodsFor:'resizing'! |
1251 |
||
1252 |
grow:numberOfBytes |
|
1253 |
"reallocate (realloc) numberOfBytes. |
|
1254 |
Raise MallocFailure if realloc fails to allocate enough memory" |
|
1255 |
||
1256 |
|mallocStatus| |
|
1257 |
||
1258 |
%{ |
|
1259 |
char *space, *prevSpace; |
|
1260 |
unsigned int nBytes; |
|
1261 |
char *__stx_realloc(); |
|
1262 |
||
1263 |
if (__isSmallInteger(numberOfBytes)) { |
|
6503 | 1264 |
nBytes = __smallIntegerVal(numberOfBytes); |
1265 |
if (nBytes > 0) { |
|
1266 |
prevSpace = (char *)__INST(address_); |
|
1267 |
if (prevSpace == (char *)nil) |
|
1268 |
prevSpace = 0; /* allocate from scratch */ |
|
1269 |
space = __stx_realloc(prevSpace, nBytes); |
|
1270 |
if (space) { |
|
1271 |
__INST(address_) = (OBJ)space; |
|
1272 |
__INST(size) = numberOfBytes; |
|
1273 |
if (space == prevSpace) { |
|
1274 |
/* same address, no re-registration */ |
|
1275 |
RETURN(self); |
|
1276 |
} |
|
1277 |
mallocStatus = true; |
|
1278 |
} else { |
|
1279 |
mallocStatus = false; |
|
1280 |
} |
|
1281 |
} |
|
6457 | 1282 |
} |
1283 |
%}. |
|
1284 |
mallocStatus == true ifTrue:[ |
|
6503 | 1285 |
Lobby registerChange:self. |
6457 | 1286 |
] ifFalse:[mallocStatus == false ifTrue:[ |
6503 | 1287 |
^ MallocFailure raiseRequestWith:numberOfBytes. |
6457 | 1288 |
] ifFalse:[ |
6503 | 1289 |
self primitiveFailed. |
6457 | 1290 |
]]. |
1291 |
! ! |
|
1292 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1293 |
!ExternalBytes class methodsFor:'documentation'! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1294 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1295 |
version |
17711 | 1296 |
^ '$Id: ExternalBytes.st 10448 2009-06-14 16:10:51Z vranyj1 $' |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1297 |
! ! |
7184 | 1298 |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1299 |
ExternalBytes initialize! |