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