static inline int pf_check_auto_grow(pf_output *out, unsigned delta) { if (pf_is_auto_grow(out) && out->used + delta > out->len) { out->len = max(out->len * 2, out->used + delta); if (out->unicode) { if (out->buf.W != out->grow.W) out->buf.W = MSVCRT_realloc(out->buf.W, out->len * sizeof(WCHAR)); else out->buf.W = MSVCRT_malloc(out->len * sizeof(WCHAR)); if (!out->buf.W) return -1; } else { if (out->buf.A != out->grow.A) out->buf.A = MSVCRT_realloc(out->buf.A, out->len * sizeof(char)); else out->buf.A = MSVCRT_malloc(out->len * sizeof(char)); if (!out->buf.A) return -1; } } return 0; }
/********************************************************************* * _recalloc (MSVCR100.@) */ void* CDECL _recalloc(void *mem, MSVCRT_size_t num, MSVCRT_size_t size) { MSVCRT_size_t old_size; void *ret; if(!mem) return MSVCRT_calloc(num, size); size = num*size; old_size = _msize(mem); ret = MSVCRT_realloc(mem, size); if(!ret) { *MSVCRT__errno() = MSVCRT_ENOMEM; return NULL; } if(size>old_size) memset((BYTE*)ret+old_size, 0, size-old_size); return ret; }
/********************************************************************* * _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; }
/********************************************************************* * _realloc_base (UCRTBASE.@) */ void* CDECL _realloc_base(void* ptr, MSVCRT_size_t size) { return MSVCRT_realloc(ptr, size); }