ReactOS 0.4.16-dev-2104-gb84fa49
heap.c File Reference
#include <malloc.h>
#include "msvcrt.h"
#include "mtdll.h"
#include "wine/debug.h"
Include dependency graph for heap.c:

Go to the source code of this file.

Macros

#define LOCK_HEAP   _lock( _HEAP_LOCK )
 
#define UNLOCK_HEAP   _unlock( _HEAP_LOCK )
 
#define SAVED_PTR(x)
 
#define ALIGN_PTR(ptr, alignment, offset)
 
#define SB_HEAP_ALIGN   16
 

Typedefs

typedef int(CDECLMSVCRT_new_handler_func) (size_t size)
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (msvcrt)
 
static voidmsvcrt_heap_alloc (DWORD flags, size_t size)
 
static voidmsvcrt_heap_realloc (DWORD flags, void *ptr, size_t size)
 
static BOOL msvcrt_heap_free (void *ptr)
 
static size_t msvcrt_heap_size (void *ptr)
 
int CDECL _callnewh (size_t size)
 
void *CDECL DECLSPEC_HOTPATCH operator_new (size_t size)
 
void *CDECL operator_new_dbg (size_t size, int type, const char *file, int line)
 
void CDECL DECLSPEC_HOTPATCH operator_delete (void *mem)
 
MSVCRT_new_handler_func CDECL _query_new_handler (void)
 
int CDECL _query_new_mode (void)
 
MSVCRT_new_handler_func CDECL _set_new_handler (MSVCRT_new_handler_func func)
 
MSVCRT_new_handler_func CDECL set_new_handler (void *func)
 
int CDECL _set_new_mode (int mode)
 
void *CDECL _expand (void *mem, size_t size)
 
int CDECL _heapchk (void)
 
int CDECL _heapmin (void)
 
int CDECL _heapwalk (_HEAPINFO *next)
 
int CDECL _heapset (unsigned int value)
 
int CDECL _heapadd (void *mem, size_t size)
 
intptr_t CDECL _get_heap_handle (void)
 
size_t CDECL _msize (void *mem)
 
void *CDECL DECLSPEC_HOTPATCH calloc (size_t count, size_t size)
 
void CDECL DECLSPEC_HOTPATCH free (void *ptr)
 
void *CDECL malloc (size_t size)
 
void *CDECL DECLSPEC_HOTPATCH realloc (void *ptr, size_t size)
 
unsigned int *CDECL __p__amblksiz (void)
 
size_t CDECL _get_sbh_threshold (void)
 
int CDECL _set_sbh_threshold (size_t threshold)
 
void CDECL _aligned_free (void *memblock)
 
void *CDECL _aligned_offset_malloc (size_t size, size_t alignment, size_t offset)
 
void *CDECL _aligned_malloc (size_t size, size_t alignment)
 
void *CDECL _aligned_offset_realloc (void *memblock, size_t size, size_t alignment, size_t offset)
 
void *CDECL _aligned_realloc (void *memblock, size_t size, size_t alignment)
 
int CDECL memmove_s (void *dest, size_t numberOfElements, const void *src, size_t count)
 
int CDECL memcpy_s (void *dest, size_t numberOfElements, const void *src, size_t count)
 
BOOL msvcrt_init_heap (void)
 
void msvcrt_destroy_heap (void)
 

Variables

static HANDLE heap
 
static HANDLE sb_heap
 
static MSVCRT_new_handler_func MSVCRT_new_handler
 
static LONG MSVCRT_new_mode
 
static unsigned int MSVCRT_amblksiz = 16
 
static size_t MSVCRT_sbh_threshold = 0
 

Macro Definition Documentation

◆ ALIGN_PTR

#define ALIGN_PTR (   ptr,
  alignment,
  offset 
)
Value:
((void *) \
((((DWORD_PTR)((char *)ptr + alignment + sizeof(void *) + offset)) & \
~(alignment - 1)) - offset))
_Check_return_ _Ret_maybenull_ _In_ size_t alignment
Definition: align.cpp:48
GLintptr offset
Definition: glext.h:5920
static PVOID ptr
Definition: dispmode.c:27
uint32_t DWORD_PTR
Definition: typedefs.h:65

