base: New Genode::Deallocator interface

Splitting the new Genode::Deallocator interface from the former
Genode::Allocator interface enables us to restrict the accessible
operations for code that is only supposed to release memory, but not
perform any allocations.

Additionally, this patch introduces variants of the 'new' operator
that takes a reference (as opposed to a pointer) to a Genode::Allocator
as argument.
This commit is contained in:
Norman Feske 2014-01-17 11:03:36 +01:00 committed by Christian Helmuth
parent 99979e09ed
commit 6ec36350d6
2 changed files with 184 additions and 181 deletions

View File

@ -19,14 +19,35 @@
namespace Genode {
class Allocator
struct Deallocator
{
public:
/**
* Free block a previously allocated block
*/
virtual void free(void *addr, size_t size) = 0;
/*********************
** Exception types **
*********************/
/**
* Return true if the size argument of 'free' is required
*
* The generic 'Allocator' interface requires the caller of 'free'
* to supply a valid size argument but not all implementations make
* use of this argument. If this function returns false, it is safe
* to call 'free' with an invalid size.
*
* Allocators that rely on the size argument must not be used for
* constructing objects whose constructors may throw exceptions.
* See the documentation of 'operator delete(void *, Allocator *)'
* below for more details.
*/
virtual bool need_size_for_free() const { return true; }
};
struct Allocator : Deallocator
{
/**
* Exception type
*/
class Out_of_memory : public Exception { };
/**
@ -60,11 +81,6 @@ namespace Genode {
return ret;
}
/**
* Free block a previously allocated block
*/
virtual void free(void *addr, size_t size) = 0;
/**
* Return total amount of backing store consumed by the allocator
*/
@ -75,21 +91,6 @@ namespace Genode {
*/
virtual size_t overhead(size_t size) = 0;
/**
* Return true if the size argument of 'free' is required
*
* The generic 'Allocator' interface requires the caller of 'free'
* to supply a valid size argument but not all implementations make
* use of this argument. If this function returns false, it is safe
* to call 'free' with an invalid size.
*
* Allocators that rely on the size argument must not be used for
* constructing objects whose constructors may throw exceptions.
* See the documentation of 'operator delete(void *, Allocator *)'
* below for more details.
*/
virtual bool need_size_for_free() const { return true; }
/***************************
** Convenience functions **
@ -113,10 +114,8 @@ namespace Genode {
};
class Range_allocator : public Allocator
struct Range_allocator : Allocator
{
public:
/**
* Destructor
*/
@ -216,7 +215,7 @@ namespace Genode {
* \param obj object to destroy
*/
template <typename T>
void destroy(Allocator *alloc, T *obj)
void destroy(Deallocator *dealloc, T *obj)
{
if (!obj)
return;
@ -225,12 +224,15 @@ namespace Genode {
obj->~T();
/* free memory at the allocator */
alloc->free(obj, sizeof(T));
dealloc->free(obj, sizeof(T));
}
}
void *operator new (Genode::size_t size, Genode::Allocator *allocator);
void *operator new [] (Genode::size_t size, Genode::Allocator *allocator);
void *operator new (Genode::size_t, Genode::Allocator *);
void *operator new [] (Genode::size_t, Genode::Allocator *);
void *operator new (Genode::size_t, Genode::Allocator &);
void *operator new [] (Genode::size_t, Genode::Allocator &);
/**

View File

@ -14,25 +14,26 @@
#include <base/printf.h>
#include <base/allocator.h>
using Genode::size_t;
using Genode::Allocator;
void *operator new(Genode::size_t size, Genode::Allocator *allocator)
static void *try_alloc(Allocator *alloc, size_t size)
{
if (!allocator)
throw Genode::Allocator::Out_of_memory();
if (!alloc)
throw Allocator::Out_of_memory();
return allocator->alloc(size);
}
void *operator new [] (Genode::size_t size, Genode::Allocator *allocator)
{
if (!allocator)
throw Genode::Allocator::Out_of_memory();
return allocator->alloc(size);
return alloc->alloc(size);
}
void operator delete (void *ptr, Genode::Allocator *alloc)
void *operator new (size_t s, Allocator *a) { return try_alloc(a, s); }
void *operator new [] (size_t s, Allocator *a) { return try_alloc(a, s); }
void *operator new (size_t s, Allocator &a) { return a.alloc(s); }
void *operator new [] (size_t s, Allocator &a) { return a.alloc(s); }
void operator delete (void *ptr, Allocator *alloc)
{
/*
* Warn on the attempt to use an allocator that relies on the size