2012-05-18 17:04:52 +02:00
|
|
|
/*
|
|
|
|
* \brief Allocator for anonymous memory used by libc
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2012-05-18
|
|
|
|
*/
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2019 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LIBC__INTERNAL__MEM_ALLOC_H_
|
|
|
|
#define _LIBC__INTERNAL__MEM_ALLOC_H_
|
2012-05-18 17:04:52 +02:00
|
|
|
|
|
|
|
/* Genode includes */
|
2019-01-21 15:59:00 +01:00
|
|
|
#include <base/ram_allocator.h>
|
2014-04-25 10:26:55 +02:00
|
|
|
#include <base/allocator_avl.h>
|
|
|
|
#include <base/env.h>
|
|
|
|
#include <util/list.h>
|
|
|
|
#include <rm_session/rm_session.h>
|
2012-05-18 17:04:52 +02:00
|
|
|
|
|
|
|
namespace Libc {
|
|
|
|
|
|
|
|
struct Mem_alloc
|
|
|
|
{
|
|
|
|
virtual void *alloc(Genode::size_t size, Genode::size_t align_log2) = 0;
|
|
|
|
virtual void free(void *ptr) = 0;
|
2013-08-26 17:26:13 +02:00
|
|
|
virtual Genode::size_t size_at(void const *ptr) const = 0;
|
2012-05-18 17:04:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return singleton instance of the memory allocator
|
|
|
|
*/
|
2017-10-18 14:20:49 +02:00
|
|
|
Mem_alloc *mem_alloc(bool executable = false);
|
2014-04-25 10:26:55 +02:00
|
|
|
|
|
|
|
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;
|
2019-01-21 15:59:00 +01:00
|
|
|
void *local_addr;
|
2014-04-25 10:26:55 +02:00
|
|
|
|
|
|
|
Dataspace(Genode::Ram_dataspace_capability c, void *a)
|
|
|
|
: cap(c), local_addr(a) {}
|
|
|
|
|
2016-09-15 14:40:37 +02:00
|
|
|
inline void * operator new(__SIZE_TYPE__, void* addr) {
|
2014-04-25 10:26:55 +02:00
|
|
|
return addr; }
|
|
|
|
|
|
|
|
inline void operator delete(void*) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Dataspace_pool : public Genode::List<Dataspace>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2019-01-21 15:59:00 +01:00
|
|
|
Genode::Ram_allocator *_ram; /* ram session for backing store */
|
|
|
|
Genode::Region_map *_region_map; /* region map of address space */
|
|
|
|
bool const _executable; /* whether to allocate executable dataspaces */
|
2014-04-25 10:26:55 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2019-01-21 15:59:00 +01:00
|
|
|
Dataspace_pool(Genode::Ram_allocator *ram,
|
2017-10-18 14:14:07 +02:00
|
|
|
Genode::Region_map *rm, bool executable) :
|
2019-01-21 15:59:00 +01:00
|
|
|
_ram(ram), _region_map(rm),
|
2017-10-18 14:14:07 +02:00
|
|
|
_executable(executable)
|
|
|
|
{ }
|
2014-04-25 10:26:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
2016-04-15 15:19:22 +02:00
|
|
|
* \throw Region_map::Invalid_dataspace,
|
|
|
|
* Region_map::Region_conflict
|
2014-04-25 10:26:55 +02:00
|
|
|
* \return 0 on success or negative error code
|
|
|
|
*/
|
|
|
|
int expand(Genode::size_t size, Genode::Range_allocator *alloc);
|
|
|
|
|
2019-01-21 15:59:00 +01:00
|
|
|
void reassign_resources(Genode::Ram_allocator *ram, Genode::Region_map *rm) {
|
|
|
|
_ram = ram, _region_map = rm; }
|
2014-04-25 10:26:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Genode::Lock mutable _lock;
|
2016-04-15 15:19:22 +02:00
|
|
|
Dataspace_pool _ds_pool; /* list of dataspaces */
|
2014-04-25 10:26:55 +02:00
|
|
|
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:
|
|
|
|
|
2017-10-18 14:14:07 +02:00
|
|
|
Mem_alloc_impl(Genode::Region_map &rm,
|
2019-01-21 15:59:00 +01:00
|
|
|
Genode::Ram_allocator &ram,
|
2017-10-18 14:14:07 +02:00
|
|
|
bool executable = false)
|
2014-04-25 10:26:55 +02:00
|
|
|
:
|
2017-10-18 14:14:07 +02:00
|
|
|
_ds_pool(&ram, &rm, executable),
|
2014-04-25 10:26:55 +02:00
|
|
|
_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;
|
|
|
|
};
|
2012-05-18 17:04:52 +02:00
|
|
|
}
|
|
|
|
|
libc: split task.cc into multiple files
This patch is the first step of re-organizing the internal structure of
the libc. The original version involved many direct calls of global
functions (often with side effects) across compilation units, which
made the control flow (e.g., the initialization sequence) hard to
follow.
The new version replaces those ad-hoc interactions with dedicated
interfaces (like suspend.h, resume.h, select.h, current_time.h). The
underlying facilities are provided by the central Libc::Kernel and
selectively propagated to the various compilation units. The latter is
done by a sequence of 'init_*' calls, which eventually will be replaced
by constructor calls.
The addition of new headers increases the chance for name clashes with
existing (public) headers. To disambiguate libc-internal header files
from public headers, this patch moves the former into a new 'internal/'
subdirectory. This makes the include directives easier to follow and the
libc's source-tree structure more tidy.
There are still a few legacies left, which cannot easily be removed
right now (e.g., because noux relies on them). However, the patch moves
those bad apples to legacy.h and legacy.cc, which highlights the
deprecation of those functions.
Issue #3497
2019-09-18 20:19:10 +02:00
|
|
|
#endif /* _LIBC__INTERNAL__MEM_ALLOC_H_ */
|