ReactOS 0.4.16-dev-455-g37b0646
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   _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

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)
 
int CDECL strncpy_s (char *dest, size_t numberOfElements, const char *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 int 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))
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   _lock( _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   _unlock( _HEAP_LOCK )

Definition at line 54 of file heap.c.

Typedef Documentation

◆ MSVCRT_new_handler_func

typedef int(CDECL * MSVCRT_new_handler_func) (size_t size)

Definition at line 67 of file heap.c.

Function Documentation

◆ __p__amblksiz()

unsigned int *CDECL __p__amblksiz ( void  )

Definition at line 528 of file heap.c.

529{
530 return &MSVCRT_amblksiz;
531}
static unsigned int MSVCRT_amblksiz
Definition: heap.c:73

◆ _aligned_free()

void CDECL _aligned_free ( void memblock)

Definition at line 567 of file heap.c.

568{
569 TRACE("(%p)\n", memblock);
570
571 if (memblock)
572 {
573 void **saved = SAVED_PTR(memblock);
574 free(*saved);
575 }
576}
#define free
Definition: debug_ros.c:5
#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 ( size_t  size,
size_t  alignment 
)

Definition at line 624 of file heap.c.

625{
626 TRACE("(%Iu, %Iu)\n", size, alignment);
627 return _aligned_offset_malloc(size, alignment, 0);
628}
GLsizeiptr size
Definition: glext.h:5919
void *CDECL _aligned_offset_malloc(size_t size, size_t alignment, size_t offset)
Definition: heap.c:581

◆ _aligned_offset_malloc()

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

Definition at line 581 of file heap.c.

582{
583 void *memblock, *temp, **saved;
584 TRACE("(%Iu, %Iu, %Iu)\n", size, alignment, offset);
585
586 /* alignment must be a power of 2 */
587 if ((alignment & (alignment - 1)) != 0)
588 {
589 *_errno() = EINVAL;
590 return NULL;
591 }
592
593 /* offset must be less than size */
594 if (offset && offset >= size)
595 {
596 *_errno() = EINVAL;
597 return NULL;
598 }
599
600 /* don't align to less than void pointer size */
601 if (alignment < sizeof(void *))
602 alignment = sizeof(void *);
603
604 /* allocate enough space for void pointer and alignment */
605 temp = malloc(size + alignment + sizeof(void *));
606
607 if (!temp)
608 return NULL;
609
610 /* adjust pointer for proper alignment and offset */
611 memblock = ALIGN_PTR(temp, alignment, offset);
612
613 /* Save the real allocation address below returned address */
614 /* so it can be found later to free. */
615 saved = SAVED_PTR(memblock);
616 *saved = temp;
617
618 return memblock;
619}
#define EINVAL
Definition: acclib.h:90
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
static calc_node_t temp
Definition: rpn_ieee.c:38
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:17
#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,
size_t  size,
size_t  alignment,
size_t  offset 
)

Definition at line 633 of file heap.c.

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

Referenced by _aligned_realloc().

◆ _aligned_realloc()

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

Definition at line 748 of file heap.c.

749{
750 TRACE("(%p, %Iu, %Iu)\n", memblock, size, alignment);
751 return _aligned_offset_realloc(memblock, size, alignment, 0);
752}
void *CDECL _aligned_offset_realloc(void *memblock, size_t size, size_t alignment, size_t offset)
Definition: heap.c:633

◆ _callnewh()

int CDECL _callnewh ( size_t  size)

Definition at line 152 of file heap.c.

153{
154 int ret = 0;
156 if(handler)
157 ret = (*handler)(size) ? 1 : 0;
158 return ret;
159}
UINT(* handler)(MSIPACKAGE *)
Definition: action.c:7512
int(CDECL * MSVCRT_new_handler_func)(size_t size)
Definition: heap.c:67
static MSVCRT_new_handler_func MSVCRT_new_handler
Definition: heap.c:69
int ret

Referenced by malloc(), and operator_new().

◆ _expand()

void *CDECL _expand ( void mem,
size_t  size 
)

