ReactOS 0.4.16-dev-87-g3dfbe52
heap.c File Reference
#include <precomp.h>
#include <malloc.h>
Include dependency graph for heap.c:

Go to the source code of this file.

Macros

#define MSVCRT_size_t   size_t
 
#define MSVCRT_intptr_t   intptr_t
 
#define MSVCRT_wchar_t   wchar_t
 
#define MSVCRT__HEAPBADNODE   _HEAPBADNODE
 
#define MSVCRT__HEAPOK   _HEAPOK
 
#define MSVCRT__HEAPEND   _HEAPEND
 
#define MSVCRT__FREEENTRY   _FREEENTRY
 
#define MSVCRT__USEDENTRY   _USEDENTRY
 
#define MSVCRT__HEAPBADBEGIN   _HEAPBADBEGIN
 
#define MSVCRT_EINVAL   EINVAL
 
#define MSVCRT_ENOSYS   ENOSYS
 
#define MSVCRT_ENOMEM   ENOMEM
 
#define MSVCRT_ERANGE   ERANGE
 
#define MSVCRT__TRUNCATE   _TRUNCATE
 
#define MSVCRT__heapinfo   _heapinfo
 
#define MSVCRT__errno   _errno
 
#define MSVCRT_calloc   calloc
 
#define MSVCRT_malloc   malloc
 
#define MSVCRT_realloc   realloc
 
#define MSVCRT_free   free
 
#define MSVCRT_memcpy_s   memcpy_s
 
#define MSVCRT_memmove_s   memmove_s
 
#define MSVCRT_strncpy_s   strncpy_s
 
#define msvcrt_set_errno   _dosmaperr
 
#define LOCK_HEAP   _mlock( _HEAP_LOCK )
 
#define UNLOCK_HEAP   _munlock( _HEAP_LOCK )
 
#define SAVED_PTR(x)
 
#define ALIGN_PTR(ptr, alignment, offset)
 
#define SB_HEAP_ALIGN   16
 

Typedefs

typedef int(CDECLMSVCRT_new_handler_func) (MSVCRT_size_t size)
 

Functions

static voidmsvcrt_heap_alloc (DWORD flags, MSVCRT_size_t size)
 
static voidmsvcrt_heap_realloc (DWORD flags, void *ptr, MSVCRT_size_t size)
 
static BOOL msvcrt_heap_free (void *ptr)
 
static MSVCRT_size_t msvcrt_heap_size (void *ptr)
 
void *CDECL DECLSPEC_HOTPATCH MSVCRT_operator_new (MSVCRT_size_t size)
 
void *CDECL MSVCRT_operator_new_dbg (MSVCRT_size_t size, int type, const char *file, int line)
 
void CDECL DECLSPEC_HOTPATCH MSVCRT_operator_delete (void *mem)
 
MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler (void)
 
int CDECL MSVCRT__query_new_mode (void)
 
MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler (MSVCRT_new_handler_func func)
 
MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler (void *func)
 
int CDECL MSVCRT__set_new_mode (int mode)
 
int CDECL _callnewh (MSVCRT_size_t size)
 
void *CDECL _expand (void *mem, MSVCRT_size_t size)
 
int CDECL _heapchk (void)
 
int CDECL _heapmin (void)
 
int CDECL _heapwalk (struct MSVCRT__heapinfo *next)
 
int CDECL _heapset (unsigned int value)
 
int CDECL _heapadd (void *mem, MSVCRT_size_t size)
 
MSVCRT_intptr_t CDECL _get_heap_handle (void)
 
MSVCRT_size_t CDECL _msize (void *mem)
 
void *CDECL DECLSPEC_HOTPATCH MSVCRT_calloc (MSVCRT_size_t count, MSVCRT_size_t size)
 
void CDECL DECLSPEC_HOTPATCH MSVCRT_free (void *ptr)
 
void *CDECL MSVCRT_malloc (MSVCRT_size_t size)
 
void *CDECL DECLSPEC_HOTPATCH MSVCRT_realloc (void *ptr, MSVCRT_size_t size)
 
unsigned int *CDECL __p__amblksiz (void)
 
MSVCRT_size_t CDECL _get_sbh_threshold (void)
 