Definition at line 38 of file heap.c.

◆ LOCK_HEAP

#define LOCK_HEAP   _lock( _HEAP_LOCK )

Definition at line 32 of file heap.c.

◆ SAVED_PTR

#define SAVED_PTR (   x)
Value:
((void *)((DWORD_PTR)((char *)x - sizeof(void *)) & \
~(sizeof(void *) - 1)))
GLint GLint GLint GLint GLint x
Definition: gl.h:1548

Definition at line 36 of file heap.c.

◆ SB_HEAP_ALIGN

#define SB_HEAP_ALIGN   16

Definition at line 42 of file heap.c.

◆ UNLOCK_HEAP

#define UNLOCK_HEAP   _unlock( _HEAP_LOCK )

Definition at line 33 of file heap.c.

Typedef Documentation

◆ MSVCRT_new_handler_func

typedef int(CDECL * MSVCRT_new_handler_func) (size_t size)

Definition at line 46 of file heap.c.

Function Documentation

◆ __p__amblksiz()

unsigned int *CDECL __p__amblksiz ( void  )

Definition at line 507 of file heap.c.

508{
509 return &MSVCRT_amblksiz;
510}
static unsigned int MSVCRT_amblksiz
Definition: heap.c:52

◆ _aligned_free()

void CDECL _aligned_free ( void memblock)

Definition at line 546 of file heap.c.

547{
548 TRACE("(%p)\n", memblock);
549
550 if (memblock)
551 {
552 void **saved = SAVED_PTR(memblock);
553 free(*saved);
554 }
555}
#define free
Definition: debug_ros.c:5
#define SAVED_PTR(x)
Definition: heap.c:36
#define TRACE(s)
Definition: solgame.cpp:4

Referenced by _aligned_offset_realloc().

◆ _aligned_malloc()

void *CDECL _aligned_malloc ( size_t  size,
size_t  alignment 
)

Definition at line 603 of file heap.c.

604{
605 TRACE("(%Iu, %Iu)\n", size, alignment);
607}
void *CDECL _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
Definition: heap.c:560
GLsizeiptr size
Definition: glext.h:5919

◆ _aligned_offset_malloc()

void *CDECL _aligned_offset_malloc ( size_t  size,
size_t  alignment,
size_t  offset 
)

Definition at line 560 of file heap.c.

561{
562 void *memblock, *temp, **saved;
563 TRACE("(%Iu, %Iu, %Iu)\n", size, alignment, offset);
564
565 /* alignment must be a power of 2 */
566 if ((alignment & (alignment - 1)) != 0)
567 {
568 *_errno() = EINVAL;
569 return NULL;
570 }
571
572 /* offset must be less than size */
573 if (offset && offset >= size)
574 {
575 *_errno() = EINVAL;
576 return NULL;
577 }
578
579 /* don't align to less than void pointer size */
580 if (alignment < sizeof(void *))
581 alignment = sizeof(void *);
582
583 /* allocate enough space for void pointer and alignment */
584 temp = malloc(size + alignment + sizeof(void *));
585
586 if (!temp)
587 return NULL;
588
589 /* adjust pointer for proper alignment and offset */
590 memblock = ALIGN_PTR(temp, alignment, offset);
591
592 /* Save the real allocation address below returned address */
593 /* so it can be found later to free. */
594 saved = SAVED_PTR(memblock);
595 *saved = temp;
596
597 return memblock;
598}
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
int *CDECL _errno(void)
Definition: errno.c:215
#define ALIGN_PTR(ptr, alignment, offset)
Definition: heap.c:38
#define EINVAL
Definition: errno.h:44
static calc_node_t temp
Definition: rpn_ieee.c:38

Referenced by _aligned_malloc(), and _aligned_offset_realloc().

◆ _aligned_offset_realloc()

void *CDECL _aligned_offset_realloc ( void memblock,
size_t  size,
size_t  alignment,
size_t  offset 
)

Definition at line 612 of file heap.c.

