libc: remove deprecated API from malloc()

This commit is contained in:
Christian Helmuth
2017-05-12 23:10:41 +02:00
parent 90b5679332
commit 6d25c614d3
3 changed files with 33 additions and 22 deletions

View File

@ -39,6 +39,11 @@ namespace Libc {
* Set libc config node * Set libc config node
*/ */
void libc_config_init(Genode::Xml_node node); void libc_config_init(Genode::Xml_node node);
/**
* Malloc allocator
*/
void init_malloc(Genode::Allocator &heap);
} }
#endif /* _LIBC_INIT_H_ */ #endif /* _LIBC_INIT_H_ */

View File

@ -26,6 +26,11 @@ extern "C" {
#include <stdlib.h> #include <stdlib.h>
} }
/* libc-internal includes */
#include "libc_init.h"
#include <base/internal/unmanaged_singleton.h>
typedef unsigned long Block_header; typedef unsigned long Block_header;
namespace Genode { namespace Genode {
@ -76,7 +81,7 @@ class Malloc : public Genode::Allocator
NUM_SLABS = (SLAB_STOP - SLAB_START) + 1 NUM_SLABS = (SLAB_STOP - SLAB_START) + 1
}; };
Genode::Allocator *_backing_store; /* back-end allocator */ Genode::Allocator &_backing_store; /* back-end allocator */
Genode::Slab_alloc *_allocator[NUM_SLABS]; /* slab allocators */ Genode::Slab_alloc *_allocator[NUM_SLABS]; /* slab allocators */
Genode::Lock _lock; Genode::Lock _lock;
@ -96,11 +101,11 @@ class Malloc : public Genode::Allocator
public: public:
Malloc(Genode::Allocator *backing_store) : _backing_store(backing_store) Malloc(Genode::Allocator &backing_store) : _backing_store(backing_store)
{ {
for (unsigned i = SLAB_START; i <= SLAB_STOP; i++) { for (unsigned i = SLAB_START; i <= SLAB_STOP; i++) {
_allocator[i - SLAB_START] = new (backing_store) _allocator[i - SLAB_START] = new (backing_store)
Genode::Slab_alloc(1U << i, backing_store); Genode::Slab_alloc(1U << i, &backing_store);
} }
} }
@ -130,7 +135,7 @@ class Malloc : public Genode::Allocator
/* use backing store if requested memory is larger than largest slab */ /* use backing store if requested memory is larger than largest slab */
if (msb > SLAB_STOP) { if (msb > SLAB_STOP) {
if (!(_backing_store->alloc(real_size, &addr))) if (!(_backing_store.alloc(real_size, &addr)))
return false; return false;
} }
else else
@ -150,7 +155,7 @@ class Malloc : public Genode::Allocator
unsigned long real_size = *addr; unsigned long real_size = *addr;
if (real_size > (1U << SLAB_STOP)) if (real_size > (1U << SLAB_STOP))
_backing_store->free(addr, real_size); _backing_store.free(addr, real_size);
else { else {
unsigned long msb = _slab_log2(real_size); unsigned long msb = _slab_log2(real_size);
_allocator[msb - SLAB_START]->free(addr); _allocator[msb - SLAB_START]->free(addr);
@ -162,7 +167,7 @@ class Malloc : public Genode::Allocator
size += sizeof(Block_header); size += sizeof(Block_header);
if (size > (1U << SLAB_STOP)) if (size > (1U << SLAB_STOP))
return _backing_store->overhead(size); return _backing_store.overhead(size);
return _allocator[_slab_log2(size) - SLAB_START]->overhead(size); return _allocator[_slab_log2(size) - SLAB_START]->overhead(size);
} }
@ -171,23 +176,13 @@ class Malloc : public Genode::Allocator
}; };
static Genode::Allocator *allocator() static Malloc *mallocator;
{
static bool constructed = 0;
static char placeholder[sizeof(Malloc)];
if (!constructed) {
Genode::construct_at<Malloc>(placeholder, Genode::env()->heap());
constructed = 1;
}
return reinterpret_cast<Malloc *>(placeholder);
}
extern "C" void *malloc(size_t size) extern "C" void *malloc(size_t size)
{ {
void *addr; void *addr;
return allocator()->alloc(size, &addr) ? addr : 0; return mallocator->alloc(size, &addr) ? addr : 0;
} }
@ -204,7 +199,7 @@ extern "C" void free(void *ptr)
{ {
if (!ptr) return; if (!ptr) return;
allocator()->free(ptr, 0); mallocator->free(ptr, 0);
} }
@ -238,3 +233,9 @@ extern "C" void *realloc(void *ptr, size_t size)
return new_addr; return new_addr;
} }
void Libc::init_malloc(Genode::Allocator &heap)
{
mallocator = unmanaged_singleton<Malloc>(heap);
}

View File

@ -361,7 +361,7 @@ struct Libc::Kernel
private: private:
Genode::Env &_env; Genode::Env &_env;
Genode::Heap _heap { _env.ram(), _env.rm() }; Genode::Allocator &_heap;
Io_response_handler _io_response_handler; Io_response_handler _io_response_handler;
Env_implementation _libc_env { _env, _heap, _io_response_handler }; Env_implementation _libc_env { _env, _heap, _io_response_handler };
Vfs_plugin _vfs { _libc_env, _heap }; Vfs_plugin _vfs { _libc_env, _heap };
@ -571,7 +571,8 @@ struct Libc::Kernel
public: public:
Kernel(Genode::Env &env) : _env(env) { } Kernel(Genode::Env &env, Genode::Allocator &heap)
: _env(env), _heap(heap) { }
~Kernel() { Genode::error(__PRETTY_FUNCTION__, " should not be executed!"); } ~Kernel() { Genode::error(__PRETTY_FUNCTION__, " should not be executed!"); }
@ -862,12 +863,16 @@ void Component::construct(Genode::Env &env)
static char *null_env = nullptr; static char *null_env = nullptr;
if (!environ) environ = &null_env; if (!environ) environ = &null_env;
Genode::Allocator &heap =
*unmanaged_singleton<Genode::Heap>(env.ram(), env.rm());
/* pass Genode::Env to libc subsystems that depend on it */ /* pass Genode::Env to libc subsystems that depend on it */
Libc::init_malloc(heap);
Libc::init_mem_alloc(env); Libc::init_mem_alloc(env);
Libc::init_dl(env); Libc::init_dl(env);
Libc::sysctl_init(env); Libc::sysctl_init(env);
kernel = unmanaged_singleton<Libc::Kernel>(env); kernel = unmanaged_singleton<Libc::Kernel>(env, heap);
Libc::libc_config_init(kernel->libc_env().libc_config()); Libc::libc_config_init(kernel->libc_env().libc_config());