새 포인터가 할당 된 위치에서 이전 포인터의 크기를 조정할 수 없으면 내용을 복사하고 이전 포인터를 해제합니다. 내가 여기 malloc_
로 시작하는 이름을 가진 여러 함수를 호출하고있어
void * realloc(void *ptr, size_t size)
{
// realloc() on a NULL pointer is the same as malloc().
if (ptr == NULL)
return malloc(size);
size_t oldsize = malloc_getsize(ptr);
// Are we shrinking an allocation? That's easy.
if (size < oldsize) {
malloc_setsize(ptr, size);
return ptr;
}
// Can we grow this allocation in place?
if (malloc_can_grow(ptr, size)) {
malloc_setsize(ptr, size);
return ptr;
}
// Create a new allocation, move the data there, and free the old one.
void *newptr = malloc(size);
if (newptr == NULL)
return NULL;
memcpy(newptr, ptr, oldsize);
free(ptr);
return newptr;
}
참고 :
+1 'mremap/MREMAP_MAYMOVE'의 경우 +1입니다. – edmz