Move malloc into kutil

This commit is contained in:
Justin C. Miller
2018-05-10 09:49:57 -07:00
parent 045bede481
commit a1bc76f305
6 changed files with 46 additions and 35 deletions

View File

@@ -11,6 +11,18 @@ void * operator new (size_t, void *p) noexcept;
namespace kutil {
/// Allocate memory. Note: this needs to be implemented
/// by the kernel, or other program using this library.
/// \arg n The number of bytes to allocate
/// \returns The allocated memory
void * malloc(size_t n);
/// Free memory allocated by malloc(). Note: this needs
/// to be implemented by the kernel, or other program
/// using this library.
/// \arg p A pointer previously returned by malloc()
void free(void *p);
/// Fill memory with the given value.
/// \arg p The beginning of the memory area to fill
/// \arg v The byte value to fill memory with
@@ -25,15 +37,29 @@ void * memset(void *p, uint8_t v, size_t n);
/// \returns A pointer to the destination memory
void * memcpy(void *dest, void *src, size_t n);
/// Read a value of type T from a location in memory
/// \arg p The location in memory to read
/// \returns The value at the given location cast to T
template <typename T>
inline T read_from(const void *p) { return *reinterpret_cast<const T *>(p); }
template <typename T>
inline T * offset_pointer(T *p, size_t offset)
inline T read_from(const void *p)
{
return reinterpret_cast<T *>(reinterpret_cast<addr_t>(p) + offset);
return *reinterpret_cast<const T *>(p);
}
/// Get a pointer that's offset from another pointer
/// \arg p The base pointer
/// \arg n The offset in bytes
/// \returns The offset pointer
template <typename T>
inline T * offset_pointer(T *p, size_t n)
{
return reinterpret_cast<T *>(reinterpret_cast<addr_t>(p) + n);
}
/// Return a pointer with the given bits masked out
/// \arg p The original pointer
/// \arg mask A bitmask of bits to clear from p
/// \returns The masked pointer
template <typename T>
inline T* mask_pointer(T *p, addr_t mask)
{