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