614{
615 void * temp, **saved;
616 size_t old_padding, new_padding, old_size;
617 TRACE("(%p, %Iu, %Iu, %Iu)\n", memblock, size, alignment, offset);
618
619 if (!memblock)
621
622 /* alignment must be a power of 2 */
623 if ((alignment & (alignment - 1)) != 0)
624 {
625 *_errno() = EINVAL;
626 return NULL;
627 }
628
629 /* offset must be less than size */
630 if (offset >= size)
631 {
632 *_errno() = EINVAL;
633 return NULL;
634 }
635
636 if (size == 0)
637 {
638 _aligned_free(memblock);
639 return NULL;
640 }
641
642 /* don't align to less than void pointer size */
643 if (alignment < sizeof(void *))
644 alignment = sizeof(void *);
645
646 /* make sure alignment and offset didn't change */
647 saved = SAVED_PTR(memblock);
648 if (memblock != ALIGN_PTR(*saved, alignment, offset))
649 {
650 *_errno() = EINVAL;
651 return NULL;
652 }
653
654 old_padding = (char *)memblock - (char *)*saved;
655
656 /* Get previous size of block */
657 old_size = _msize(*saved);
658 if (old_size == -1)
659 {
660 /* It seems this function was called with an invalid pointer. Bail out. */
661 return NULL;
662 }
663
664 /* Adjust old_size to get amount of actual data in old block. */
665 if (old_size < old_padding)
666 {
667 /* Shouldn't happen. Something's weird, so bail out. */
668 return NULL;
669 }
670 old_size -= old_padding;
671
672 temp = realloc(*saved, size + alignment + sizeof(void *));
673
674 if (!temp)
675 return NULL;
676
677 /* adjust pointer for proper alignment and offset */
678 memblock = ALIGN_PTR(temp, alignment, offset);
679
680 /* Save the real allocation address below returned address */
681 /* so it can be found later to free. */
682 saved = SAVED_PTR(memblock);
683
684 new_padding = (char *)memblock - (char *)temp;
685
686/*
687 Memory layout of old block is as follows:
688 +-------+---------------------+-+--------------------------+-----------+
689 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
690 +-------+---------------------+-+--------------------------+-----------+
691 ^ ^ ^
692 | | |
693 *saved saved memblock
694
695 Memory layout of new block is as follows:
696 +-------+-----------------------------+-+----------------------+-------+
697 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
698 +-------+-----------------------------+-+----------------------+-------+
699 ^ ^ ^
700 | | |
701 temp saved memblock
702
703 However, in the new block, actual data is still written as follows
704 (because it was copied by realloc):
705 +-------+---------------------+--------------------------------+-------+
706 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
707 +-------+---------------------+--------------------------------+-------+
708 ^ ^ ^
709 | | |
710 temp saved memblock
711
712 Therefore, min(old_size,size) bytes of actual data have to be moved
713 from the offset they were at in the old block (temp + old_padding),
714 to the offset they have to be in the new block (temp + new_padding == memblock).
715*/
716 if (new_padding != old_padding)
717 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
718
719 *saved = temp;
720
721 return memblock;
722}
#define realloc
Definition: debug_ros.c:6
void CDECL _aligned_free(void *memblock)
Definition: heap.c:546
size_t CDECL _msize(void *mem)
Definition: heap.c:354
size_t const old_size
Definition: expand.cpp:65
#define memmove(s1, s2, n)
Definition: mkisofs.h:881

Referenced by _aligned_realloc().

◆ _aligned_realloc()

void *CDECL _aligned_realloc ( void memblock,
size_t  size,
size_t  alignment 
)

Definition at line 727 of file heap.c.

728{
729 TRACE("(%p, %Iu, %Iu)\n", memblock, size, alignment);
730 return _aligned_offset_realloc(memblock, size, alignment, 0);
731}
void *CDECL _aligned_offset_realloc(void *memblock, size_t size, size_t alignment, size_t offset)
Definition: heap.c:612

◆ _callnewh()

int CDECL _callnewh ( size_t  size)

Definition at line 131 of file heap.c.