int CDECL _set_sbh_threshold (MSVCRT_size_t threshold)
 
void CDECL _aligned_free (void *memblock)
 
void *CDECL _aligned_offset_malloc (MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
 
void *CDECL _aligned_malloc (MSVCRT_size_t size, MSVCRT_size_t alignment)
 
void *CDECL _aligned_offset_realloc (void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
 
void *CDECL _aligned_realloc (void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment)
 
int CDECL MSVCRT_memmove_s (void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
 
int CDECL MSVCRT_memcpy_s (void *dest, MSVCRT_size_t numberOfElements, const void *src, MSVCRT_size_t count)
 
int CDECL MSVCRT_strncpy_s (char *dest, MSVCRT_size_t numberOfElements, const char *src, MSVCRT_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 int MSVCRT_new_mode
 
static unsigned int MSVCRT_amblksiz = 16
 
static MSVCRT_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))
GLintptr offset
Definition: glext.h:5920
static PVOID ptr
Definition: dispmode.c:27
uint32_t DWORD_PTR
Definition: typedefs.h:65

Definition at line 59 of file heap.c.

◆ LOCK_HEAP

#define LOCK_HEAP   _mlock( _HEAP_LOCK )

Definition at line 53 of file heap.c.

◆ MSVCRT__errno

#define MSVCRT__errno (   void)    _errno

Definition at line 42 of file heap.c.

◆ MSVCRT__FREEENTRY

#define MSVCRT__FREEENTRY   _FREEENTRY

Definition at line 33 of file heap.c.

◆ MSVCRT__HEAPBADBEGIN

#define MSVCRT__HEAPBADBEGIN   _HEAPBADBEGIN

Definition at line 35 of file heap.c.

◆ MSVCRT__HEAPBADNODE

#define MSVCRT__HEAPBADNODE   _HEAPBADNODE

Definition at line 30 of file heap.c.

◆ MSVCRT__HEAPEND

#define MSVCRT__HEAPEND   _HEAPEND

Definition at line 32 of file heap.c.

◆ MSVCRT__heapinfo

Definition at line 41 of file heap.c.

◆ MSVCRT__HEAPOK

#define MSVCRT__HEAPOK   _HEAPOK

Definition at line 31 of file heap.c.

◆ MSVCRT__TRUNCATE

#define MSVCRT__TRUNCATE   _TRUNCATE

Definition at line 40 of file heap.c.

◆ MSVCRT__USEDENTRY

#define MSVCRT__USEDENTRY   _USEDENTRY

Definition at line 34 of file heap.c.

◆ MSVCRT_calloc

#define MSVCRT_calloc   calloc

Definition at line 43 of file heap.c.

◆ MSVCRT_EINVAL

#define MSVCRT_EINVAL   EINVAL

Definition at line 36 of file heap.c.

◆ MSVCRT_ENOMEM

#define MSVCRT_ENOMEM   ENOMEM

Definition at line 38 of file heap.c.

◆ MSVCRT_ENOSYS

#define MSVCRT_ENOSYS   ENOSYS

Definition at line 37 of file heap.c.

◆ MSVCRT_ERANGE

#define MSVCRT_ERANGE   ERANGE

Definition at line 39 of file heap.c.

◆ MSVCRT_free

#define MSVCRT_free   free

Definition at line 46 of file heap.c.

◆ MSVCRT_intptr_t

Definition at line 28 of file heap.c.

◆ MSVCRT_malloc

#define MSVCRT_malloc   malloc

Definition at line 44 of file heap.c.

◆ MSVCRT_memcpy_s

#define MSVCRT_memcpy_s   memcpy_s

Definition at line 47 of file heap.c.

◆ MSVCRT_memmove_s

#define MSVCRT_memmove_s   memmove_s

Definition at line 48 of file heap.c.

◆ MSVCRT_realloc

#define MSVCRT_realloc   realloc

Definition at line 45 of file heap.c.

◆ msvcrt_set_errno

#define msvcrt_set_errno   _dosmaperr

Definition at line 50 of file heap.c.

◆ MSVCRT_size_t

static MSVCRT_size_t   size_t

Definition at line 27 of file heap.c.

◆ MSVCRT_strncpy_s

#define MSVCRT_strncpy_s   strncpy_s

Definition at line 49 of file heap.c.

◆ MSVCRT_wchar_t

Definition at line 29 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 57 of file heap.c.

◆ SB_HEAP_ALIGN

#define SB_HEAP_ALIGN   16

Definition at line 63 of file heap.c.

◆ UNLOCK_HEAP

#define UNLOCK_HEAP   _munlock( _HEAP_LOCK )

Definition at line 54 of file heap.c.

Typedef Documentation

◆ MSVCRT_new_handler_func

typedef int(CDECL * MSVCRT_new_handler_func) (MSVCRT_size_t size)

Definition at line 67 of file heap.c.

Function Documentation

◆ __p__amblksiz()

unsigned int *CDECL __p__amblksiz ( void  )

Definition at line 534 of file heap.c.

535{
536 return &MSVCRT_amblksiz;
537}
static unsigned int MSVCRT_amblksiz
Definition: heap.c:73

◆ _aligned_free()

void CDECL _aligned_free ( void memblock)

Definition at line 573 of file heap.c.

574{
575 TRACE("(%p)\n", memblock);
576
577 if (memblock)
578 {
579 void **saved = SAVED_PTR(memblock);
580 MSVCRT_free(*saved);
581 }
582}
#define MSVCRT_free
Definition: heap.c:46
#define SAVED_PTR(x)
Definition: heap.c:57
#define TRACE(s)
Definition: solgame.cpp:4

Referenced by _aligned_offset_realloc().

◆ _aligned_malloc()

void *CDECL _aligned_malloc ( MSVCRT_size_t  size,
MSVCRT_size_t  alignment 
)

Definition at line 630 of file heap.c.

631{
632 TRACE("(%lu, %lu)\n", size, alignment);
633 return _aligned_offset_malloc(size, alignment, 0);
634}
GLsizeiptr size
Definition: glext.h:5919
void *CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
Definition: heap.c:587

◆ _aligned_offset_malloc()

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

Definition at line 587 of file heap.c.

588{
589 void *memblock, *temp, **saved;
590 TRACE("(%lu, %lu, %lu)\n", size, alignment, offset);
591
592 /* alignment must be a power of 2 */
593 if ((alignment & (alignment - 1)) != 0)
594 {
596 return NULL;
597 }
598
599 /* offset must be less than size */
600 if (offset && offset >= size)
601 {
603 return NULL;
604 }
605
606 /* don't align to less than void pointer size */
607 if (alignment < sizeof(void *))
608 alignment = sizeof(void *);
609
610 /* allocate enough space for void pointer and alignment */
611 temp = MSVCRT_malloc(size + alignment + sizeof(void *));
612
613 if (!temp)
614 return NULL;
615
616 /* adjust pointer for proper alignment and offset */
617 memblock = ALIGN_PTR(temp, alignment, offset);
618
619 /* Save the real allocation address below returned address */
620 /* so it can be found later to free. */
621 saved = SAVED_PTR(memblock);
622 *saved = temp;
623
624 return memblock;
625}
#define NULL
Definition: types.h:112
static calc_node_t temp
Definition: rpn_ieee.c:38
#define MSVCRT_EINVAL
Definition: heap.c:36
#define MSVCRT__errno
Definition: heap.c:42
#define MSVCRT_malloc
Definition: heap.c:44
#define ALIGN_PTR(ptr, alignment, offset)
Definition: heap.c:59

Referenced by _aligned_malloc(), and _aligned_offset_realloc().

◆ _aligned_offset_realloc()

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

Definition at line 639 of file heap.c.

641{
642 void * temp, **saved;
643 MSVCRT_size_t old_padding, new_padding, old_size;
644 TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset);
645
646 if (!memblock)
647 return _aligned_offset_malloc(size, alignment, offset);
648
649 /* alignment must be a power of 2 */
650 if ((alignment & (alignment - 1)) != 0)
651 {
653 return NULL;
654 }
655
656 /* offset must be less than size */
657 if (offset >= size)
658 {
660 return NULL;
661 }
662
663 if (size == 0)
664 {
665 _aligned_free(memblock);
666 return NULL;
667 }
668
669 /* don't align to less than void pointer size */
670 if (alignment < sizeof(void *))
671 alignment = sizeof(void *);
672
673 /* make sure alignment and offset didn't change */
674 saved = SAVED_PTR(memblock);
675 if (memblock != ALIGN_PTR(*saved, alignment, offset))
676 {
678 return NULL;
679 }
680
681 old_padding = (char *)memblock - (char *)*saved;
682
683 /* Get previous size of block */
684 old_size = _msize(*saved);
685 if (old_size == -1)
686 {
687 /* It seems this function was called with an invalid pointer. Bail out. */
688 return NULL;
689 }
690
691 /* Adjust old_size to get amount of actual data in old block. */
692 if (old_size < old_padding)
693 {
694 /* Shouldn't happen. Something's weird, so bail out. */
695 return NULL;
696 }
697 old_size -= old_padding;
698
699 temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *));
700
701 if (!temp)
702 return NULL;
703
704 /* adjust pointer for proper alignment and offset */
705 memblock = ALIGN_PTR(temp, alignment, offset);
706
707 /* Save the real allocation address below returned address */
708 /* so it can be found later to free. */
709 saved = SAVED_PTR(memblock);
710
711 new_padding = (char *)memblock - (char *)temp;
712
713/*
714 Memory layout of old block is as follows:
715 +-------+---------------------+-+--------------------------+-----------+
716 | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... |
717 +-------+---------------------+-+--------------------------+-----------+
718 ^ ^ ^
719 | | |
720 *saved saved memblock
721
722 Memory layout of new block is as follows:
723 +-------+-----------------------------+-+----------------------+-------+
724 | ... | "new_padding" bytes | | ... "size" bytes ... | ... |
725 +-------+-----------------------------+-+----------------------+-------+
726 ^ ^ ^
727 | | |
728 temp saved memblock
729
730 However, in the new block, actual data is still written as follows
731 (because it was copied by MSVCRT_realloc):
732 +-------+---------------------+--------------------------------+-------+
733 | ... | "old_padding" bytes | ... "old_size" bytes ... | ... |
734 +-------+---------------------+--------------------------------+-------+
735 ^ ^ ^
736 | | |
737 temp saved memblock
738
739 Therefore, min(old_size,size) bytes of actual data have to be moved
740 from the offset they were at in the old block (temp + old_padding),
741 to the offset they have to be in the new block (temp + new_padding == memblock).
742*/
743 if (new_padding != old_padding)
744 memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size);
745
746 *saved = temp;
747
748 return memblock;
749}
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
unsigned long MSVCRT_size_t
Definition: msvcrt.h:67
MSVCRT_size_t CDECL _msize(void *mem)
Definition: heap.c:389
void CDECL _aligned_free(void *memblock)
Definition: heap.c:573
#define MSVCRT_realloc
Definition: heap.c:45

