From 6d25c614d38186960a6096c42f44b1dfecad74b6 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Fri, 12 May 2017 23:10:41 +0200 Subject: [PATCH] libc: remove deprecated API from malloc() --- repos/libports/src/lib/libc/libc_init.h | 5 ++++ repos/libports/src/lib/libc/malloc.cc | 39 +++++++++++++------------ repos/libports/src/lib/libc/task.cc | 11 +++++-- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/repos/libports/src/lib/libc/libc_init.h b/repos/libports/src/lib/libc/libc_init.h index d00da588d..2eb90f862 100644 --- a/repos/libports/src/lib/libc/libc_init.h +++ b/repos/libports/src/lib/libc/libc_init.h @@ -39,6 +39,11 @@ namespace Libc { * Set libc config node */ void libc_config_init(Genode::Xml_node node); + + /** + * Malloc allocator + */ + void init_malloc(Genode::Allocator &heap); } #endif /* _LIBC_INIT_H_ */ diff --git a/repos/libports/src/lib/libc/malloc.cc b/repos/libports/src/lib/libc/malloc.cc index cfdb77c9f..5a5ee07ce 100644 --- a/repos/libports/src/lib/libc/malloc.cc +++ b/repos/libports/src/lib/libc/malloc.cc @@ -26,6 +26,11 @@ extern "C" { #include } +/* libc-internal includes */ +#include "libc_init.h" +#include + + typedef unsigned long Block_header; namespace Genode { @@ -76,7 +81,7 @@ class Malloc : public Genode::Allocator 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::Lock _lock; @@ -96,11 +101,11 @@ class Malloc : public Genode::Allocator 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++) { _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 */ if (msb > SLAB_STOP) { - if (!(_backing_store->alloc(real_size, &addr))) + if (!(_backing_store.alloc(real_size, &addr))) return false; } else @@ -150,7 +155,7 @@ class Malloc : public Genode::Allocator unsigned long real_size = *addr; if (real_size > (1U << SLAB_STOP)) - _backing_store->free(addr, real_size); + _backing_store.free(addr, real_size); else { unsigned long msb = _slab_log2(real_size); _allocator[msb - SLAB_START]->free(addr); @@ -162,7 +167,7 @@ class Malloc : public Genode::Allocator size += sizeof(Block_header); if (size > (1U << SLAB_STOP)) - return _backing_store->overhead(size); + return _backing_store.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 bool constructed = 0; - static char placeholder[sizeof(Malloc)]; - if (!constructed) { - Genode::construct_at(placeholder, Genode::env()->heap()); - constructed = 1; - } - - return reinterpret_cast(placeholder); -} +static Malloc *mallocator; extern "C" void *malloc(size_t size) { 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; - allocator()->free(ptr, 0); + mallocator->free(ptr, 0); } @@ -238,3 +233,9 @@ extern "C" void *realloc(void *ptr, size_t size) return new_addr; } + + +void Libc::init_malloc(Genode::Allocator &heap) +{ + mallocator = unmanaged_singleton(heap); +} diff --git a/repos/libports/src/lib/libc/task.cc b/repos/libports/src/lib/libc/task.cc index 88f1e4da5..4494fa294 100644 --- a/repos/libports/src/lib/libc/task.cc +++ b/repos/libports/src/lib/libc/task.cc @@ -361,7 +361,7 @@ struct Libc::Kernel private: Genode::Env &_env; - Genode::Heap _heap { _env.ram(), _env.rm() }; + Genode::Allocator &_heap; Io_response_handler _io_response_handler; Env_implementation _libc_env { _env, _heap, _io_response_handler }; Vfs_plugin _vfs { _libc_env, _heap }; @@ -571,7 +571,8 @@ struct Libc::Kernel 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!"); } @@ -862,12 +863,16 @@ void Component::construct(Genode::Env &env) static char *null_env = nullptr; if (!environ) environ = &null_env; + Genode::Allocator &heap = + *unmanaged_singleton(env.ram(), env.rm()); + /* pass Genode::Env to libc subsystems that depend on it */ + Libc::init_malloc(heap); Libc::init_mem_alloc(env); Libc::init_dl(env); Libc::sysctl_init(env); - kernel = unmanaged_singleton(env); + kernel = unmanaged_singleton(env, heap); Libc::libc_config_init(kernel->libc_env().libc_config());