132{
133 int ret = 0;
135 if(handler)
136 ret = (*handler)(size) ? 1 : 0;
137 return ret;
138}
UINT(* handler)(MSIPACKAGE *)
Definition: action.c:7512
int(CDECL * MSVCRT_new_handler_func)(size_t size)
Definition: heap.c:46
static MSVCRT_new_handler_func MSVCRT_new_handler
Definition: heap.c:48
return ret
Definition: mutex.c:146

Referenced by __declspec(), for(), heap_alloc_dbg(), malloc(), and operator_new().

◆ _expand()

void *CDECL _expand ( void mem,
size_t  size 
)

Definition at line 236 of file heap.c.

237{
239}
static void * msvcrt_heap_realloc(DWORD flags, void *ptr, size_t size)
Definition: heap.c:74
#define HEAP_REALLOC_IN_PLACE_ONLY
Definition: nt_native.h:1699
Definition: mem.c:349

Referenced by _aligned_offset_realloc_base().

◆ _get_heap_handle()

intptr_t CDECL _get_heap_handle ( void  )

Definition at line 346 of file heap.c.

347{
348 return (intptr_t)heap;
349}
static HANDLE heap
Definition: heap.c:44
int intptr_t
Definition: corecrt.h:176

Referenced by test__get_heap_handle().

◆ _get_sbh_threshold()

size_t CDECL _get_sbh_threshold ( void  )

Definition at line 515 of file heap.c.

516{
518}
static size_t MSVCRT_sbh_threshold
Definition: heap.c:54

◆ _heapadd()

int CDECL _heapadd ( void mem,
size_t  size 
)

Definition at line 336 of file heap.c.

337{
338 TRACE("(%p,%Iu) unsupported in Win32\n", mem,size);
339 *_errno() = ENOSYS;
340 return -1;
341}
#define ENOSYS
Definition: errno.h:60

◆ _heapchk()

int CDECL _heapchk ( void  )

Definition at line 244 of file heap.c.

