1 /*
2 * msvcrt.dll heap functions
3 *
4 * Copyright 2000 Jon Griffiths
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * Note: Win32 heap operations are MT safe. We only lock the new
21 * handler and non atomic heap operations
22 */
23
24 #include "msvcrt.h"
25 #include "mtdll.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
29
30 /* MT */
31 #define LOCK_HEAP _mlock( _HEAP_LOCK )
32 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
33
34 /* _aligned */
35 #define SAVED_PTR(x) ((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
36 ~(sizeof(void *) - 1)))
37 #define ALIGN_PTR(ptr, alignment, offset) ((void *) \
38 ((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
39 ~(alignment - 1)) - offset))
40
41
42 typedef void (CDECL *MSVCRT_new_handler_func)(MSVCRT_size_t size);
43
44 static MSVCRT_new_handler_func MSVCRT_new_handler;
45 static int MSVCRT_new_mode;
46
47 /* FIXME - According to documentation it should be 8*1024, at runtime it returns 16 */
48 static unsigned int MSVCRT_amblksiz = 16;
49 /* FIXME - According to documentation it should be 480 bytes, at runtime default is 0 */
50 static MSVCRT_size_t MSVCRT_sbh_threshold = 0;
51
52 /*********************************************************************
53 * ??2@YAPAXI@Z (MSVCRT.@)
54 */
55 void* CDECL MSVCRT_operator_new(MSVCRT_size_t size)
56 {
57 void *retval = HeapAlloc(GetProcessHeap(), 0, size);
58 TRACE("(%ld) returning %p\n", size, retval);
59 if(retval) return retval;
60 LOCK_HEAP;
61 if(MSVCRT_new_handler)
62 (*MSVCRT_new_handler)(size);
63 UNLOCK_HEAP;
64 return retval;
65 }
66
67
68 /*********************************************************************
69 * ??2@YAPAXIHPBDH@Z (MSVCRT.@)
70 */
71 void* CDECL MSVCRT_operator_new_dbg(MSVCRT_size_t size, int type, const char *file, int line)
72 {
73 return MSVCRT_operator_new( size );
74 }
75
76
77 /*********************************************************************
78 * ??3@YAXPAX@Z (MSVCRT.@)
79 */
80 void CDECL MSVCRT_operator_delete(void *mem)
81 {
82 TRACE("(%p)\n", mem);
83 HeapFree(GetProcessHeap(), 0, mem);
84 }
85
86
87 /*********************************************************************
88 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
89 */
90 MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler(void)
91 {
92 return MSVCRT_new_handler;
93 }
94
95
96 /*********************************************************************
97 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
98 */
99 int CDECL MSVCRT__query_new_mode(void)
100 {
101 return MSVCRT_new_mode;
102 }
103
104 /*********************************************************************
105 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
106 */
107 MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
108 {
109 MSVCRT_new_handler_func old_handler;
110 LOCK_HEAP;
111 old_handler = MSVCRT_new_handler;
112 MSVCRT_new_handler = func;
113 UNLOCK_HEAP;
114 return old_handler;
115 }
116
117 /*********************************************************************
118 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
119 */
120 MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler(void *func)
121 {
122 TRACE("(%p)\n",func);
123 MSVCRT__set_new_handler(NULL);
124 return NULL;
125 }
126
127 /*********************************************************************
128 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
129 */
130 int CDECL MSVCRT__set_new_mode(int mode)
131 {
132 int old_mode;
133 LOCK_HEAP;
134 old_mode = MSVCRT_new_mode;
135 MSVCRT_new_mode = mode;
136 UNLOCK_HEAP;
137 return old_mode;
138 }
139
140 /*********************************************************************
141 * _callnewh (MSVCRT.@)
142 */
143 int CDECL _callnewh(MSVCRT_size_t size)
144 {
145 if(MSVCRT_new_handler)
146 (*MSVCRT_new_handler)(size);
147 return 0;
148 }
149
150 /*********************************************************************
151 * _expand (MSVCRT.@)
152 */
153 void* CDECL _expand(void* mem, MSVCRT_size_t size)
154 {
155 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, mem, size);
156 }
157
158 /*********************************************************************
159 * _heapchk (MSVCRT.@)
160 */
161 int CDECL _heapchk(void)
162 {
163 if (!HeapValidate( GetProcessHeap(), 0, NULL))
164 {
165 msvcrt_set_errno(GetLastError());
166 return MSVCRT__HEAPBADNODE;
167 }
168 return MSVCRT__HEAPOK;
169 }
170
171 /*********************************************************************
172 * _heapmin (MSVCRT.@)
173 */
174 int CDECL _heapmin(void)
175 {
176 if (!HeapCompact( GetProcessHeap(), 0 ))
177 {
178 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
179 msvcrt_set_errno(GetLastError());
180 return -1;
181 }
182 return 0;
183 }
184
185 /*********************************************************************
186 * _heapwalk (MSVCRT.@)
187 */
188 int CDECL _heapwalk(struct MSVCRT__heapinfo* next)
189 {
190 PROCESS_HEAP_ENTRY phe;
191
192 LOCK_HEAP;
193 phe.lpData = next->_pentry;
194 phe.cbData = next->_size;
195 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
196
197 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
198 !HeapValidate( GetProcessHeap(), 0, phe.lpData ))
199 {
200 UNLOCK_HEAP;
201 msvcrt_set_errno(GetLastError());
202 return MSVCRT__HEAPBADNODE;
203 }
204
205 do
206 {
207 if (!HeapWalk( GetProcessHeap(), &phe ))
208 {
209 UNLOCK_HEAP;
210 if (GetLastError() == ERROR_NO_MORE_ITEMS)
211 return MSVCRT__HEAPEND;
212 msvcrt_set_errno(GetLastError());
213 if (!phe.lpData)
214 return MSVCRT__HEAPBADBEGIN;
215 return MSVCRT__HEAPBADNODE;
216 }
217 } while (phe.wFlags & (PROCESS_HEAP_REGION|PROCESS_HEAP_UNCOMMITTED_RANGE));
218
219 UNLOCK_HEAP;
220 next->_pentry = phe.lpData;
221 next->_size = phe.cbData;
222 next->_useflag = phe.wFlags & PROCESS_HEAP_ENTRY_BUSY ? MSVCRT__USEDENTRY : MSVCRT__FREEENTRY;
223 return MSVCRT__HEAPOK;
224 }
225
226 /*********************************************************************
227 * _heapset (MSVCRT.@)
228 */
229 int CDECL _heapset(unsigned int value)
230 {
231 int retval;
232 struct MSVCRT__heapinfo heap;
233
234 memset( &heap, 0, sizeof(heap) );
235 LOCK_HEAP;
236 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
237 {
238 if (heap._useflag == MSVCRT__FREEENTRY)
239 memset(heap._pentry, value, heap._size);
240 }
241 UNLOCK_HEAP;
242 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
243 }
244
245 /*********************************************************************
246 * _heapadd (MSVCRT.@)
247 */
248 int CDECL _heapadd(void* mem, MSVCRT_size_t size)
249 {
250 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
251 *MSVCRT__errno() = MSVCRT_ENOSYS;
252 return -1;
253 }
254
255 /*********************************************************************
256 * _heapadd (MSVCRT.@)
257 */
258 MSVCRT_intptr_t CDECL _get_heap_handle(void)
259 {
260 return (MSVCRT_intptr_t)GetProcessHeap();
261 }
262
263 /*********************************************************************
264 * _msize (MSVCRT.@)
265 */
266 MSVCRT_size_t CDECL _msize(void* mem)
267 {
268 MSVCRT_size_t size = HeapSize(GetProcessHeap(),0,mem);
269 if (size == ~(MSVCRT_size_t)0)
270 {
271 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
272 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
273 }
274 return size;
275 }
276
277 /*********************************************************************
278 * calloc (MSVCRT.@)
279 */
280 void* CDECL MSVCRT_calloc(MSVCRT_size_t size, MSVCRT_size_t count)
281 {
282 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
283 }
284
285 /*********************************************************************
286 * free (MSVCRT.@)
287 */
288 void CDECL MSVCRT_free(void* ptr)
289 {
290 HeapFree(GetProcessHeap(),0,ptr);
291 }
292
293 /*********************************************************************
294 * malloc (MSVCRT.@)
295 */
296 void* CDECL MSVCRT_malloc(MSVCRT_size_t size)
297 {
298 void *ret = HeapAlloc(GetProcessHeap(),0,size);
299 if (!ret)
300 *MSVCRT__errno() = MSVCRT_ENOMEM;
301 return ret;
302 }
303
304 /*********************************************************************
305 * realloc (MSVCRT.@)
306 */
307 void* CDECL MSVCRT_realloc(void* ptr, MSVCRT_size_t size)
308 {
309 if (!ptr) return MSVCRT_malloc(size);
310 if (size) return HeapReAlloc(GetProcessHeap(), 0, ptr, size);
311 MSVCRT_free(ptr);
312 return NULL;
313 }
314
315 /*********************************************************************
316 * __p__amblksiz (MSVCRT.@)
317 */
318 unsigned int* CDECL __p__amblksiz(void)
319 {
320 return &MSVCRT_amblksiz;
321 }
322
323 /*********************************************************************
324 * _get_sbh_threshold (MSVCRT.@)
325 */
326 MSVCRT_size_t CDECL _get_sbh_threshold(void)
327 {
328 return MSVCRT_sbh_threshold;
329 }
330
331 /*********************************************************************
332 * _set_sbh_threshold (MSVCRT.@)
333 */
334 int CDECL _set_sbh_threshold(MSVCRT_size_t threshold)
335 {
336 if(threshold > 1016)
337 return 0;
338 else
339 MSVCRT_sbh_threshold = threshold;
340 return 1;
341 }
342
343 /*********************************************************************
344 * _aligned_free (MSVCRT.@)
345 */
346 void CDECL _aligned_free(void *memblock)
347 {
348 TRACE("(%p)\n", memblock);
349
350 if (memblock)
351 {
352 void **saved = SAVED_PTR(memblock);
353 MSVCRT_free(*saved);
354 }
355 }
356
357 /*********************************************************************
358 * _aligned_offset_malloc (MSVCRT.@)
359 */
360 void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
361 {
362 void *memblock, *temp, **saved;
363 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
364
365 /* alignment must be a power of 2 */
366 if ((alignment & (alignment - 1)) != 0)
367 {
368 *MSVCRT__errno() = MSVCRT_EINVAL;
369 return NULL;
370 }
371
372 /* offset must be less than size */
373 if (offset >= size)
374 {
375 *MSVCRT__errno() = MSVCRT_EINVAL;
376 return NULL;
377 }
378
379 /* don't align to less than void pointer size */
380 if (alignment < sizeof(void *))
381 alignment = sizeof(void *);
382
383 /* allocate enough space for void pointer and alignment */
384 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
385
386 if (!temp)
387 return NULL;
388
389 /* adjust pointer for proper alignment and offset */
390 memblock = ALIGN_PTR(temp, alignment, offset);
391
392 /* Save the real allocation address below returned address */
393 /* so it can be found later to free. */
394 saved = SAVED_PTR(memblock);
395 *saved = temp;
396
397 return memblock;
398 }
399
400 /*********************************************************************
401 * _aligned_malloc (MSVCRT.@)
402 */
403 void * CDECL _aligned_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment)
404 {
405 TRACE("(%lu, %lu)\n", size, alignment);
406 return _aligned_offset_malloc(size, alignment, 0);
407 }
408
409 /*********************************************************************
410 * _aligned_offset_realloc (MSVCRT.@)
411 */
412 void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size,
413 MSVCRT_size_t alignment, MSVCRT_size_t offset)
414 {
415 void * temp, **saved;
416 MSVCRT_size_t old_padding, new_padding, old_size;
417 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
418
419 if (!memblock)
420 return _aligned_offset_malloc(size, alignment, offset);
421
422 /* alignment must be a power of 2 */
423 if ((alignment & (alignment - 1)) != 0)
424 {
425 *MSVCRT__errno() = MSVCRT_EINVAL;
426 return NULL;
427 }
428
429 /* offset must be less than size */
430 if (offset >= size)
431 {
432 *MSVCRT__errno() = MSVCRT_EINVAL;
433 return NULL;
434 }
435
436 if (size == 0)
437 {
438 _aligned_free(memblock);
439 return NULL;
440 }
441
442 /* don't align to less than void pointer size */
443 if (alignment < sizeof(void *))
444 alignment = sizeof(void *);
445
446 /* make sure alignment and offset didn't change */
447 saved = SAVED_PTR(memblock);
448 if (memblock != ALIGN_PTR(*saved, alignment, offset))
449 {
450 *MSVCRT__errno() = MSVCRT_EINVAL;
451 return NULL;
452 }
453
454 old_padding = (char *)memblock - (char *)*saved;
455
456 /* Get previous size of block */
457 old_size = _msize(*saved);
458 if (old_size == -1)
459 {
460 /* It seems this function was called with an invalid pointer. Bail out. */
461 return NULL;
462 }
463
464 /* Adjust old_size to get amount of actual data in old block. */
465 if (old_size < old_padding)
466 {
467 /* Shouldn't happen. Something's weird, so bail out. */
468 return NULL;
469 }
470 old_size -= old_padding;
471
472 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
473
474 if (!temp)
475 return NULL;
476
477 /* adjust pointer for proper alignment and offset */
478 memblock = ALIGN_PTR(temp, alignment, offset);
479
480 /* Save the real allocation address below returned address */
481 /* so it can be found later to free. */
482 saved = SAVED_PTR(memblock);
483
484 new_padding = (char *)memblock - (char *)temp;
485
486 /*
487 Memory layout of old block is as follows:
488 +-------+---------------------+-+--------------------------+-----------+
489 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
490 +-------+---------------------+-+--------------------------+-----------+
491 ^ ^ ^
492 | | |
493 *saved saved memblock
494
495 Memory layout of new block is as follows:
496 +-------+-----------------------------+-+----------------------+-------+
497 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
498 +-------+-----------------------------+-+----------------------+-------+
499 ^ ^ ^
500 | | |
501 temp saved memblock
502
503 However, in the new block, actual data is still written as follows
504 (because it was copied by MSVCRT_realloc):
505 +-------+---------------------+--------------------------------+-------+
506 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
507 +-------+---------------------+--------------------------------+-------+
508 ^ ^ ^
509 | | |
510 temp saved memblock
511
512 Therefore, min(old_size,size) bytes of actual data have to be moved
513 from the offset they were at in the old block (temp + old_padding),
514 to the offset they have to be in the new block (temp + new_padding == memblock).
515 */
516 if (new_padding != old_padding)
517 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
518
519 *saved = temp;
520
521 return memblock;
522 }
523
524 /*********************************************************************
525 * _aligned_realloc (MSVCRT.@)
526 */
527 void * CDECL _aligned_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
528 {
529 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
530 return _aligned_offset_realloc(memblock, size, alignment, 0);
531 }
532
533 /*********************************************************************
534 * memmove_s (MSVCRT.@)
535 */
536 int CDECL memmove_s(void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
537 {
538 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
539
540 if(!count)
541 return 0;
542
543 if(!dest || !src) {
544 if(dest)
545 memset(dest, 0, numberOfElements);
546
547 *MSVCRT__errno() = MSVCRT_EINVAL;
548 return MSVCRT_EINVAL;
549 }
550
551 if(count > numberOfElements) {
552 memset(dest, 0, numberOfElements);
553
554 *MSVCRT__errno() = MSVCRT_ERANGE;
555 return MSVCRT_ERANGE;
556 }
557
558 memmove(dest, src, count);
559 return 0;
560 }
561
562 /*********************************************************************
563 * strncpy_s (MSVCRT.@)
564 */
565 int CDECL strncpy_s(char *dest, MSVCRT_size_t numberOfElements,
566 const char *src, MSVCRT_size_t count)
567 {
568 MSVCRT_size_t i, end;
569
570 TRACE("(%s %lu %s %lu)\n", dest, numberOfElements, src, count);
571
572 if(!count)
573 return 0;
574
575 if(!dest || !src || !numberOfElements) {
576 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
577 *MSVCRT__errno() = MSVCRT_EINVAL;
578 return MSVCRT_EINVAL;
579 }
580
581 if(count!=MSVCRT__TRUNCATE && count<numberOfElements)
582 end = count;
583 else
584 end = numberOfElements-1;
585
586 for(i=0; i<end && src[i]; i++)
587 dest[i] = src[i];
588
589 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
590 dest[i] = '\0';
591 return 0;
592 }
593
594 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
595 dest[0] = '\0';
596 *MSVCRT__errno() = MSVCRT_EINVAL;
597 return MSVCRT_EINVAL;
598 }
599
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.