Referenced by _aligned_realloc().

◆ _aligned_realloc()

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

Definition at line 754 of file heap.c.

755{
756 TRACE("(%p, %lu, %lu)\n", memblock, size, alignment);
757 return _aligned_offset_realloc(memblock, size, alignment, 0);
758}
void *CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset)
Definition: heap.c:639

◆ _callnewh()

int CDECL _callnewh ( MSVCRT_size_t  size)

Definition at line 259 of file heap.c.

260{
261 int ret = 0;
263 if(handler)
264 ret = (*handler)(size) ? 1 : 0;
265 return ret;
266}
UINT(* handler)(MSIPACKAGE *)
Definition: action.c:7512
int(CDECL * MSVCRT_new_handler_func)(MSVCRT_size_t size)
Definition: heap.c:67
static MSVCRT_new_handler_func MSVCRT_new_handler
Definition: heap.c:69
int ret

◆ _expand()

void *CDECL _expand ( void mem,
MSVCRT_size_t  size 
)

Definition at line 271 of file heap.c.

272{
274}
#define HEAP_REALLOC_IN_PLACE_ONLY
Definition: nt_native.h:1696
static void * msvcrt_heap_realloc(DWORD flags, void *ptr, MSVCRT_size_t size)
Definition: heap.c:95
Definition: mem.c:156