245{
246 if (!HeapValidate(heap, 0, NULL) ||
247 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
248 {
250 return _HEAPBADNODE;
251 }
252 return _HEAPOK;
253}
static HANDLE sb_heap
Definition: heap.c:44
#define _HEAPOK
Definition: malloc.h:28
#define _HEAPBADNODE
Definition: malloc.h:30
BOOL WINAPI HeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
Definition: heapmem.c:156
#define msvcrt_set_errno
Definition: heap.c:50
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

◆ _heapmin()

int CDECL _heapmin ( void  )

Definition at line 258 of file heap.c.

259{
260 if (!HeapCompact( heap, 0 ) ||
261 (sb_heap && !HeapCompact( sb_heap, 0 )))
262 {
265 return -1;
266 }
267 return 0;
268}
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:102
SIZE_T WINAPI HeapCompact(HANDLE hHeap, DWORD dwFlags)
Definition: heapmem.c:145

◆ _heapset()

int CDECL _heapset ( unsigned int  value)

Definition at line 317 of file heap.c.

318{
319 int retval;
321
322 memset( &heap, 0, sizeof(heap) );
323 LOCK_HEAP;
324 while ((retval = _heapwalk(&heap)) == _HEAPOK)
325 {
326 if (heap._useflag == _FREEENTRY)
327 memset(heap._pentry, value, heap._size);
328 }
330 return retval == _HEAPEND ? _HEAPOK : retval;
331}
#define UNLOCK_HEAP
Definition: heap.c:33
int CDECL _heapwalk(_HEAPINFO *next)
Definition: heap.c:273
#define LOCK_HEAP
Definition: heap.c:32
#define _HEAPEND
Definition: malloc.h:31
#define _FREEENTRY
Definition: malloc.h:34
#define memset(x, y, z)
Definition: compat.h:39
Definition: pdh_main.c:96
int retval
Definition: wcstombs.cpp:91

◆ _heapwalk()

int CDECL _heapwalk ( _HEAPINFO next)

Definition at line 273 of file heap.c.

274{
276
277 if (sb_heap)
278 FIXME("small blocks heap not supported\n");
279
280 LOCK_HEAP;
281 phe.lpData = next->_pentry;
282 phe.cbData = next->_size;
283 phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
284
285 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
286 !HeapValidate( heap, 0, phe.lpData ))
287 {
290 return _HEAPBADNODE;
291 }
292
293 do
294 {
295 if (!HeapWalk( heap, &phe ))
296 {
299 return _HEAPEND;
301 if (!phe.lpData)
302 return _HEAPBADBEGIN;
303 return _HEAPBADNODE;
304 }
306
308 next->_pentry = phe.lpData;
309 next->_size = phe.cbData;
311 return _HEAPOK;
312}
#define FIXME(fmt,...)
Definition: precomp.h:53
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:105
#define _HEAPBADBEGIN
Definition: malloc.h:29
#define _USEDENTRY
Definition: malloc.h:35
BOOL WINAPI HeapWalk(HANDLE hHeap, LPPROCESS_HEAP_ENTRY lpEntry)
Definition: heapmem.c:291
#define PROCESS_HEAP_UNCOMMITTED_RANGE
Definition: minwinbase.h:76
#define PROCESS_HEAP_ENTRY_BUSY
Definition: minwinbase.h:77
#define PROCESS_HEAP_REGION
Definition: minwinbase.h:75
static unsigned __int64 next
Definition: rand_nt.c:6
Definition: minwinbase.h:361
WORD wFlags
Definition: minwinbase.h:366
PVOID lpData
Definition: minwinbase.h:362
DWORD cbData
Definition: minwinbase.h:363

Referenced by _heapset().

◆ _msize()

size_t CDECL _msize ( void mem)

Definition at line 354 of file heap.c.

355{
356 size_t size = msvcrt_heap_size(mem);
357 if (size == ~(size_t)0)
358 {
359 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
360 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
361 }
362 return size;
363}
#define WARN(fmt,...)
Definition: precomp.h:61
static size_t msvcrt_heap_size(void *ptr)
Definition: heap.c:117

Referenced by _aligned_offset_realloc(), and test__o_malloc().

◆ _query_new_handler()

MSVCRT_new_handler_func CDECL _query_new_handler ( void  )

Definition at line 187 of file heap.c.

188{
189 return MSVCRT_new_handler;
190}

◆ _query_new_mode()

int CDECL _query_new_mode ( void  )

Definition at line 196 of file heap.c.

197{
198 return MSVCRT_new_mode;
199}
static LONG MSVCRT_new_mode
Definition: heap.c:49

Referenced by __declspec(), for(), and heap_alloc_dbg().

◆ _set_new_handler()

Definition at line 204 of file heap.c.

205{
206 MSVCRT_new_handler_func old_handler;
207 LOCK_HEAP;
208 old_handler = MSVCRT_new_handler;
211 return old_handler;
212}
GLenum func
Definition: glext.h:6028

Referenced by set_new_handler().

◆ _set_new_mode()

int CDECL _set_new_mode ( int  mode)

Definition at line 227 of file heap.c.

228{
229 if(!MSVCRT_CHECK_PMT(mode == 0 || mode == 1)) return -1;
231}
#define InterlockedExchange
Definition: armddk.h:54
#define MSVCRT_CHECK_PMT(x)
Definition: msvcrt.h:378
GLenum mode
Definition: glext.h:6217

Referenced by __getmainargs(), and __wgetmainargs().

◆ _set_sbh_threshold()

int CDECL _set_sbh_threshold ( size_t  threshold)

Definition at line 523 of file heap.c.

524{
525#ifdef _WIN64
526 return 0;
527#else
528 if(threshold > 1016)
529 return 0;
530
531 if(!sb_heap)
532 {
533 sb_heap = HeapCreate(0, 0, 0);
534 if(!sb_heap)
535 return 0;
536 }
537
538 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
539 return 1;
540#endif
541}
HANDLE WINAPI HeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize)
Definition: heapmem.c:45

◆ calloc()

void *CDECL DECLSPEC_HOTPATCH calloc ( size_t  count,
size_t  size 
)

Definition at line 386 of file heap.c.

387{
388 size_t bytes = count*size;
389
390 if (size && bytes / size != count)
391 {
392 *_errno() = ENOMEM;
393 return NULL;
394 }
395
397}
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
static void * msvcrt_heap_alloc(DWORD flags, size_t size)
Definition: heap.c:56
#define ENOMEM
Definition: errno.h:35
GLuint GLuint GLsizei count
Definition: gl.h:1545

