ReactOS 0.4.16-dev-2104-gb84fa49
corecrt_malloc.h File Reference
#include <corecrt.h>
Include dependency graph for corecrt_malloc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

_ACRTIMP void *__cdecl calloc (size_t, size_t)
 
_ACRTIMP void __cdecl free (void *)
 
_ACRTIMP void *__cdecl malloc (size_t)
 
_ACRTIMP void *__cdecl realloc (void *, size_t)
 
_ACRTIMP void *__cdecl _recalloc (void *, size_t, size_t) __WINE_ALLOC_SIZE(2
 
_ACRTIMP void *__cdecl __WINE_DEALLOC (free)
 
_ACRTIMP void *__cdecl _expand (void *, size_t)
 
_ACRTIMP size_t __cdecl _msize (void *)
 
_ACRTIMP void __cdecl _aligned_free (void *)
 
_ACRTIMP void *__cdecl _aligned_malloc (size_t, size_t) __WINE_ALLOC_SIZE(1) __WINE_DEALLOC(_aligned_free) __WINE_MALLOC
 
_ACRTIMP void *__cdecl _aligned_offset_malloc (size_t, size_t, size_t) __WINE_ALLOC_SIZE(1) __WINE_DEALLOC(_aligned_free) __WINE_MALLOC
 
_ACRTIMP void *__cdecl _aligned_realloc (void *, size_t, size_t) __WINE_ALLOC_SIZE(2) __WINE_DEALLOC(_aligned_free)
 
_ACRTIMP void *__cdecl _aligned_offset_realloc (void *, size_t, size_t, size_t) __WINE_ALLOC_SIZE(2) __WINE_DEALLOC(_aligned_free)
 

Function Documentation

◆ __WINE_DEALLOC()

_ACRTIMP void *__cdecl __WINE_DEALLOC ( free  )

◆ _aligned_free()

_ACRTIMP 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()

_ACRTIMP 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}
_Check_return_ _Ret_maybenull_ _In_ size_t alignment
Definition: align.cpp:48
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()

_ACRTIMP 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
GLintptr offset
Definition: glext.h:5920
static calc_node_t temp
Definition: rpn_ieee.c:38

Referenced by _aligned_malloc(), and _aligned_offset_realloc().

◆ _aligned_offset_realloc()

_ACRTIMP 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()

_ACRTIMP 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

◆ _expand()

_ACRTIMP 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().

◆ _msize()

_ACRTIMP 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().

◆ _recalloc()

_ACRTIMP void *__cdecl _recalloc ( void ,
size_t  ,
size_t   
)

◆ calloc()

_ACRTIMP void *__cdecl calloc ( size_t  nmemb,
size_t  size 
)

Definition at line 157 of file cabinet.c.

158{
159 return (void *)RtlAllocateHeap(ProcessHeap, HEAP_ZERO_MEMORY, nmemb * size);
160}
HANDLE ProcessHeap
Definition: servman.c:15
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
#define HEAP_ZERO_MEMORY
Definition: compat.h:134

◆ free()

_ACRTIMP void __cdecl free ( void ptr)

Definition at line 151 of file cabinet.c.

152{
154}
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
static PVOID ptr
Definition: dispmode.c:27

◆ malloc()

_ACRTIMP void *__cdecl malloc ( size_t  size)

Definition at line 145 of file cabinet.c.

◆ realloc()

_ACRTIMP void *__cdecl 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}