◆ _get_heap_handle()

MSVCRT_intptr_t CDECL _get_heap_handle ( void  )

Definition at line 381 of file heap.c.

382{
383 return (MSVCRT_intptr_t)heap;
384}
long MSVCRT_intptr_t
Definition: msvcrt.h:68
static HANDLE heap
Definition: heap.c:65

◆ _get_sbh_threshold()

MSVCRT_size_t CDECL _get_sbh_threshold ( void  )

Definition at line 542 of file heap.c.

543{
545}
static MSVCRT_size_t MSVCRT_sbh_threshold
Definition: heap.c:75

Referenced by test_sbheap().

◆ _heapadd()

int CDECL _heapadd ( void mem,
MSVCRT_size_t  size 
)

Definition at line 371 of file heap.c.

372{
373 TRACE("(%p,%ld) unsupported in Win32\n", mem,size);
375 return -1;
376}
#define MSVCRT_ENOSYS
Definition: heap.c:37

◆ _heapchk()

int CDECL _heapchk ( void  )

Definition at line 279 of file heap.c.

280{
281 if (!HeapValidate(heap, 0, NULL) ||
282 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
283 {
285 return MSVCRT__HEAPBADNODE;
286 }
287 return MSVCRT__HEAPOK;
288}
BOOL WINAPI HeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
Definition: heapmem.c:156
#define MSVCRT__HEAPOK
Definition: heap.c:31
#define MSVCRT__HEAPBADNODE
Definition: heap.c:30
static HANDLE sb_heap
Definition: heap.c:65
#define msvcrt_set_errno
Definition: heap.c:50
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

◆ _heapmin()

int CDECL _heapmin ( void  )

Definition at line 293 of file heap.c.

294{
295 if (!HeapCompact( heap, 0 ) ||
296 (sb_heap && !HeapCompact( sb_heap, 0 )))
297 {
300 return -1;
301 }
302 return 0;
303}
#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 352 of file heap.c.

353{
354 int retval;
355 struct MSVCRT__heapinfo heap;
356
357 memset( &heap, 0, sizeof(heap) );
358 LOCK_HEAP;
359 while ((retval = _heapwalk(&heap)) == MSVCRT__HEAPOK)
360 {
361 if (heap._useflag == MSVCRT__FREEENTRY)
362 memset(heap._pentry, value, heap._size);
363 }
365 return retval == MSVCRT__HEAPEND? MSVCRT__HEAPOK : retval;
366}
#define MSVCRT__FREEENTRY
Definition: heap.c:33
#define UNLOCK_HEAP
Definition: heap.c:54
int CDECL _heapwalk(struct MSVCRT__heapinfo *next)
Definition: heap.c:308
#define MSVCRT__HEAPEND
Definition: heap.c:32
#define LOCK_HEAP
Definition: heap.c:53
#define memset(x, y, z)
Definition: compat.h:39
Definition: pdh_main.c:94

◆ _heapwalk()

int CDECL _heapwalk ( struct MSVCRT__heapinfo next)

Definition at line 308 of file heap.c.

309{
311
312 if (sb_heap)
313 FIXME("small blocks heap not supported\n");
314
315 LOCK_HEAP;
316 phe.lpData = next->_pentry;
317 phe.cbData = (DWORD)next->_size;
318 phe.wFlags = next->_useflag == MSVCRT__USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
319
320 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
321 !HeapValidate( heap, 0, phe.lpData ))
322 {
325 return MSVCRT__HEAPBADNODE;
326 }
327
328 do
329 {
330 if (!HeapWalk( heap, &phe ))
331 {
334 return MSVCRT__HEAPEND;
336 if (!phe.lpData)
338 return MSVCRT__HEAPBADNODE;
339 }
341
343 next->_pentry = phe.lpData;
344 next->_size = phe.cbData;
346 return MSVCRT__HEAPOK;
347}
#define FIXME(fmt,...)
Definition: precomp.h:53
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:105
BOOL WINAPI HeapWalk(HANDLE hHeap, LPPROCESS_HEAP_ENTRY lpEntry)
Definition: heapmem.c:291
if(dx< 0)
Definition: linetemp.h:194
#define DWORD
Definition: nt_native.h:44
static unsigned __int64 next
Definition: rand_nt.c:6
#define MSVCRT__USEDENTRY
Definition: heap.c:34
#define MSVCRT__HEAPBADBEGIN
Definition: heap.c:35
Definition: winbase.h:1268
WORD wFlags
Definition: winbase.h:1273
PVOID lpData
Definition: winbase.h:1269
DWORD cbData
Definition: winbase.h:1270
#define PROCESS_HEAP_UNCOMMITTED_RANGE
Definition: winbase.h:335
#define PROCESS_HEAP_ENTRY_BUSY
Definition: winbase.h:336
#define PROCESS_HEAP_REGION
Definition: winbase.h:334

Referenced by _heapset().

◆ _msize()

MSVCRT_size_t CDECL _msize ( void mem)

Definition at line 389 of file heap.c.

390{
392 if (size == ~(MSVCRT_size_t)0)
393 {
394 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
395 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
396 }
397 return size;
398}
#define WARN(fmt,...)
Definition: precomp.h:61
static MSVCRT_size_t msvcrt_heap_size(void *ptr)
Definition: heap.c:138

Referenced by _aligned_offset_realloc().

◆ _set_sbh_threshold()

int CDECL _set_sbh_threshold ( MSVCRT_size_t  threshold)

Definition at line 550 of file heap.c.

551{
552#ifdef _WIN64
553 return 0;
554#else
555 if(threshold > 1016)
556 return 0;
557
558 if(!sb_heap)
559 {
560 sb_heap = HeapCreate(0, 0, 0);
561 if(!sb_heap)
562 return 0;
563 }
564
565 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
566 return 1;
567#endif
568}
HANDLE WINAPI HeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize)
Definition: heapmem.c:45