◆ free()

void CDECL DECLSPEC_HOTPATCH free ( void ptr)

Definition at line 412 of file heap.c.

413{
415}
static BOOL msvcrt_heap_free(void *ptr)
Definition: heap.c:106

◆ malloc()

void *CDECL malloc ( size_t  size)

Definition at line 430 of file heap.c.

431{
432 void *ret;
433
434 do
435 {
437 if (ret || !MSVCRT_new_mode)
438 break;
439 } while(_callnewh(size));
440
441 if (!ret)
442 *_errno() = ENOMEM;
443 return ret;
444}
int CDECL _callnewh(size_t size)
Definition: heap.c:131

◆ memcpy_s()

int CDECL memcpy_s ( void dest,
size_t  numberOfElements,
const void src,
size_t  count 
)

Definition at line 779 of file heap.c.

780{
781 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
782
783 if(!count)
784 return 0;
785
786 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
787 if (!MSVCRT_CHECK_PMT(src != NULL))
788 {
790 return EINVAL;
791 }
793 {
795 return ERANGE;
796 }
797
799 return 0;
800}
#define ERANGE
Definition: errno.h:55
#define MSVCRT_CHECK_PMT_ERR(x, err)
Definition: msvcrt.h:377
GLenum src
Definition: glext.h:6340
static size_t numberOfElements
Definition: string.c:98
static char * dest
Definition: rtl.c:135

Referenced by _fread_nolock_s(), _wcsrtombs_internal(), __crt_strtox::big_integer::big_integer(), common_assert_to_message_box_build_string(), __crt_win32_buffer< Character, ResizePolicy >::detach(), __crt_strtox::big_integer::operator=(), setmbcp_internal(), and throw().

◆ memmove_s()

int CDECL memmove_s ( void dest,
size_t  numberOfElements,
const void src,
size_t  count 
)

Definition at line 736 of file heap.c.

737{
738 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
739
740 if(!count)
741 return 0;
742
743 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
744 if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
746
748 return 0;
749}

◆ msvcrt_destroy_heap()

void msvcrt_destroy_heap ( void  )

Definition at line 840 of file heap.c.

841{
842#if _MSVCR_VER <= 100 && !defined(__REACTOS__)
844#endif
845 if(sb_heap)
847}
BOOL WINAPI HeapDestroy(HANDLE hHeap)
Definition: heapmem.c:85

Referenced by DllMain().

◆ msvcrt_heap_alloc()

static void * msvcrt_heap_alloc ( DWORD  flags,
size_t  size 
)
static

Definition at line 56 of file heap.c.

57{
59 {
60 void *memblock, *temp, **saved;
61
62 temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN);
63 if(!temp) return NULL;
64
65 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
66 saved = SAVED_PTR(memblock);
67 *saved = temp;
68 return memblock;
69 }
70
71 return HeapAlloc(heap, flags, size);
72}
#define HeapAlloc
Definition: compat.h:733
#define SB_HEAP_ALIGN
Definition: heap.c:42
GLbitfield flags
Definition: glext.h:7161

Referenced by calloc(), malloc(), and operator_new().

◆ msvcrt_heap_free()

static BOOL msvcrt_heap_free ( void ptr)
static

Definition at line 106 of file heap.c.

107{
108 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
109 {
110 void **saved = SAVED_PTR(ptr);
111 return HeapFree(sb_heap, 0, *saved);
112 }
113
114 return HeapFree(heap, 0, ptr);
115}
#define HeapFree(x, y, z)
Definition: compat.h:735

Referenced by free(), and operator_delete().

◆ msvcrt_heap_realloc()

static void * msvcrt_heap_realloc ( DWORD  flags,
void ptr,
size_t  size 
)
static

Definition at line 74 of file heap.c.

