1 /* |
|
2 * Copyright (c) 2010 Ixonos Plc. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Ixonos Plc |
|
14 * |
|
15 * Description: |
|
16 * Wrappers for Symbian OS -specific system functions. |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include <e32base.h> |
|
22 #include <s32file.h> |
|
23 #include "epoclib.h" |
|
24 |
|
25 #define NAMELEN 120 |
|
26 #define EL_EXPORT |
|
27 |
|
28 |
|
29 EL_EXPORT void free(TAny *ptr) |
|
30 |
|
31 { |
|
32 #ifdef MALLOC_DEBUG |
|
33 if ( !ptr ) |
|
34 return; |
|
35 |
|
36 unsigned *p32 = (unsigned*) (((unsigned) ptr) - (NAMELEN+8)); |
|
37 |
|
38 if ( p32[0] != 0xdaded1d0 ) |
|
39 User::Panic(_L("BadFree"), 1); |
|
40 |
|
41 ptr = (TAny*) p32; |
|
42 #endif |
|
43 |
|
44 // Dangling pointer check |
|
45 unsigned count = (unsigned) User::AllocLen(ptr); |
|
46 TUint8 *p; |
|
47 |
|
48 p = (TUint8*) ptr; |
|
49 while ( count >> 2 ) |
|
50 { |
|
51 *((TUint32*)p) = 0xfeedf1d0; |
|
52 p += 4; |
|
53 count -= 4; |
|
54 } |
|
55 while ( count ) |
|
56 { |
|
57 *(p++) = 0x17; |
|
58 count--; |
|
59 } |
|
60 |
|
61 User::Free(ptr); |
|
62 } |
|
63 |
|
64 #ifdef MALLOC_DEBUG |
|
65 EL_EXPORT TAny *debugMalloc(u_int32 size, char *file, int line) |
|
66 #else |
|
67 EL_EXPORT TAny *malloc(u_int32 size) |
|
68 #endif |
|
69 { |
|
70 // Uninit checks |
|
71 TAny *ptr; |
|
72 unsigned count = size; |
|
73 TUint8 *p; |
|
74 |
|
75 #ifdef MALLOC_DEBUG |
|
76 ptr = User::Alloc(size + NAMELEN+8); |
|
77 if ( !ptr ) |
|
78 return 0; |
|
79 p = ((TUint8*) ptr) + NAMELEN+8; |
|
80 #else |
|
81 ptr = User::Alloc(size); |
|
82 if ( !ptr ) |
|
83 return 0; |
|
84 p = (TUint8*) ptr; |
|
85 #endif |
|
86 |
|
87 while ( count >> 2 ) |
|
88 { |
|
89 *((TUint32*)p) = 0xdeadbeef; |
|
90 p += 4; |
|
91 count -= 4; |
|
92 } |
|
93 while ( count ) |
|
94 { |
|
95 *(p++) = 0x42; |
|
96 count--; |
|
97 } |
|
98 |
|
99 #ifdef MALLOC_DEBUG |
|
100 unsigned *p32 = (unsigned*) ptr; |
|
101 p32[0] = 0xdaded1d0; |
|
102 p32[1] = (unsigned) line; |
|
103 char *c = (char*) &p32[2]; |
|
104 int n = NAMELEN; |
|
105 while ( (*file != 0) && n ) |
|
106 { |
|
107 *(c++) = *(file++); |
|
108 n--; |
|
109 } |
|
110 return (TAny*) (((unsigned) p32) + NAMELEN + 8); |
|
111 #else |
|
112 return ptr; |
|
113 #endif |
|
114 } |
|
115 |
|
116 EL_EXPORT TAny *realloc(void *memblock, u_int32 size) |
|
117 { |
|
118 // realloc not used at least currently, skip the debug-branch |
|
119 return User::ReAlloc(memblock, size); |
|
120 } |
|
121 |
|
122 |
|
123 #ifdef MALLOC_DEBUG |
|
124 EL_EXPORT TAny *debugCalloc(u_int32 num, u_int32 size, char *file, int line) |
|
125 { |
|
126 TAny *ptr; |
|
127 TUint8 *p; |
|
128 |
|
129 ptr = User::Alloc(num*size + NAMELEN+8); |
|
130 if ( !ptr ) |
|
131 return 0; |
|
132 p = ((TUint8*) ptr) + NAMELEN+8; |
|
133 |
|
134 Mem::Fill(p, size*num, 0); |
|
135 |
|
136 unsigned *p32 = (unsigned*) ptr; |
|
137 p32[0] = 0xdaded1d0; |
|
138 p32[1] = (unsigned) line; |
|
139 char *c = (char*) &p32[2]; |
|
140 int n = NAMELEN; |
|
141 while ( (*file != 0) && n ) |
|
142 { |
|
143 *(c++) = *(file++); |
|
144 n--; |
|
145 } |
|
146 return (TAny*) p; |
|
147 } |
|
148 #else |
|
149 EL_EXPORT TAny *calloc(u_int32 num, u_int32 size) |
|
150 |
|
151 { |
|
152 TAny *dest = User::Alloc(size*num); |
|
153 Mem::Fill(dest, size*num, 0); |
|
154 return dest; |
|
155 } |
|
156 #endif |
|
157 |
|
158 EL_EXPORT TAny *memset(TAny *dest, TInt c, TInt size) |
|
159 { |
|
160 Mem::Fill(dest, size, c); |
|
161 return dest; //returning the value of dest as in windows |
|
162 } |
|
163 EL_EXPORT TAny *memcpy(TAny *dest, const TAny *src, TInt size) |
|
164 { |
|
165 Mem::Copy(dest, src, size); |
|
166 return dest; |
|
167 } |
|
168 |
|
169 EL_EXPORT TAny *memmove(TAny *dest, const TAny *src, u_int32 count) |
|
170 { |
|
171 Mem::Copy(dest,src,count); |
|
172 return dest; |
|
173 } |
|
174 |
|
175 long atol( |
|
176 const char *nptr |
|
177 ) |
|
178 { |
|
179 int c; // current char |
|
180 long total; // current total |
|
181 int sign; // if '-', then negative, otherwise positive |
|
182 |
|
183 TLex8 string((unsigned char *)nptr); |
|
184 // skip whitespace |
|
185 string.SkipSpace(); |
|
186 |
|
187 //prendre un caratere dans string lui faire le sign |
|
188 c = (int)string.Peek(); |
|
189 string.Inc(); |
|
190 |
|
191 sign = c; // save sign indication |
|
192 if (c == '-' || c == '+') |
|
193 // skip sign |
|
194 {c = (int)string.Peek(); |
|
195 string.Inc(); |
|
196 } |
|
197 else //If c is not a sign, it is necessary to go increment back into the descriptors to get the right |
|
198 //number |
|
199 { |
|
200 string.UnGet(); |
|
201 } |
|
202 |
|
203 total = 0; |
|
204 |
|
205 while (string.Peek().IsDigit()) |
|
206 { |
|
207 total = 10 * total + (c - '0'); |
|
208 string.Inc(); |
|
209 c = (int)string.Peek(); |
|
210 |
|
211 } |
|
212 |
|
213 if (sign == '-') |
|
214 return -total; |
|
215 else |
|
216 return total; /* return result, negated if necessary */ |
|
217 } |
|
218 |
|
219 |
|
220 /* |
|
221 *int atoi(char *nptr) - Convert string to long |
|
222 * |
|
223 *Purpose: |
|
224 * Converts ASCII string pointed to by nptr to binary. |
|
225 * Overflow is not detected. Because of this, we can just use |
|
226 * atol(). |
|
227 * |
|
228 *Entry: |
|
229 * nptr = ptr to string to convert |
|
230 * |
|
231 *Exit: |
|
232 * return int value of the string |
|
233 * |
|
234 *Exceptions: |
|
235 * None - overflow is not detected. |
|
236 * |
|
237 *******************************************************************************/ |
|
238 |
|
239 EL_EXPORT int atoi( |
|
240 const char *nptr |
|
241 ) |
|
242 { |
|
243 return (int)atol(nptr); |
|
244 } |
|
245 // End of File |
|