2018-10-17 10:55:43 +02:00
|
|
|
#include "kmalloc.h"
|
2018-10-25 10:00:37 +02:00
|
|
|
|
|
|
|
#ifndef SERENITY
|
|
|
|
#include <cstdio>
|
2018-10-10 15:11:43 +02:00
|
|
|
#include <cstdlib>
|
2018-10-25 10:00:37 +02:00
|
|
|
#endif
|
2018-10-10 15:11:43 +02:00
|
|
|
|
2018-10-25 10:00:37 +02:00
|
|
|
#if defined(SERENITY) && defined(USERLAND)
|
2018-10-10 15:11:43 +02:00
|
|
|
#define USE_SYSTEM_MALLOC
|
2018-10-25 10:00:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define USE_SYSTEM_MALLOC
|
|
|
|
|
|
|
|
#ifndef USE_SYSTEM_MALLOC
|
|
|
|
#include "SimpleMalloc.h"
|
|
|
|
#endif
|
2018-10-10 15:11:43 +02:00
|
|
|
|
|
|
|
#ifdef USE_SYSTEM_MALLOC
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-10-16 13:59:28 +02:00
|
|
|
void* kcalloc(size_t nmemb, size_t size)
|
2018-10-10 15:11:43 +02:00
|
|
|
{
|
|
|
|
return calloc(nmemb, size);
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:59:28 +02:00
|
|
|
void* kmalloc(size_t size)
|
2018-10-10 15:11:43 +02:00
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void kfree(void* ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:59:28 +02:00
|
|
|
void* krealloc(void* ptr, size_t size)
|
2018-10-10 15:11:43 +02:00
|
|
|
{
|
|
|
|
return realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
2018-10-31 23:19:15 +01:00
|
|
|
void* kmalloc_eternal(size_t size)
|
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:11:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 14:29:00 +01:00
|
|
|
void* operator new(size_t size)
|
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new[](size_t size)
|
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr, size_t)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr, size_t)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:11:43 +02:00
|
|
|
#else
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2018-10-16 13:59:28 +02:00
|
|
|
void* kcalloc(size_t nmemb, size_t size)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
if (!nmemb || !size)
|
|
|
|
return nullptr;
|
2019-01-31 17:31:23 +01:00
|
|
|
return SimpleMalloc::allocate_zeroed(nmemb * size);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 13:59:28 +02:00
|
|
|
void* kmalloc(size_t size)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
if (!size)
|
|
|
|
return nullptr;
|
|
|
|
return SimpleMalloc::allocate(size);
|
|
|
|
}
|
|
|
|
|
2018-10-31 23:19:15 +01:00
|
|
|
void* kmalloc_eternal(size_t size)
|
|
|
|
{
|
|
|
|
if (!size)
|
|
|
|
return nullptr;
|
|
|
|
return SimpleMalloc::allocate(size);
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
void kfree(void* ptr)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
SimpleMalloc::free((byte*)ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:59:28 +02:00
|
|
|
void* krealloc(void* ptr, size_t size)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return ptr;
|
|
|
|
return SimpleMalloc::reallocate((byte*)ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new(std::size_t size)
|
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new[](std::size_t size)
|
|
|
|
{
|
|
|
|
return kmalloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete(void* ptr, size_t)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr, size_t)
|
|
|
|
{
|
|
|
|
return kfree(ptr);
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:11:43 +02:00
|
|
|
#endif
|
|
|
|
|