75{
76 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
77 {
78 /* TODO: move data to normal heap if it exceeds sbh_threshold limit */
79 void *memblock, *temp, **saved;
80 size_t old_padding, new_padding, old_size;
81
82 saved = SAVED_PTR(ptr);
83 old_padding = (char*)ptr - (char*)*saved;
84 old_size = HeapSize(sb_heap, 0, *saved);
85 if(old_size == -1)
86 return NULL;
87 old_size -= old_padding;
88
89 temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN);
90 if(!temp) return NULL;
91
92 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
93 saved = SAVED_PTR(memblock);
94 new_padding = (char*)memblock - (char*)temp;
95
96 if(new_padding != old_padding)
97 memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size);
98
99 *saved = temp;
100 return memblock;
101 }
102
103 return HeapReAlloc(heap, flags, ptr, size);
104}
#define HeapReAlloc
Definition: compat.h:734
SIZE_T WINAPI HeapSize(HANDLE, DWORD, LPCVOID)

Referenced by _expand(), and realloc().

◆ msvcrt_heap_size()

static size_t msvcrt_heap_size ( void ptr)
static

Definition at line 117 of file heap.c.

118{
119 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
120 {
121 void **saved = SAVED_PTR(ptr);
122 return HeapSize(sb_heap, 0, *saved);
123 }
124
125 return HeapSize(heap, 0, ptr);
126}

Referenced by _msize().

◆ msvcrt_init_heap()

BOOL msvcrt_init_heap ( void  )

Definition at line 830 of file heap.c.

831{
832#if _MSVCR_VER <= 100 && !defined(__REACTOS__)
833 heap = HeapCreate(0, 0, 0);
834#else
836#endif
837 return heap != NULL;
838}
#define GetProcessHeap()
Definition: compat.h:736

Referenced by DllMain().

◆ operator_delete()

◆ operator_new()

void *CDECL DECLSPEC_HOTPATCH operator_new ( size_t  size)

Definition at line 143 of file heap.c.

144{
145 void *retval;
146
147 do
148 {
150 if(retval)
151 {
152 TRACE("(%Iu) returning %p\n", size, retval);
153 return retval;
154 }
155 } while(_callnewh(size));
156
157 TRACE("(%Iu) out of memory\n", size);
158#if _MSVCR_VER >= 80
159 throw_bad_alloc();
160#endif
161 return NULL;
162}

Referenced by operator_new_dbg().

◆ operator_new_dbg()

void *CDECL operator_new_dbg ( size_t  size,
int  type,
const char file,
int  line 
)

Definition at line 168 of file heap.c.

169{
170 return operator_new( size );
171}
void *CDECL DECLSPEC_HOTPATCH operator_new(size_t size)
Definition: heap.c:143

◆ realloc()

void *CDECL DECLSPEC_HOTPATCH realloc ( void ptr,
size_t  size 
)

Definition at line 459 of file heap.c.

460{
461 if (!ptr) return malloc(size);
462 if (size) return msvcrt_heap_realloc(0, ptr, size);
463 free(ptr);
464 return NULL;
465}

◆ set_new_handler()

MSVCRT_new_handler_func CDECL set_new_handler ( void func)

Definition at line 217 of file heap.c.

218{
219 TRACE("(%p)\n",func);
221 return NULL;
222}
MSVCRT_new_handler_func CDECL _set_new_handler(MSVCRT_new_handler_func func)
Definition: heap.c:204

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( msvcrt  )

Variable Documentation

◆ heap

◆ MSVCRT_amblksiz

unsigned int MSVCRT_amblksiz = 16
static

Definition at line 52 of file heap.c.

Referenced by __p__amblksiz().

◆ MSVCRT_new_handler

MSVCRT_new_handler_func MSVCRT_new_handler
static

Definition at line 48 of file heap.c.

Referenced by _callnewh(), _query_new_handler(), and _set_new_handler().

◆ MSVCRT_new_mode

LONG MSVCRT_new_mode
static

Definition at line 49 of file heap.c.

Referenced by _query_new_mode(), _set_new_mode(), and malloc().

◆ MSVCRT_sbh_threshold

size_t MSVCRT_sbh_threshold = 0
static

Definition at line 54 of file heap.c.

Referenced by _get_sbh_threshold(), _set_sbh_threshold(), and msvcrt_heap_alloc().

◆ sb_heap