◆ MSVCRT__query_new_handler()

MSVCRT_new_handler_func CDECL MSVCRT__query_new_handler ( void  )

Definition at line 206 of file heap.c.

207{
208 return MSVCRT_new_handler;
209}

◆ MSVCRT__query_new_mode()

int CDECL MSVCRT__query_new_mode ( void  )

Definition at line 215 of file heap.c.

216{
217 return MSVCRT_new_mode;
218}
static int MSVCRT_new_mode
Definition: heap.c:70

◆ MSVCRT__set_new_handler()

MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler ( MSVCRT_new_handler_func  func)

Definition at line 223 of file heap.c.

224{
225 MSVCRT_new_handler_func old_handler;
226 LOCK_HEAP;
227 old_handler = MSVCRT_new_handler;
230 return old_handler;
231}
GLenum func
Definition: glext.h:6028

Referenced by MSVCRT_set_new_handler().

◆ MSVCRT__set_new_mode()

int CDECL MSVCRT__set_new_mode ( int  mode)

Definition at line 246 of file heap.c.

247{
248 int old_mode;
249 LOCK_HEAP;
250 old_mode = MSVCRT_new_mode;
253 return old_mode;
254}
GLenum mode
Definition: glext.h:6217

◆ MSVCRT_calloc()

