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