author | Claus Gittinger <cg@exept.de> |
Thu, 08 Apr 2010 13:57:17 +0200 | |
changeset 12864 | b22c567683a2 |
parent 12788 | ffc937f4b361 |
child 13076 | 29b8a558bcf2 |
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) { |
|
12788
ffc937f4b361
No longer use _NEED_L_MODIFIER_IN_PRINTF
Stefan Vogel <sv@exept.de>
parents:
12786
diff
changeset
|
59 |
printf("EXTBYTES [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) { |
|
12788
ffc937f4b361
No longer use _NEED_L_MODIFIER_IN_PRINTF
Stefan Vogel <sv@exept.de>
parents:
12786
diff
changeset
|
78 |
printf("EXTBYTES [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) { |
12788
ffc937f4b361
No longer use _NEED_L_MODIFIER_IN_PRINTF
Stefan Vogel <sv@exept.de>
parents:
12786
diff
changeset
|
101 |
printf("EXTBYTES [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) { |
12788
ffc937f4b361
No longer use _NEED_L_MODIFIER_IN_PRINTF
Stefan Vogel <sv@exept.de>
parents:
12786
diff
changeset
|
132 |
printf("EXTBYTES [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) { |
141 |
printf("EXTBYTES: free bytes at: %08x\n", ptr); |
|
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 |
} |
|
157 |
printf("EXTBYTES [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:[ |
|
11854 | 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 |
11854 | 437 |
^ ((self new:aString size+1) |
438 |
replaceBytesFrom:1 to:aString size with:aString startingAt:1) |
|
439 |
at:aString size+1 put:0; |
|
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 |
11854 | 449 |
as soon as the reference to the returned externalBytes object |
450 |
is gone (by the next garbage collect). |
|
451 |
If the memory has been passed to a C-function which |
|
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) { |
12786 | 486 |
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) { |
12786 | 511 |
if (@global(TraceMalloc) == true ) { |
512 |
printf("EXTBYTES [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) { |
11854 | 640 |
unsigned INT addr; |
6459 | 641 |
|
11854 | 642 |
addr = (unsigned INT)__INST(address_); |
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 |
||
746 |
instVarAt:index |
|
747 |
"redefined to suppress direct access to my address, which is a non-object" |
|
748 |
||
12864 | 749 |
index == 1 ifTrue:[^ self address]. |
6459 | 750 |
^ super instVarAt:index |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
751 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
752 |
|
11739 | 753 |
!ExternalBytes methodsFor:'compatibility-VW'! |
754 |
||
755 |
copyCStringFromHeap |
|
12594
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
756 |
|idx byte s| |
11739 | 757 |
|
12594
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
758 |
idx := 1. |
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
759 |
s := WriteStream on:String new. |
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
760 |
[(byte := self at:idx) ~~ 0] whileTrue:[ |
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
761 |
s nextPut:(Character value:byte). |
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
762 |
idx := idx + 1. |
11739 | 763 |
]. |
12594
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
764 |
^ s contents asString |
11739 | 765 |
! ! |
766 |
||
6491
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
767 |
!ExternalBytes methodsFor:'converting'! |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
768 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
769 |
asExternalAddress |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
770 |
"return the start address as an external address" |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
771 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
772 |
%{ /* NOCONTEXT */ |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
773 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
774 |
RETURN(__MKEXTERNALADDRESS(__INST(address_))); |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
775 |
%}. |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
776 |
! |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
777 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
778 |
asExternalBytes |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
779 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
780 |
^ self |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
781 |
! ! |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
782 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
783 |
!ExternalBytes methodsFor:'filling & replacing'! |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
784 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
785 |
replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
786 |
"replace elements from another collection, which must be a ByteArray- |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
787 |
like collection. |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
788 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
789 |
Notice: This operation modifies the receiver, NOT a copy; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
790 |
therefore the change may affect all others referencing the receiver." |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
791 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
792 |
%{ /* NOCONTEXT */ |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
793 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
794 |
int nIndex, repNIndex; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
795 |
int startIndex, stopIndex; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
796 |
REGISTER unsigned char *src; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
797 |
REGISTER int repStartIndex; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
798 |
int repStopIndex, count; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
799 |
REGISTER unsigned char *dst; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
800 |
OBJ cls; |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
801 |
|
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
802 |
if ((__isBytes(aCollection) || __isExternalBytesLike(aCollection)) |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
803 |
&& __bothSmallInteger(start, stop) |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
804 |
&& __isSmallInteger(repStart)) { |
8908 | 805 |
startIndex = __intVal(start) - 1; |
806 |
if (startIndex >= 0) { |
|
807 |
dst = (unsigned char *)__INST(address_) + startIndex; |
|
808 |
nIndex = __smallIntegerVal(__INST(size)); |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
809 |
|
8908 | 810 |
stopIndex = __intVal(stop) - 1; |
811 |
count = stopIndex - startIndex + 1; |
|
812 |
if (count == 0) { |
|
813 |
RETURN ( self ); |
|
814 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
815 |
|
8908 | 816 |
if ((count > 0) && (stopIndex < nIndex)) { |
817 |
repStartIndex = __intVal(repStart) - 1; |
|
818 |
if (repStartIndex >= 0) { |
|
819 |
if (__isExternalBytesLike(aCollection)) { |
|
820 |
OBJ sz; |
|
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
821 |
|
8908 | 822 |
src = __externalBytesVal(aCollection); |
823 |
if (src == 0) goto fallBack; |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
824 |
|
8908 | 825 |
sz = __externalBytesSize(aCollection); |
826 |
if (__isSmallInteger(sz)) { |
|
827 |
repNIndex = __smallIntegerVal(sz); |
|
828 |
} else { |
|
829 |
repNIndex = -1; /* unknown */ |
|
830 |
} |
|
831 |
src = src + repStartIndex; |
|
832 |
} else { |
|
833 |
repNIndex = __qSize(aCollection) - OHDR_SIZE; |
|
834 |
src = __byteArrayVal(aCollection) + repStartIndex; |
|
835 |
if ((cls = __qClass(aCollection)) != @global(ByteArray)) { |
|
836 |
int nInst; |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
837 |
|
8908 | 838 |
nInst = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
839 |
src += nInst; |
|
840 |
repNIndex -= nInst; |
|
841 |
} |
|
842 |
} |
|
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
843 |
|
8908 | 844 |
repStopIndex = repStartIndex + (stopIndex - startIndex); |
845 |
if (repStopIndex < repNIndex) { |
|
846 |
if (aCollection == self) { |
|
847 |
/* take care of overlapping copy */ |
|
848 |
if (src < dst) { |
|
849 |
/* must do a reverse copy */ |
|
850 |
src += count; |
|
851 |
dst += count; |
|
852 |
while (count-- > 0) { |
|
853 |
*--dst = *--src; |
|
854 |
} |
|
855 |
RETURN ( self ); |
|
856 |
} |
|
857 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
858 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
859 |
#ifdef memcpy4 |
8908 | 860 |
if (((unsigned INT)src & 3) == ((unsigned INT)dst & 3)) { |
861 |
int nW; |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
862 |
|
8908 | 863 |
/* copy unaligned part */ |
864 |
while (count && ((unsigned INT)src & 3)) { |
|
865 |
*dst++ = *src++; |
|
866 |
count--; |
|
867 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
868 |
|
8908 | 869 |
if (count > 0) { |
870 |
/* copy aligned part */ |
|
871 |
nW = count >> 2; |
|
872 |
memcpy4(dst, src, nW); |
|
873 |
if ((count = count & 3) != 0) { |
|
874 |
/* copy any remaining part */ |
|
875 |
src += (nW<<2); |
|
876 |
dst += (nW<<2); |
|
877 |
while (count--) { |
|
878 |
*dst++ = *src++; |
|
879 |
} |
|
880 |
} |
|
881 |
} |
|
882 |
RETURN ( self ); |
|
883 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
884 |
#else |
8901
824a89d0b5c7
alpha64 vs. POINTER_SIZE cleanup
Claus Gittinger <cg@exept.de>
parents:
8288
diff
changeset
|
885 |
# if __POINTER_SIZE__ == 8 |
8908 | 886 |
if (((unsigned INT)src & 7) == ((unsigned INT)dst & 7)) { |
887 |
/* copy unaligned part */ |
|
888 |
while (count && ((unsigned INT)src & 7)) { |
|
889 |
*dst++ = *src++; |
|
890 |
count--; |
|
891 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
892 |
|
8908 | 893 |
/* copy aligned part */ |
894 |
while (count >= 8) { |
|
895 |
((unsigned INT *)dst)[0] = ((unsigned INT *)src)[0]; |
|
896 |
dst += 8; |
|
897 |
src += 8; |
|
898 |
count -= 8; |
|
899 |
} |
|
900 |
while (count--) { |
|
901 |
*dst++ = *src++; |
|
902 |
} |
|
903 |
RETURN ( self ); |
|
904 |
} |
|
905 |
# endif /* 64bit */ |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
906 |
#endif /* memcpy4 */ |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
907 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
908 |
#ifdef FAST_MEMCPY |
8908 | 909 |
memcpy(dst, src, count); |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
910 |
#else |
8919 | 911 |
# ifdef __UNROLL_LOOPS__ |
8908 | 912 |
while (count >= 8) { |
913 |
dst[0] = src[0]; dst[1] = src[1]; |
|
914 |
dst[2] = src[2]; dst[3] = src[3]; |
|
915 |
dst[4] = src[4]; dst[5] = src[5]; |
|
916 |
dst[6] = src[6]; dst[7] = src[7]; |
|
917 |
dst += 8; src += 8; |
|
918 |
count -= 8; |
|
919 |
} |
|
8919 | 920 |
# endif /* __UNROLL_LOOPS__ */ |
8908 | 921 |
while (count-- > 0) { |
922 |
*dst++ = *src++; |
|
923 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
924 |
#endif |
8908 | 925 |
RETURN ( self ); |
926 |
} |
|
927 |
} |
|
928 |
} |
|
929 |
} |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
930 |
} |
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
931 |
fallBack: ; |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
932 |
%}. |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
933 |
" |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
934 |
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
|
935 |
" |
7191
82bddba980b6
tuned the replaceBytes from another externalBytes object case.
Claus Gittinger <cg@exept.de>
parents:
7184
diff
changeset
|
936 |
^ super replaceBytesFrom:start to:stop with:aCollection startingAt:repStart |
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
937 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
938 |
" |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
939 |
(ExternalBytes unprotectedNew:16) |
8908 | 940 |
replaceFrom:1 to:8 |
941 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
942 |
startingAt:1 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
943 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
944 |
(ExternalBytes unprotectedNew:16) |
8908 | 945 |
replaceFrom:3 to:10 |
946 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
947 |
startingAt:4 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
948 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
949 |
(ExternalBytes unprotectedNew:16) |
8908 | 950 |
replaceFrom:3 to:4 |
951 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
952 |
startingAt:1 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
953 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
954 |
(ExternalBytes unprotectedNew:16) |
8908 | 955 |
replaceFrom:0 to:9 |
956 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
957 |
startingAt:1 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
958 |
|
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
959 |
(ExternalBytes unprotectedNew:16) |
8908 | 960 |
replaceFrom:1 to:10 |
961 |
with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160] |
|
962 |
startingAt:0 |
|
6483
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
963 |
" |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
964 |
! ! |
ec599166f169
replaceBytesFrom:to:with:startingAt:
Stefan Vogel <sv@exept.de>
parents:
6471
diff
changeset
|
965 |
|
6444 | 966 |
!ExternalBytes methodsFor:'finalization'! |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
967 |
|
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
968 |
executor |
8908 | 969 |
"redefined to return a lightweight copy |
7184 | 970 |
- 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
|
971 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
972 |
%{ /* NOCONTEXT */ |
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
973 |
|
7386
0996b933cbf1
Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents:
7257
diff
changeset
|
974 |
OBJ theCopy, cls; |
7184 | 975 |
|
7855
27f64d1beaa1
Have to protect self, otherwise bad things happen in the VM.
Stefan Vogel <sv@exept.de>
parents:
7386
diff
changeset
|
976 |
__PROTECT__(self); |
7184 | 977 |
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
|
978 |
__UNPROTECT__(self); |
7386
0996b933cbf1
Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents:
7257
diff
changeset
|
979 |
__InstPtr(theCopy)->o_class = cls = __InstPtr(self)->o_class; |
0996b933cbf1
Need __STORE() when assigning class
Stefan Vogel <sv@exept.de>
parents:
7257
diff
changeset
|
980 |
__STORE(theCopy, cls); |
7184 | 981 |
RETURN (theCopy); |
6491
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
982 |
%} |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
983 |
|
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
984 |
" |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
985 |
(ExternalBytes unprotectedNew:10) executor |
6976d9b69e01
Conversion between ExternalBytes and ExternalAddress
Stefan Vogel <sv@exept.de>
parents:
6484
diff
changeset
|
986 |
" |
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
987 |
! |
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
988 |
|
6462
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
989 |
finalizationLobby |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
990 |
"answer the registry used for finalization. |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
991 |
ExternalBytes have their own Registry" |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
992 |
|
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
993 |
^ Lobby |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
994 |
! |
245e99c09df0
Allow #registerForFinalization for classes defining their own Lobby.
Stefan Vogel <sv@exept.de>
parents:
6459
diff
changeset
|
995 |
|
6440
eb9cdc352a7c
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6425
diff
changeset
|
996 |
finalize |
2779 | 997 |
"some ExternalBytes object was finalized; |
998 |
free the associated heap memory with it" |
|
999 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1000 |
%{ /* NOCONTEXT */ |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1001 |
|
6457 | 1002 |
char *mem = (char *)(__INST(address_)); |
1003 |
if (mem && (OBJ)mem != nil) { |
|
6459 | 1004 |
__stx_free(mem); |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1005 |
} |
1133 | 1006 |
__INST(address_) = __INST(size) = nil; |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1007 |
%} |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1008 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1009 |
|
6444 | 1010 |
!ExternalBytes methodsFor:'freeing'! |
1011 |
||
6459 | 1012 |
free |
1013 |
"free a previously allocated piece of memory - be very careful, there |
|
1014 |
are no checks done here. All dangers you usually have with malloc/free |
|
1015 |
are present here ..." |
|
1016 |
||
1017 |
"at least, we check for double freeing the same chunk" |
|
1018 |
self address isNil ifTrue:[ |
|
1019 |
self error:'freeing memory twice'. |
|
1020 |
^ self |
|
1021 |
]. |
|
1022 |
Lobby unregister:self. |
|
1023 |
self finalize. "/ does what we need here .. |
|
1024 |
self unprotectFromGC |
|
7184 | 1025 |
! |
1026 |
||
1027 |
register |
|
1028 |
"register the receiver to be automatically finalized by the GC" |
|
1029 |
||
1030 |
Lobby register:self. |
|
6444 | 1031 |
! ! |
1032 |
||
6443 | 1033 |
!ExternalBytes methodsFor:'pointer arithmethic'! |
1034 |
||
6502
87f587d1542e
rename #from:to to #referenceToBytesFrom:to
Stefan Vogel <sv@exept.de>
parents:
6491
diff
changeset
|
1035 |
referenceToBytesFrom:start to:stop |
6459 | 1036 |
"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
|
1037 |
BE CAREFUL: after the receiver has been freed, the new ExternalBytes |
6459 | 1038 |
contents is undefined" |
1039 |
||
8908 | 1040 |
%{ /* NOCONTEXT */ |
6459 | 1041 |
|
6471 | 1042 |
char *addr; |
6459 | 1043 |
int size; |
1044 |
int __start, __stop; |
|
1045 |
||
1046 |
if (__bothSmallInteger(start, stop) && __INST(address_) != nil) { |
|
6503 | 1047 |
__start = __smallIntegerVal(start); |
1048 |
__stop = __smallIntegerVal(stop); |
|
1049 |
if (__start > 0 && __start <= __stop && __stop <= __smallIntegerVal(__INST(size))) { |
|
1050 |
addr = (char *)(__INST(address_)) + (__start - 1); |
|
1051 |
size = __stop - __start + 1; |
|
1052 |
RETURN( __MKEXTERNALBYTES_N(addr, size) ); |
|
1053 |
} |
|
6459 | 1054 |
} |
1055 |
%}. |
|
1056 |
^ self primitiveFailed |
|
6458 | 1057 |
! ! |
6443 | 1058 |
|
6458 | 1059 |
!ExternalBytes methodsFor:'printing & storing'! |
6443 | 1060 |
|
6459 | 1061 |
displayString |
1062 |
"return a printed representation of the receiver for displaying" |
|
1063 |
||
1064 |
|addr addrString| |
|
6443 | 1065 |
|
6459 | 1066 |
(addr := self address) isNil ifTrue:[ |
11854 | 1067 |
addrString := '[free]' |
6459 | 1068 |
] ifFalse:[ |
11854 | 1069 |
size notNil ifTrue:[ |
1070 |
addrString := '[sz:', size printString, ' ' |
|
1071 |
] ifFalse:[ |
|
1072 |
addrString := '[' |
|
1073 |
]. |
|
1074 |
addrString := addrString , 'at:' , (addr printStringRadix:16), ']' |
|
6459 | 1075 |
]. |
1076 |
^ self class name , addrString |
|
1077 |
||
1078 |
"Modified: / 24.2.2000 / 19:02:19 / cg" |
|
6458 | 1079 |
! ! |
1080 |
||
7257 | 1081 |
!ExternalBytes methodsFor:'private-accessing'! |
6458 | 1082 |
|
9388 | 1083 |
invalidateReference |
1084 |
"clear the start address and size" |
|
1085 |
||
1086 |
%{ /* NOCONTEXT */ |
|
1087 |
__INST(address_) = nil; |
|
1088 |
__INST(size) = nil; |
|
1089 |
%} |
|
1090 |
! |
|
1091 |
||
8908 | 1092 |
setAddress:aNumber size:sz |
6459 | 1093 |
"set the start address and size" |
1094 |
||
1095 |
%{ /* NOCONTEXT */ |
|
1096 |
if (__INST(address_) == nil) { |
|
1097 |
if (aNumber == nil) { |
|
1098 |
__INST(address_) = nil; |
|
1099 |
} else { |
|
1100 |
if (__isSmallInteger(aNumber)) { |
|
1101 |
__INST(address_) = (OBJ) __intVal(aNumber); |
|
1102 |
} else if(__isInteger(aNumber)) { |
|
1103 |
__INST(address_) = (OBJ) __unsignedLongIntVal(aNumber); |
|
1104 |
} else if(__isExternalAddress(aNumber)) { |
|
1105 |
__INST(address_) = __externalAddressVal(aNumber); |
|
1106 |
} |
|
6445 | 1107 |
} |
6459 | 1108 |
__INST(size) = sz; |
1109 |
RETURN (self); |
|
6443 | 1110 |
} |
1111 |
%}. |
|
6459 | 1112 |
^ self error:'cannot change address' |
8288 | 1113 |
! |
1114 |
||
8908 | 1115 |
setSize:sz |
8288 | 1116 |
"set the size - warning: dangerous if wrong" |
1117 |
||
1118 |
size := sz |
|
6443 | 1119 |
! ! |
1120 |
||
7257 | 1121 |
!ExternalBytes methodsFor:'private-allocation'! |
6444 | 1122 |
|
1123 |
allocateBytes:numberOfBytes |
|
11854 | 1124 |
"allocate (malloc) numberOfBytes; if doClear is true, the allocated memory is cleared. |
1125 |
Fail if already allocated. |
|
1126 |
Raise MallocFailure if malloc fails to allocate enough memory" |
|
1127 |
||
1128 |
^ self allocateBytes:numberOfBytes clear:true |
|
1129 |
! |
|
1130 |
||
1131 |
allocateBytes:numberOfBytes clear:doClear |
|
1132 |
"allocate (malloc) numberOfBytes; if doClear is true, the allocated memory is cleared. |
|
6444 | 1133 |
Fail if already allocated. |
1134 |
Raise MallocFailure if malloc fails to allocate enough memory" |
|
1135 |
||
1136 |
|mallocFailure| |
|
1137 |
||
6457 | 1138 |
%{ |
6444 | 1139 |
char *space; |
1140 |
unsigned int nBytes; |
|
1141 |
char *__stx_malloc(); |
|
1142 |
||
1143 |
/* |
|
8908 | 1144 |
* Fail if already allocated |
6444 | 1145 |
*/ |
1146 |
if (__INST(address_) == nil && __isSmallInteger(numberOfBytes)) { |
|
6503 | 1147 |
nBytes = __smallIntegerVal(numberOfBytes); |
1148 |
if (nBytes > 0) { |
|
1149 |
space = __stx_malloc(nBytes); |
|
1150 |
if (space) { |
|
11854 | 1151 |
if (doClear == true) { |
1152 |
bzero(space, nBytes); |
|
1153 |
} |
|
6503 | 1154 |
__INST(address_) = (OBJ)space; |
1155 |
__INST(size) = numberOfBytes; |
|
1156 |
RETURN(self); |
|
1157 |
} else { |
|
1158 |
mallocFailure = true; |
|
1159 |
} |
|
1160 |
} |
|
6444 | 1161 |
} |
1162 |
%}. |
|
1163 |
mallocFailure == true ifTrue:[ |
|
6503 | 1164 |
^ MallocFailure raiseRequestWith:numberOfBytes. |
6444 | 1165 |
] ifFalse:[ |
6503 | 1166 |
self primitiveFailed. |
6444 | 1167 |
]. |
1168 |
! ! |
|
1169 |
||
5557 | 1170 |
!ExternalBytes methodsFor:'queries'! |
1171 |
||
6459 | 1172 |
basicSize |
1173 |
"we do not know how many bytes are valid" |
|
5557 | 1174 |
|
6459 | 1175 |
size isNil ifTrue:[^ 0]. |
1176 |
^ size |
|
6443 | 1177 |
! |
1178 |
||
9389 | 1179 |
isValid |
1180 |
"true if I gave an address" |
|
1181 |
||
1182 |
%{ /* NOCONTEXT */ |
|
1183 |
RETURN ((__INST(address_) == nil) ? false : true ); |
|
1184 |
%} |
|
1185 |
! |
|
1186 |
||
6459 | 1187 |
species |
1188 |
"when copying, or concatenating, return instances of this class" |
|
1189 |
||
1190 |
^ ByteArray |
|
5557 | 1191 |
! ! |
1192 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1193 |
!ExternalBytes methodsFor:'registration'! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1194 |
|
8908 | 1195 |
protectFromGC |
6459 | 1196 |
"enter a reference to the receiver into the AllocatedInstances |
1197 |
class variable - this prevents it from ever being finalized by |
|
1198 |
the garbage collector, thus protecting the underlying memory." |
|
1199 |
||
10137
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1200 |
|wasBlocked| |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1201 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1202 |
|
11854 | 1203 |
"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
|
1204 |
a finalization method" |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1205 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1206 |
wasBlocked := OperatingSystem blockInterrupts. |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1207 |
"/ AccessLock critical:[ |
11854 | 1208 |
AllocatedInstances isNil ifTrue:[ |
1209 |
AllocatedInstances := IdentitySet new |
|
1210 |
]. |
|
1211 |
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
|
1212 |
"/ ] |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1213 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1214 |
! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1215 |
|
8908 | 1216 |
unprotectFromGC |
6459 | 1217 |
"remove the receiver from the AllocatedInstances |
1218 |
class variable - if there is no other reference to the receiver, |
|
1219 |
the next garbage collect will finalize the receiver and the underlying |
|
1220 |
memory be freed." |
|
1221 |
||
10137
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1222 |
|wasBlocked| |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1223 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1224 |
"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
|
1225 |
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
|
1226 |
|
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1227 |
wasBlocked := OperatingSystem blockInterrupts. |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1228 |
"/ AccessLock critical:[ |
11854 | 1229 |
AllocatedInstances notNil ifTrue:[ |
1230 |
AllocatedInstances remove:self ifAbsent:nil. |
|
10138 | 1231 |
]. |
10137
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1232 |
"/ ] |
7bfdc51fcb65
Avoid deadlock when free is called by finalization code (Stefan Vogel)
Michael Beyl <mb@exept.de>
parents:
9895
diff
changeset
|
1233 |
wasBlocked ifFalse:[OperatingSystem unblockInterrupts]. |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1234 |
! ! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1235 |
|
6457 | 1236 |
!ExternalBytes methodsFor:'resizing'! |
1237 |
||
1238 |
grow:numberOfBytes |
|
1239 |
"reallocate (realloc) numberOfBytes. |
|
1240 |
Raise MallocFailure if realloc fails to allocate enough memory" |
|
1241 |
||
1242 |
|mallocStatus| |
|
1243 |
||
1244 |
%{ |
|
1245 |
char *space, *prevSpace; |
|
1246 |
unsigned int nBytes; |
|
1247 |
char *__stx_realloc(); |
|
1248 |
||
1249 |
if (__isSmallInteger(numberOfBytes)) { |
|
6503 | 1250 |
nBytes = __smallIntegerVal(numberOfBytes); |
1251 |
if (nBytes > 0) { |
|
1252 |
prevSpace = (char *)__INST(address_); |
|
1253 |
if (prevSpace == (char *)nil) |
|
1254 |
prevSpace = 0; /* allocate from scratch */ |
|
1255 |
space = __stx_realloc(prevSpace, nBytes); |
|
1256 |
if (space) { |
|
1257 |
__INST(address_) = (OBJ)space; |
|
1258 |
__INST(size) = numberOfBytes; |
|
1259 |
if (space == prevSpace) { |
|
1260 |
/* same address, no re-registration */ |
|
1261 |
RETURN(self); |
|
1262 |
} |
|
1263 |
mallocStatus = true; |
|
1264 |
} else { |
|
1265 |
mallocStatus = false; |
|
1266 |
} |
|
1267 |
} |
|
6457 | 1268 |
} |
1269 |
%}. |
|
1270 |
mallocStatus == true ifTrue:[ |
|
6503 | 1271 |
Lobby registerChange:self. |
6457 | 1272 |
] ifFalse:[mallocStatus == false ifTrue:[ |
6503 | 1273 |
^ MallocFailure raiseRequestWith:numberOfBytes. |
6457 | 1274 |
] ifFalse:[ |
6503 | 1275 |
self primitiveFailed. |
6457 | 1276 |
]]. |
1277 |
! ! |
|
1278 |
||
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1279 |
!ExternalBytes class methodsFor:'documentation'! |
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1280 |
|
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1281 |
version |
12864 | 1282 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalBytes.st,v 1.78 2010-04-08 11:57:17 cg Exp $' |
12594
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
1283 |
! |
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
1284 |
|
d7267a747c2f
changed: #copyCStringFromHeap
Claus Gittinger <cg@exept.de>
parents:
11854
diff
changeset
|
1285 |
version_CVS |
12864 | 1286 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalBytes.st,v 1.78 2010-04-08 11:57:17 cg Exp $' |
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1287 |
! ! |
7184 | 1288 |
|
848
76a83f34c26a
moved ExtBytes & ExtFunc from libbasic2
Claus Gittinger <cg@exept.de>
parents:
diff
changeset
|
1289 |
ExternalBytes initialize! |