void *CDECL DECLSPEC_HOTPATCH MSVCRT_calloc ( MSVCRT_size_t  count,
MSVCRT_size_t  size 
)

Definition at line 421 of file heap.c.

422{
424
425 if (size && bytes / size != count)
426 {
428 return NULL;
429 }
430
432}
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
GLuint GLuint GLsizei count
Definition: gl.h:1545
static void * msvcrt_heap_alloc(DWORD flags, MSVCRT_size_t size)
Definition: heap.c:77
#define MSVCRT_ENOMEM
Definition: heap.c:38

◆ msvcrt_destroy_heap()

void msvcrt_destroy_heap ( void  )

Definition at line 905 of file heap.c.

906{
908 if(sb_heap)
910}
BOOL WINAPI HeapDestroy(HANDLE hHeap)
Definition: heapmem.c:85

◆ MSVCRT_free()

void CDECL DECLSPEC_HOTPATCH MSVCRT_free ( void ptr)

Definition at line 447 of file heap.c.

448{
450}
static BOOL msvcrt_heap_free(void *ptr)
Definition: heap.c:127

◆ msvcrt_heap_alloc()

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

Definition at line 77 of file heap.c.

78{
80 {
81 void *memblock, *temp, **saved;
82
83 temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN);
84 if(!temp) return NULL;
85
86 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
87 saved = SAVED_PTR(memblock);
88 *saved = temp;
89 return memblock;
90 }
91
92 return HeapAlloc(heap, flags, size);
93}
#define HeapAlloc
Definition: compat.h:733
GLbitfield flags
Definition: glext.h:7161
#define SB_HEAP_ALIGN
Definition: heap.c:63