Definition at line 257 of file heap.c.

258{
260}
#define HEAP_REALLOC_IN_PLACE_ONLY
Definition: nt_native.h:1696
static void * msvcrt_heap_realloc(DWORD flags, void *ptr, size_t size)
Definition: heap.c:95
Definition: mem.c:349

◆ _get_heap_handle()

intptr_t CDECL _get_heap_handle ( void  )

Definition at line 367 of file heap.c.

368{
369 return (intptr_t)heap;
370}
static HANDLE heap
Definition: heap.c:65
int intptr_t
Definition: vcruntime.h:134

◆ _get_sbh_threshold()

size_t CDECL _get_sbh_threshold ( void  )

Definition at line 536 of file heap.c.

537{
539}
static size_t MSVCRT_sbh_threshold
Definition: heap.c:75

Referenced by test_sbheap().

◆ _heapadd()

int CDECL _heapadd ( void mem,
size_t  size 
)

Definition at line 357 of file heap.c.

358{
359 TRACE("(%p,%Iu) unsupported in Win32\n", mem,size);
360 *_errno() = ENOSYS;
361 return -1;
362}
#define ENOSYS
Definition: errno.h:57

◆ _heapchk()

int CDECL _heapchk ( void  )

Definition at line 265 of file heap.c.

266{
267 if (!HeapValidate(heap, 0, NULL) ||
268 (sb_heap && !HeapValidate(sb_heap, 0, NULL)))
269 {
271 return _HEAPBADNODE;
272 }
273 return _HEAPOK;
274}
BOOL WINAPI HeapValidate(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
Definition: heapmem.c:156
#define _HEAPOK
Definition: malloc.h:29
#define _HEAPBADNODE
Definition: malloc.h:31
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 279 of file heap.c.

280{
281 if (!HeapCompact( heap, 0 ) ||
282 (sb_heap && !HeapCompact( sb_heap, 0 )))
283 {
286 return -1;
287 }
288 return 0;
289}
#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 338 of file heap.c.

339{
340 int retval;
342
343 memset( &heap, 0, sizeof(heap) );
344 LOCK_HEAP;
345 while ((retval = _heapwalk(&heap)) == _HEAPOK)
346 {
347 if (heap._useflag == _FREEENTRY)
348 memset(heap._pentry, value, heap._size);
349 }
351 return retval == _HEAPEND ? _HEAPOK : retval;
352}
#define _HEAPEND
Definition: malloc.h:32
#define _FREEENTRY
Definition: malloc.h:36
#define UNLOCK_HEAP
Definition: heap.c:54
int CDECL _heapwalk(_HEAPINFO *next)
Definition: heap.c:294
#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 ( _HEAPINFO next)

Definition at line 294 of file heap.c.

295{
297
298 if (sb_heap)
299 FIXME("small blocks heap not supported\n");
300
301 LOCK_HEAP;
302 phe.lpData = next->_pentry;
303 phe.cbData = (DWORD)next->_size;
304 phe.wFlags = next->_useflag == _USEDENTRY ? PROCESS_HEAP_ENTRY_BUSY : 0;
305
306 if (phe.lpData && phe.wFlags & PROCESS_HEAP_ENTRY_BUSY &&
307 !HeapValidate( heap, 0, phe.lpData ))
308 {
311 return _HEAPBADNODE;
312 }
313
314 do
315 {
316 if (!HeapWalk( heap, &phe ))
317 {
320 return _HEAPEND;
322 if (!phe.lpData)
323 return _HEAPBADBEGIN;
324 return _HEAPBADNODE;
325 }
327
329 next->_pentry = phe.lpData;
330 next->_size = phe.cbData;
332 return _HEAPOK;
333}
#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 _HEAPBADBEGIN
Definition: malloc.h:30
#define _USEDENTRY
Definition: malloc.h:37
#define DWORD
Definition: nt_native.h:44
static unsigned __int64 next
Definition: rand_nt.c:6
Definition: winbase.h:1293
WORD wFlags
Definition: winbase.h:1298
PVOID lpData
Definition: winbase.h:1294
DWORD cbData
Definition: winbase.h:1295
#define PROCESS_HEAP_UNCOMMITTED_RANGE
Definition: winbase.h:361
#define PROCESS_HEAP_ENTRY_BUSY
Definition: winbase.h:362
#define PROCESS_HEAP_REGION
Definition: winbase.h:360

Referenced by _heapset().

◆ _msize()

size_t CDECL _msize ( void mem)

Definition at line 375 of file heap.c.

376{
377 size_t size = msvcrt_heap_size(mem);
378 if (size == ~(size_t)0)
379 {
380 WARN(":Probably called with non wine-allocated memory, ret = -1\n");
381 /* At least the Win32 crtdll/msvcrt also return -1 in this case */
382 }
383 return size;
384}
#define WARN(fmt,...)
Definition: precomp.h:61
static size_t msvcrt_heap_size(void *ptr)
Definition: heap.c:138

Referenced by _aligned_offset_realloc().

◆ _query_new_handler()

MSVCRT_new_handler_func CDECL _query_new_handler ( void  )

Definition at line 208 of file heap.c.

209{
210 return MSVCRT_new_handler;
211}

◆ _query_new_mode()

int CDECL _query_new_mode ( void  )

Definition at line 217 of file heap.c.

218{
219 return MSVCRT_new_mode;
220}
static int MSVCRT_new_mode
Definition: heap.c:70

◆ _set_new_handler()

Definition at line 225 of file heap.c.

226{
227 MSVCRT_new_handler_func old_handler;
228 LOCK_HEAP;
229 old_handler = MSVCRT_new_handler;
232 return old_handler;
233}
GLenum func
Definition: glext.h:6028

Referenced by set_new_handler().

◆ _set_new_mode()

int CDECL _set_new_mode ( int  mode)

Definition at line 248 of file heap.c.

249{
250 if(!MSVCRT_CHECK_PMT(mode == 0 || mode == 1)) return -1;
252}
#define InterlockedExchange
Definition: armddk.h:54
GLenum mode
Definition: glext.h:6217
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26

◆ _set_sbh_threshold()

int CDECL _set_sbh_threshold ( size_t  threshold)

Definition at line 544 of file heap.c.

545{
546#ifdef _WIN64
547 return 0;
548#else
549 if(threshold > 1016)
550 return 0;
551
552 if(!sb_heap)
553 {
554 sb_heap = HeapCreate(0, 0, 0);
555 if(!sb_heap)
556 return 0;
557 }
558
559 MSVCRT_sbh_threshold = (threshold+0xf) & ~0xf;
560 return 1;
561#endif
562}
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 407 of file heap.c.

408{
409 size_t bytes = count*size;
410
411 if (size && bytes / size != count)
412 {
413 *_errno() = ENOMEM;
414 return NULL;
415 }
416
418}
#define ENOMEM
Definition: acclib.h:84
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, size_t size)
Definition: heap.c:77

◆ free()

void CDECL DECLSPEC_HOTPATCH free ( void ptr)

Definition at line 433 of file heap.c.

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

◆ malloc()

void *CDECL malloc ( size_t  size)

Definition at line 451 of file heap.c.

452{
453 void *ret;
454
455 do
456 {
458 if (ret || !MSVCRT_new_mode)
459 break;
460 } while(_callnewh(size));
461
462 if (!ret)
463 *_errno() = ENOMEM;
464 return ret;
465}
int CDECL _callnewh(size_t size)
Definition: heap.c:152

◆ memcpy_s()

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

Definition at line 800 of file heap.c.

801{
802 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
803
804 if(!count)
805 return 0;
806
807 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
808 if (!MSVCRT_CHECK_PMT(src != NULL))
809 {
811 return EINVAL;
812 }
814 {
816 return ERANGE;
817 }
818
820 return 0;
821}
#define ERANGE
Definition: acclib.h:92
GLenum src
Definition: glext.h:6340
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

◆ memmove_s()

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

Definition at line 757 of file heap.c.

758{
759 TRACE("(%p %Iu %p %Iu)\n", dest, numberOfElements, src, count);
760
761 if(!count)
762 return 0;
763
764 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
765 if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
767
769 return 0;
770}

◆ msvcrt_destroy_heap()

void msvcrt_destroy_heap ( void  )

Definition at line 899 of file heap.c.

900{
902 if(sb_heap)
904}
BOOL WINAPI HeapDestroy(HANDLE hHeap)
Definition: heapmem.c:85

◆ msvcrt_heap_alloc()

static void * msvcrt_heap_alloc ( DWORD  flags,
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 calloc(), malloc(), and 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 free(), and operator_delete().

◆ msvcrt_heap_realloc()

static void * msvcrt_heap_realloc ( DWORD  flags,
void ptr,
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 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 realloc().

◆ msvcrt_heap_size()

static 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 889 of file heap.c.

890{
891#ifdef __REACTOS__
893#else
894 heap = HeapCreate(0, 0, 0);
895#endif
896 return heap != NULL;
897}
#define GetProcessHeap()
Definition: compat.h:736

Referenced by crt_process_init(), and DllMain().

◆ operator_delete()

◆ operator_new()

void *CDECL DECLSPEC_HOTPATCH operator_new ( size_t  size)

Definition at line 164 of file heap.c.

165{
166 void *retval;
167
168 do
169 {
170 retval = msvcrt_heap_alloc(0, size);
171 if(retval)
172 {
173 TRACE("(%Iu) returning %p\n", size, retval);
174 return retval;
175 }
176 } while(_callnewh(size));
177
178 TRACE("(%Iu) out of memory\n", size);
179#if _MSVCR_VER >= 80
180 throw_bad_alloc();
181#endif
182 return NULL;
183}

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 189 of file heap.c.

190{
191 return operator_new( size );
192}
void *CDECL DECLSPEC_HOTPATCH operator_new(size_t size)
Definition: heap.c:164

◆ realloc()

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

Definition at line 480 of file heap.c.

481{
482 if (!ptr) return malloc(size);
483 if (size) return msvcrt_heap_realloc(0, ptr, size);
484 free(ptr);
485 return NULL;
486}

◆ set_new_handler()

MSVCRT_new_handler_func CDECL set_new_handler ( void func)

Definition at line 238 of file heap.c.

239{
240 TRACE("(%p)\n",func);
242 return NULL;
243}
MSVCRT_new_handler_func CDECL _set_new_handler(MSVCRT_new_handler_func func)
Definition: heap.c:225

◆ strncpy_s()

int CDECL strncpy_s ( char dest,
size_t  numberOfElements,
const char src,
size_t  count 
)

Definition at line 854 of file heap.c.

856{
857 size_t i, end;
858
859 TRACE("(%p %Iu %s %Iu)\n", dest, numberOfElements, debugstr_a(src), count);
860
861 if(!count) {
863 *dest = 0;
864 return 0;
865 }
866
867 if (!MSVCRT_CHECK_PMT(dest != NULL)) return EINVAL;
868 if (!MSVCRT_CHECK_PMT(src != NULL)) return EINVAL;
869 if (!MSVCRT_CHECK_PMT(numberOfElements != 0)) return EINVAL;
870
872 end = count;
873 else
875
876 for(i=0; i<end && src[i]; i++)
877 dest[i] = src[i];
878
879 if(!src[i] || end==count || count==_TRUNCATE) {
880 dest[i] = '\0';
881 return 0;
882 }
883
884 MSVCRT_INVALID_PMT("dest[numberOfElements] is too small", EINVAL);
885 dest[0] = '\0';
886 return EINVAL;
887}
#define _TRUNCATE
Definition: crtdefs.h:198
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 debugstr_a
Definition: kernel32.h:31
#define MSVCRT_INVALID_PMT(x)
Definition: mbstowcs_s.c:25

Referenced by __Named_exception::__Named_exception(), and __Named_exception::operator=().

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

Definition at line 69 of file heap.c.

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

◆ MSVCRT_new_mode

int MSVCRT_new_mode
static

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

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

◆ sb_heap