libc: move mem alloc definition in header file

Prerequisite to fix Issue #1130, to be able to reinstantiate the libc
memory allocator several times if wanted.
This commit is contained in:
Alexander Boettcher 2014-04-25 10:26:55 +02:00 committed by Norman Feske
parent bab5bb67e7
commit 8da1c77908
2 changed files with 98 additions and 98 deletions

View File

@ -24,104 +24,6 @@
using namespace Genode;
namespace Libc {
class Mem_alloc_impl : public Mem_alloc
{
private:
enum {
MIN_CHUNK_SIZE = 4*1024, /* in machine words */
MAX_CHUNK_SIZE = 1024*1024
};
class Dataspace : public List<Dataspace>::Element
{
public:
Ram_dataspace_capability cap;
void *local_addr;
Dataspace(Ram_dataspace_capability c, void *a)
: cap(c), local_addr(a) {}
inline void * operator new(Genode::size_t, void* addr) {
return addr; }
inline void operator delete(void*) { }
};
class Dataspace_pool : public List<Dataspace>
{
private:
Ram_session *_ram_session; /* ram session for backing store */
Rm_session *_rm_session; /* region manager */
public:
/**
* Constructor
*/
Dataspace_pool(Ram_session *ram_session, Rm_session *rm_session):
_ram_session(ram_session), _rm_session(rm_session) { }
/**
* Destructor
*/
~Dataspace_pool();
/**
* Expand dataspace by specified size
*
* \param size number of bytes to add to the dataspace pool
* \param md_alloc allocator to expand. This allocator is also
* used for meta data allocation (only after
* being successfully expanded).
* \throw Rm_session::Invalid_dataspace,
* Rm_session::Region_conflict
* \return 0 on success or negative error code
*/
int expand(size_t size, Range_allocator *alloc);
void reassign_resources(Ram_session *ram, Rm_session *rm) {
_ram_session = ram, _rm_session = rm; }
};
Lock mutable _lock;
Dataspace_pool _ds_pool; /* list of dataspaces */
Allocator_avl _alloc; /* local allocator */
size_t _chunk_size;
/**
* Try to allocate block at our local allocator
*
* \return true on success
*
* This function is a utility used by 'alloc' to avoid
* code duplication.
*/
bool _try_local_alloc(size_t size, void **out_addr);
public:
Mem_alloc_impl()
:
_ds_pool(env()->ram_session(), env()->rm_session()),
_alloc(0),
_chunk_size(MIN_CHUNK_SIZE)
{ }
void *alloc(Genode::size_t size, Genode::size_t align_log2);
void free(void *ptr);
Genode::size_t size_at(void const *ptr) const;
};
}
using namespace Genode;;
Libc::Mem_alloc_impl::Dataspace_pool::~Dataspace_pool()
{
/* free all ram_dataspaces */

View File

@ -9,6 +9,11 @@
/* Genode includes */
#include <base/allocator.h>
#include <base/allocator_avl.h>
#include <base/env.h>
#include <util/list.h>
#include <ram_session/ram_session.h>
#include <rm_session/rm_session.h>
namespace Libc {
@ -23,6 +28,99 @@ namespace Libc {
* Return singleton instance of the memory allocator
*/
Mem_alloc *mem_alloc();
class Mem_alloc_impl : public Mem_alloc
{
private:
enum {
MIN_CHUNK_SIZE = 4*1024, /* in machine words */
MAX_CHUNK_SIZE = 1024*1024
};
class Dataspace : public Genode::List<Dataspace>::Element
{
public:
Genode::Ram_dataspace_capability cap;
void *local_addr;
Dataspace(Genode::Ram_dataspace_capability c, void *a)
: cap(c), local_addr(a) {}
inline void * operator new(Genode::size_t, void* addr) {
return addr; }
inline void operator delete(void*) { }
};
class Dataspace_pool : public Genode::List<Dataspace>
{
private:
Genode::Ram_session *_ram_session; /* ram session for backing store */
Genode::Rm_session *_rm_session; /* region manager */
public:
/**
* Constructor
*/
Dataspace_pool(Genode::Ram_session *ram_session, Genode::Rm_session *rm_session):
_ram_session(ram_session), _rm_session(rm_session) { }
/**
* Destructor
*/
~Dataspace_pool();
/**
* Expand dataspace by specified size
*
* \param size number of bytes to add to the dataspace pool
* \param md_alloc allocator to expand. This allocator is also
* used for meta data allocation (only after
* being successfully expanded).
* \throw Rm_session::Invalid_dataspace,
* Rm_session::Region_conflict
* \return 0 on success or negative error code
*/
int expand(Genode::size_t size, Genode::Range_allocator *alloc);
void reassign_resources(Genode::Ram_session *ram, Genode::Rm_session *rm) {
_ram_session = ram, _rm_session = rm; }
};
Genode::Lock mutable _lock;
Dataspace_pool _ds_pool; /* list of dataspaces */
Genode::Allocator_avl _alloc; /* local allocator */
Genode::size_t _chunk_size;
/**
* Try to allocate block at our local allocator
*
* \return true on success
*
* This function is a utility used by 'alloc' to avoid
* code duplication.
*/
bool _try_local_alloc(Genode::size_t size, void **out_addr);
public:
Mem_alloc_impl()
:
_ds_pool(Genode::env()->ram_session(), Genode::env()->rm_session()),
_alloc(0),
_chunk_size(MIN_CHUNK_SIZE)
{ }
void *alloc(Genode::size_t size, Genode::size_t align_log2);
void free(void *ptr);
Genode::size_t size_at(void const *ptr) const;
};
}
#endif /* _LIBC_MEM_ALLOC_H_ */