Referenced by MSVCRT_calloc(), MSVCRT_malloc(), and MSVCRT_operator_new().

◆ msvcrt_heap_free()

static BOOL msvcrt_heap_free ( void ptr)
static

Definition at line 127 of file heap.c.

128{
129 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
130 {
131 void **saved = SAVED_PTR(ptr);
132 return HeapFree(sb_heap, 0, *saved);
133 }
134
135 return HeapFree(heap, 0, ptr);
136}
#define HeapFree(x, y, z)
Definition: compat.h:735

Referenced by MSVCRT_free(), and MSVCRT_operator_delete().

◆ msvcrt_heap_realloc()

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

Definition at line 95 of file heap.c.

96{
97 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
98 {
99 /* TODO: move data to normal heap if it exceeds sbh_threshold limit */
100 void *memblock, *temp, **saved;
101 MSVCRT_size_t old_padding, new_padding, old_size;
102
103 saved = SAVED_PTR(ptr);
104 old_padding = (char*)ptr - (char*)*saved;
105 old_size = HeapSize(sb_heap, 0, *saved);
106 if(old_size == -1)
107 return NULL;
108 old_size -= old_padding;
109
110 temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN);
111 if(!temp) return NULL;
112
113 memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0);
114 saved = SAVED_PTR(memblock);
115 new_padding = (char*)memblock - (char*)temp;
116
117 if(new_padding != old_padding)
118 memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size);
119
120 *saved = temp;
121 return memblock;
122 }
123
124 return HeapReAlloc(heap, flags, ptr, size);
125}
#define HeapReAlloc
Definition: compat.h:734
SIZE_T WINAPI HeapSize(HANDLE, DWORD, LPCVOID)

Referenced by _expand(), and MSVCRT_realloc().

◆ msvcrt_heap_size()

static MSVCRT_size_t msvcrt_heap_size ( void ptr)
static

Definition at line 138 of file heap.c.

139{
140 if(sb_heap && ptr && !HeapValidate(heap, 0, ptr))
141 {
142 void **saved = SAVED_PTR(ptr);
143 return HeapSize(sb_heap, 0, *saved);
144 }
145
146 return HeapSize(heap, 0, ptr);
147}

Referenced by _msize().

◆ msvcrt_init_heap()

BOOL msvcrt_init_heap ( void  )

Definition at line 895 of file heap.c.

896{
897#ifdef __REACTOS__
899#else
900 heap = HeapCreate(0, 0, 0);
901#endif
902 return heap != NULL;
903}
#define GetProcessHeap()
Definition: compat.h:736

Referenced by crt_process_init(), and DllMain().

◆ MSVCRT_malloc()

void *CDECL MSVCRT_malloc ( MSVCRT_size_t  size)

Definition at line 465 of file heap.c.

466{
467 void *ret = msvcrt_heap_alloc(0, size);
468 if (!ret)
470 return ret;
471}

◆ MSVCRT_memcpy_s()

int CDECL MSVCRT_memcpy_s ( void dest,
MSVCRT_size_t  numberOfElements,
const void src,
MSVCRT_size_t  count 
)

Definition at line 806 of file heap.c.

807{
808 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
809
810 if(!count)
811 return 0;
812
813 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
814 if (!MSVCRT_CHECK_PMT(src != NULL))
815 {
817 return MSVCRT_EINVAL;
818 }
820 {
822 return MSVCRT_ERANGE;
823 }
824
826 return 0;
827}
GLenum src
Definition: glext.h:6340
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
static size_t numberOfElements
Definition: string.c:87
static char * dest
Definition: rtl.c:135
#define MSVCRT_CHECK_PMT_ERR(x, err)
Definition: safecrt.h:15
#define MSVCRT_ERANGE
Definition: heap.c:39

◆ MSVCRT_memmove_s()

int CDECL MSVCRT_memmove_s ( void dest,
MSVCRT_size_t  numberOfElements,
const void src,
MSVCRT_size_t  count 
)

Definition at line 763 of file heap.c.

