static void* msvcrt_heap_realloc(DWORD flags, void *ptr, MSVCRT_size_t size) { if(sb_heap && ptr && !HeapValidate(heap, 0, ptr)) { /* TODO: move data to normal heap if it exceeds sbh_threshold limit */ void *memblock, *temp, **saved; MSVCRT_size_t old_padding, new_padding, old_size; saved = SAVED_PTR(ptr); old_padding = (char*)ptr - (char*)*saved; old_size = HeapSize(sb_heap, 0, *saved); if(old_size == -1) return NULL; old_size -= old_padding; temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN); if(!temp) return NULL; memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0); saved = SAVED_PTR(memblock); new_padding = (char*)memblock - (char*)temp; if(new_padding != old_padding) memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size); *saved = temp; return memblock; } return HeapReAlloc(heap, flags, ptr, size); }
/********************************************************************* * _aligned_free (MSVCRT.@) */ void CDECL _aligned_free(void *memblock) { TRACE("(%p)\n", memblock); if (memblock) { void **saved = SAVED_PTR(memblock); MSVCRT_free(*saved); } }
static MSVCRT_size_t msvcrt_heap_size(void *ptr) { if(sb_heap && ptr && !HeapValidate(heap, 0, ptr)) { void **saved = SAVED_PTR(ptr); return HeapSize(sb_heap, 0, *saved); } return HeapSize(heap, 0, ptr); }
static BOOL msvcrt_heap_free(void *ptr) { if(sb_heap && ptr && !HeapValidate(heap, 0, ptr)) { void **saved = SAVED_PTR(ptr); return HeapFree(sb_heap, 0, *saved); } return HeapFree(heap, 0, ptr); }
/********************************************************************* * _aligned_msize (MSVCR100.@) */ size_t CDECL _aligned_msize(void *p, MSVCRT_size_t alignment, MSVCRT_size_t offset) { void **alloc_ptr; if(!MSVCRT_CHECK_PMT(p)) return -1; if(alignment < sizeof(void*)) alignment = sizeof(void*); alloc_ptr = SAVED_PTR(p); return _msize(*alloc_ptr)-alignment-sizeof(void*); }
static void* msvcrt_heap_alloc(DWORD flags, MSVCRT_size_t size) { if(size < MSVCRT_sbh_threshold) { void *memblock, *temp, **saved; temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN); if(!temp) return NULL; memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0); saved = SAVED_PTR(memblock); *saved = temp; return memblock; } return HeapAlloc(heap, flags, size); }
/********************************************************************* * _aligned_offset_malloc (MSVCRT.@) */ void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset) { void *memblock, *temp, **saved; TRACE("(%lu, %lu, %lu)\n", size, alignment, offset); /* alignment must be a power of 2 */ if ((alignment & (alignment - 1)) != 0) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } /* offset must be less than size */ if (offset >= size) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } /* don't align to less than void pointer size */ if (alignment < sizeof(void *)) alignment = sizeof(void *); /* allocate enough space for void pointer and alignment */ temp = MSVCRT_malloc(size + alignment + sizeof(void *)); if (!temp) return NULL; /* adjust pointer for proper alignment and offset */ memblock = ALIGN_PTR(temp, alignment, offset); /* Save the real allocation address below returned address */ /* so it can be found later to free. */ saved = SAVED_PTR(memblock); *saved = temp; return memblock; }
/********************************************************************* * _aligned_offset_realloc (MSVCRT.@) */ void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset) { void * temp, **saved; MSVCRT_size_t old_padding, new_padding, old_size; TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset); if (!memblock) return _aligned_offset_malloc(size, alignment, offset); /* alignment must be a power of 2 */ if ((alignment & (alignment - 1)) != 0) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } /* offset must be less than size */ if (offset >= size) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } if (size == 0) { _aligned_free(memblock); return NULL; } /* don't align to less than void pointer size */ if (alignment < sizeof(void *)) alignment = sizeof(void *); /* make sure alignment and offset didn't change */ saved = SAVED_PTR(memblock); if (memblock != ALIGN_PTR(*saved, alignment, offset)) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } old_padding = (char *)memblock - (char *)*saved; /* Get previous size of block */ old_size = _msize(*saved); if (old_size == -1) { /* It seems this function was called with an invalid pointer. Bail out. */ return NULL; } /* Adjust old_size to get amount of actual data in old block. */ if (old_size < old_padding) { /* Shouldn't happen. Something's weird, so bail out. */ return NULL; } old_size -= old_padding; temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *)); if (!temp) return NULL; /* adjust pointer for proper alignment and offset */ memblock = ALIGN_PTR(temp, alignment, offset); /* Save the real allocation address below returned address */ /* so it can be found later to free. */ saved = SAVED_PTR(memblock); new_padding = (char *)memblock - (char *)temp; /* Memory layout of old block is as follows: +-------+---------------------+-+--------------------------+-----------+ | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... | +-------+---------------------+-+--------------------------+-----------+ ^ ^ ^ | | | *saved saved memblock Memory layout of new block is as follows: +-------+-----------------------------+-+----------------------+-------+ | ... | "new_padding" bytes | | ... "size" bytes ... | ... | +-------+-----------------------------+-+----------------------+-------+ ^ ^ ^ | | | temp saved memblock However, in the new block, actual data is still written as follows (because it was copied by MSVCRT_realloc): +-------+---------------------+--------------------------------+-------+ | ... | "old_padding" bytes | ... "old_size" bytes ... | ... | +-------+---------------------+--------------------------------+-------+ ^ ^ ^ | | | temp saved memblock Therefore, min(old_size,size) bytes of actual data have to be moved from the offset they were at in the old block (temp + old_padding), to the offset they have to be in the new block (temp + new_padding == memblock). */ if (new_padding != old_padding) memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size); *saved = temp; return memblock; }