kernel: Implement realloc_etc and make use of it.
This commit is contained in:
@@ -60,6 +60,7 @@ extern "C" {
|
|||||||
|
|
||||||
|
|
||||||
void* memalign_etc(size_t alignment, size_t size, uint32 flags) _ALIGNED_BY_ARG(1);
|
void* memalign_etc(size_t alignment, size_t size, uint32 flags) _ALIGNED_BY_ARG(1);
|
||||||
|
void* realloc_etc(void* address, size_t newSize, uint32 flags);
|
||||||
void free_etc(void* address, uint32 flags);
|
void free_etc(void* address, uint32 flags);
|
||||||
|
|
||||||
void* memalign(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
|
void* memalign(size_t alignment, size_t size) _ALIGNED_BY_ARG(1);
|
||||||
|
|||||||
@@ -542,7 +542,7 @@ guarded_heap_free(void* address, uint32 flags)
|
|||||||
|
|
||||||
|
|
||||||
static void*
|
static void*
|
||||||
guarded_heap_realloc(void* address, size_t newSize)
|
guarded_heap_realloc(void* address, size_t newSize, uint32 flags)
|
||||||
{
|
{
|
||||||
guarded_heap_area* area = guarded_heap_get_locked_area_for(sGuardedHeap,
|
guarded_heap_area* area = guarded_heap_get_locked_area_for(sGuardedHeap,
|
||||||
address);
|
address);
|
||||||
@@ -562,13 +562,13 @@ guarded_heap_realloc(void* address, size_t newSize)
|
|||||||
if (oldSize == newSize)
|
if (oldSize == newSize)
|
||||||
return address;
|
return address;
|
||||||
|
|
||||||
void* newBlock = malloc(newSize);
|
void* newBlock = malloc_etc(newSize, flags);
|
||||||
if (newBlock == NULL)
|
if (newBlock == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memcpy(newBlock, address, min_c(oldSize, newSize));
|
memcpy(newBlock, address, min_c(oldSize, newSize));
|
||||||
|
|
||||||
free(address);
|
free_etc(address, flags);
|
||||||
|
|
||||||
return newBlock;
|
return newBlock;
|
||||||
}
|
}
|
||||||
@@ -966,17 +966,24 @@ free(void* address)
|
|||||||
|
|
||||||
|
|
||||||
void*
|
void*
|
||||||
realloc(void* address, size_t newSize)
|
realloc_etc(void* address, size_t newSize, uint32 flags)
|
||||||
{
|
{
|
||||||
if (newSize == 0) {
|
if (newSize == 0) {
|
||||||
free(address);
|
free_etc(address, flags);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (address == NULL)
|
if (address == NULL)
|
||||||
return malloc(newSize);
|
return malloc_etc(newSize, flags);
|
||||||
|
|
||||||
return guarded_heap_realloc(address, newSize);
|
return guarded_heap_realloc(address, newSize, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
|
realloc(void* address, size_t newSize)
|
||||||
|
{
|
||||||
|
return realloc_etc(address, newSize, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1773,7 +1773,7 @@ heap_set_get_caller(heap_allocator* heap, addr_t (*getCaller)())
|
|||||||
|
|
||||||
static status_t
|
static status_t
|
||||||
heap_realloc(heap_allocator *heap, void *address, void **newAddress,
|
heap_realloc(heap_allocator *heap, void *address, void **newAddress,
|
||||||
size_t newSize)
|
size_t newSize, uint32 flags)
|
||||||
{
|
{
|
||||||
ReadLocker areaReadLocker(heap->area_lock);
|
ReadLocker areaReadLocker(heap->area_lock);
|
||||||
heap_area *area = heap->all_areas;
|
heap_area *area = heap->all_areas;
|
||||||
@@ -1864,7 +1864,7 @@ heap_realloc(heap_allocator *heap, void *address, void **newAddress,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// if not, allocate a new chunk of memory
|
// if not, allocate a new chunk of memory
|
||||||
*newAddress = malloc(newSize);
|
*newAddress = malloc_etc(newSize, flags);
|
||||||
T(Reallocate((addr_t)address, (addr_t)*newAddress, newSize));
|
T(Reallocate((addr_t)address, (addr_t)*newAddress, newSize));
|
||||||
if (*newAddress == NULL) {
|
if (*newAddress == NULL) {
|
||||||
// we tried but it didn't work out, but still the operation is done
|
// we tried but it didn't work out, but still the operation is done
|
||||||
@@ -2402,7 +2402,7 @@ free(void *address)
|
|||||||
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
realloc(void *address, size_t newSize)
|
realloc_etc(void *address, size_t newSize, uint32 flags)
|
||||||
{
|
{
|
||||||
if (!gKernelStartup && !are_interrupts_enabled()) {
|
if (!gKernelStartup && !are_interrupts_enabled()) {
|
||||||
panic("realloc(): called with interrupts disabled\n");
|
panic("realloc(): called with interrupts disabled\n");
|
||||||
@@ -2410,10 +2410,10 @@ realloc(void *address, size_t newSize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (address == NULL)
|
if (address == NULL)
|
||||||
return malloc(newSize);
|
return malloc_etc(newSize, flags);
|
||||||
|
|
||||||
if (newSize == 0) {
|
if (newSize == 0) {
|
||||||
free(address);
|
free_etc(address, flags);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2421,7 +2421,7 @@ realloc(void *address, size_t newSize)
|
|||||||
int32 offset = smp_get_current_cpu() * HEAP_CLASS_COUNT;
|
int32 offset = smp_get_current_cpu() * HEAP_CLASS_COUNT;
|
||||||
for (uint32 i = 0; i < sHeapCount; i++) {
|
for (uint32 i = 0; i < sHeapCount; i++) {
|
||||||
heap_allocator *heap = sHeaps[(i + offset) % sHeapCount];
|
heap_allocator *heap = sHeaps[(i + offset) % sHeapCount];
|
||||||
if (heap_realloc(heap, address, &newAddress, newSize) == B_OK) {
|
if (heap_realloc(heap, address, &newAddress, newSize, flags) == B_OK) {
|
||||||
#if PARANOID_HEAP_VALIDATION
|
#if PARANOID_HEAP_VALIDATION
|
||||||
heap_validate_heap(heap);
|
heap_validate_heap(heap);
|
||||||
#endif
|
#endif
|
||||||
@@ -2430,7 +2430,7 @@ realloc(void *address, size_t newSize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// maybe it was allocated from the dedicated grow heap
|
// maybe it was allocated from the dedicated grow heap
|
||||||
if (heap_realloc(sGrowHeap, address, &newAddress, newSize) == B_OK)
|
if (heap_realloc(sGrowHeap, address, &newAddress, newSize, flags) == B_OK)
|
||||||
return newAddress;
|
return newAddress;
|
||||||
|
|
||||||
// or maybe it was a huge allocation using an area
|
// or maybe it was a huge allocation using an area
|
||||||
@@ -2476,6 +2476,13 @@ realloc(void *address, size_t newSize)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void *
|
||||||
|
realloc(void *address, size_t newSize)
|
||||||
|
{
|
||||||
|
return realloc_etc(address, newSize, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // USE_DEBUG_HEAP_FOR_MALLOC
|
#endif // USE_DEBUG_HEAP_FOR_MALLOC
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -256,15 +256,15 @@ free(void* address)
|
|||||||
|
|
||||||
|
|
||||||
void*
|
void*
|
||||||
realloc(void* address, size_t newSize)
|
realloc_etc(void* address, size_t newSize, uint32 flags)
|
||||||
{
|
{
|
||||||
if (newSize == 0) {
|
if (newSize == 0) {
|
||||||
block_free(address, 0);
|
block_free(address, flags);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (address == NULL)
|
if (address == NULL)
|
||||||
return block_alloc(newSize, 0, 0);
|
return block_alloc(newSize, 0, flags);
|
||||||
|
|
||||||
size_t oldSize;
|
size_t oldSize;
|
||||||
ObjectCache* cache = MemoryManager::GetAllocationInfo(address, oldSize);
|
ObjectCache* cache = MemoryManager::GetAllocationInfo(address, oldSize);
|
||||||
@@ -276,18 +276,25 @@ realloc(void* address, size_t newSize)
|
|||||||
if (oldSize == newSize)
|
if (oldSize == newSize)
|
||||||
return address;
|
return address;
|
||||||
|
|
||||||
void* newBlock = block_alloc(newSize, 0, 0);
|
void* newBlock = block_alloc(newSize, 0, flags);
|
||||||
if (newBlock == NULL)
|
if (newBlock == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memcpy(newBlock, address, std::min(oldSize, newSize));
|
memcpy(newBlock, address, std::min(oldSize, newSize));
|
||||||
|
|
||||||
block_free(address, 0);
|
block_free(address, flags);
|
||||||
|
|
||||||
return newBlock;
|
return newBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void*
|
||||||
|
realloc(void* address, size_t newSize)
|
||||||
|
{
|
||||||
|
return realloc_etc(address, newSize, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // USE_SLAB_ALLOCATOR_FOR_MALLOC
|
#endif // USE_SLAB_ALLOCATOR_FOR_MALLOC
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -541,8 +541,7 @@ realloc_page_protections(uint8* pageProtections, size_t areaSize,
|
|||||||
uint32 allocationFlags)
|
uint32 allocationFlags)
|
||||||
{
|
{
|
||||||
size_t bytes = area_page_protections_size(areaSize);
|
size_t bytes = area_page_protections_size(areaSize);
|
||||||
// TODO: Implement realloc_etc and pass allocationFlags.
|
return (uint8*)realloc_etc(pageProtections, bytes, allocationFlags);
|
||||||
return (uint8*)realloc(pageProtections, bytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user