764{
765 TRACE("(%p %lu %p %lu)\n", dest, numberOfElements, src, count);
766
767 if(!count)
768 return 0;
769
770 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
771 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
773
775 return 0;
776}

◆ MSVCRT_operator_delete()

◆ MSVCRT_operator_new()

void *CDECL DECLSPEC_HOTPATCH MSVCRT_operator_new ( MSVCRT_size_t  size)

Definition at line 152 of file heap.c.

153{
154 void *retval;
155 int freed;
157
158 do
159 {
160 retval = msvcrt_heap_alloc(0, size);
161 if(retval)
162 {
163 TRACE("(%ld) returning %p\n", size, retval);
164 return retval;
165 }
166
167 LOCK_HEAP;
169 if(handler)
170 freed = (*handler)(size);
171 else
172 freed = 0;
174 } while(freed);
175
176 TRACE("(%ld) out of memory\n", size);
177#if _MSVCR_VER >= 80
178 throw_exception(EXCEPTION_BAD_ALLOC, 0, "bad allocation");
179#endif
180 return NULL;
181}

Referenced by MSVCRT_operator_new_dbg().

◆ MSVCRT_operator_new_dbg()

void *CDECL MSVCRT_operator_new_dbg ( MSVCRT_size_t  size,
int  type,
const char file,
int  line 
)

Definition at line 187 of file heap.c.

188{
189 return MSVCRT_operator_new( size );
190}
void *CDECL DECLSPEC_HOTPATCH MSVCRT_operator_new(MSVCRT_size_t size)
Definition: heap.c:152

◆ MSVCRT_realloc()

void *CDECL DECLSPEC_HOTPATCH MSVCRT_realloc ( void ptr,
MSVCRT_size_t  size 
)

Definition at line 486 of file heap.c.

487{
488 if (!ptr) return MSVCRT_malloc(size);
489 if (size) return msvcrt_heap_realloc(0, ptr, size);
491 return NULL;
492}

◆ MSVCRT_set_new_handler()

MSVCRT_new_handler_func CDECL MSVCRT_set_new_handler ( void func)

Definition at line 236 of file heap.c.

237{
238 TRACE("(%p)\n",func);
240 return NULL;
241}
MSVCRT_new_handler_func CDECL MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
Definition: heap.c:223

◆ MSVCRT_strncpy_s()

int CDECL MSVCRT_strncpy_s ( char dest,
MSVCRT_size_t  numberOfElements,
const char src,
MSVCRT_size_t  count 
)

Definition at line 860 of file heap.c.

862{
864
865 TRACE("(%s %lu %s %lu)\n", dest, numberOfElements, src, count);
866
867 if(!count) {
869 *dest = 0;
870 return 0;
871 }
872
873 if (!MSVCRT_CHECK_PMT(dest != NULL)) return MSVCRT_EINVAL;
874 if (!MSVCRT_CHECK_PMT(src != NULL)) return MSVCRT_EINVAL;
876
878 end = count;
879 else
881
882 for(i=0; i<end && src[i]; i++)
883 dest[i] = src[i];
884
885 if(!src[i] || end==count || count==MSVCRT__TRUNCATE) {
886 dest[i] = '\0';
887 return 0;
888 }
889
890 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", MSVCRT_EINVAL);
891 dest[0] = '\0';
892 return MSVCRT_EINVAL;
893}
GLuint GLuint end
Definition: gl.h:1545
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define MSVCRT_INVALID_PMT(x)
Definition: mbstowcs_s.c:25
#define MSVCRT__TRUNCATE
Definition: heap.c:40

Variable Documentation

◆ heap

◆ MSVCRT_amblksiz

unsigned int MSVCRT_amblksiz = 16
static

Definition at line 73 of file heap.c.

Referenced by __p__amblksiz().

◆ MSVCRT_new_handler

MSVCRT_new_handler_func MSVCRT_new_handler
static

◆ MSVCRT_new_mode

int MSVCRT_new_mode
static

Definition at line 70 of file heap.c.

Referenced by MSVCRT__query_new_mode(), and MSVCRT__set_new_mode().

◆ MSVCRT_sbh_threshold

MSVCRT_size_t MSVCRT_sbh_threshold = 0
static

Definition at line 75 of file heap.c.

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

◆ sb_heap