libc: use non-anonymous 'operator new' and destroy()

This removes implementations of and also references to anonymous new and
delete operators from the libc implementation. As allocators for
new/delete Libc::Allocator instances are used, which (paradoxically) map
to libc malloc/free.
This commit is contained in:
Christian Helmuth 2019-05-23 12:30:18 +02:00
parent 9288fe63ad
commit 6c42bd4dd3
5 changed files with 42 additions and 27 deletions

View File

@ -17,11 +17,16 @@
#include <base/lock.h> #include <base/lock.h>
#include <base/lock_guard.h> #include <base/lock_guard.h>
#include <base/thread.h> #include <base/thread.h>
#include <libc/allocator.h>
/* Libc includes */ /* Libc includes */
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
static Libc::Allocator object_alloc;
/* /*
* A reader-preferring implementation of a readers-writer lock as described * A reader-preferring implementation of a readers-writer lock as described
* in Michael Raynal, "Concurrent Programming: Algorithms, Principles, and * in Michael Raynal, "Concurrent Programming: Algorithms, Principles, and
@ -100,7 +105,7 @@ extern "C" {
try { try {
Genode::Lock::Guard g(rwlock_init_lock); Genode::Lock::Guard g(rwlock_init_lock);
*rwlock = new struct pthread_rwlock(); *rwlock = new (object_alloc) struct pthread_rwlock();
return 0; return 0;
} catch (...) { return ENOMEM; } } catch (...) { return ENOMEM; }
} }
@ -112,7 +117,7 @@ extern "C" {
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) int pthread_rwlock_destroy(pthread_rwlock_t *rwlock)
{ {
delete *rwlock; destroy(object_alloc, *rwlock);
return 0; return 0;
} }
@ -149,7 +154,7 @@ extern "C" {
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr) int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
{ {
*attr = new struct pthread_rwlockattr(); *attr = new (object_alloc) struct pthread_rwlockattr();
return 0; return 0;
} }
@ -170,7 +175,7 @@ extern "C" {
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr) int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
{ {
delete *attr; destroy(object_alloc, *attr);
return 0; return 0;
} }

View File

@ -201,7 +201,7 @@ static void select_notify()
} }
static void print(Genode::Output &output, timeval *tv) static inline void print(Genode::Output &output, timeval *tv)
{ {
if (!tv) { if (!tv) {
print(output, "nullptr"); print(output, "nullptr");

View File

@ -15,9 +15,13 @@
#include <base/log.h> #include <base/log.h>
#include <base/semaphore.h> #include <base/semaphore.h>
#include <semaphore.h> #include <semaphore.h>
#include <libc/allocator.h>
using namespace Genode; using namespace Genode;
static Libc::Allocator object_alloc;
extern "C" { extern "C" {
/* /*
@ -39,7 +43,7 @@ extern "C" {
int sem_destroy(sem_t *sem) int sem_destroy(sem_t *sem)
{ {
delete *sem; destroy(object_alloc, *sem);
return 0; return 0;
} }
@ -53,7 +57,7 @@ extern "C" {
int sem_init(sem_t *sem, int pshared, unsigned int value) int sem_init(sem_t *sem, int pshared, unsigned int value)
{ {
*sem = new struct sem(value); *sem = new (object_alloc) struct sem(value);
return 0; return 0;
} }

View File

@ -16,6 +16,7 @@
#include <base/sleep.h> #include <base/sleep.h>
#include <base/thread.h> #include <base/thread.h>
#include <util/list.h> #include <util/list.h>
#include <libc/allocator.h>
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
@ -28,8 +29,7 @@
using namespace Genode; using namespace Genode;
void * operator new(__SIZE_TYPE__ size) { return malloc(size); } static Libc::Allocator object_alloc;
void operator delete (void * p) { return free(p); }
static Env *_env_ptr; /* solely needed to spawn the timeout thread for the static Env *_env_ptr; /* solely needed to spawn the timeout thread for the
@ -172,7 +172,7 @@ extern "C" {
{ {
thread->join(retval); thread->join(retval);
delete thread; destroy(object_alloc, thread);
return 0; return 0;
} }
@ -183,7 +183,7 @@ extern "C" {
if (!attr) if (!attr)
return EINVAL; return EINVAL;
*attr = new pthread_attr; *attr = new (object_alloc) pthread_attr;
return 0; return 0;
} }
@ -194,7 +194,7 @@ extern "C" {
if (!attr || !*attr) if (!attr || !*attr)
return EINVAL; return EINVAL;
delete *attr; destroy(object_alloc, *attr);
*attr = 0; *attr = 0;
return 0; return 0;
@ -251,7 +251,7 @@ extern "C" {
* of the pthread object would also destruct the 'Thread' of the main * of the pthread object would also destruct the 'Thread' of the main
* thread. * thread.
*/ */
static pthread *main = new pthread(*Thread::myself()); static pthread *main = new (object_alloc) pthread(*Thread::myself());
return main; return main;
} }
@ -498,7 +498,7 @@ extern "C" {
if (!attr) if (!attr)
return EINVAL; return EINVAL;
*attr = new pthread_mutex_attr; *attr = new (object_alloc) pthread_mutex_attr;
return 0; return 0;
} }
@ -509,7 +509,7 @@ extern "C" {
if (!attr || !*attr) if (!attr || !*attr)
return EINVAL; return EINVAL;
delete *attr; destroy(object_alloc, *attr);
*attr = 0; *attr = 0;
return 0; return 0;
@ -533,7 +533,7 @@ extern "C" {
if (!mutex) if (!mutex)
return EINVAL; return EINVAL;
*mutex = new pthread_mutex(attr); *mutex = new (object_alloc) pthread_mutex(attr);
return 0; return 0;
} }
@ -544,7 +544,7 @@ extern "C" {
if ((!mutex) || (*mutex == PTHREAD_MUTEX_INITIALIZER)) if ((!mutex) || (*mutex == PTHREAD_MUTEX_INITIALIZER))
return EINVAL; return EINVAL;
delete *mutex; destroy(object_alloc, *mutex);
*mutex = PTHREAD_MUTEX_INITIALIZER; *mutex = PTHREAD_MUTEX_INITIALIZER;
return 0; return 0;
@ -655,7 +655,7 @@ extern "C" {
try { try {
Genode::Lock::Guard g(cond_init_lock); Genode::Lock::Guard g(cond_init_lock);
*cond = new pthread_cond; *cond = new (object_alloc) pthread_cond;
return 0; return 0;
} catch (...) { return ENOMEM; } } catch (...) { return ENOMEM; }
} }
@ -673,7 +673,7 @@ extern "C" {
if (!cond || !*cond) if (!cond || !*cond)
return EINVAL; return EINVAL;
delete *cond; destroy(object_alloc, *cond);
*cond = 0; *cond = 0;
return 0; return 0;
@ -854,7 +854,7 @@ extern "C" {
* thread to mark the key slot as used. * thread to mark the key slot as used.
*/ */
if (!key_list[k].first()) { if (!key_list[k].first()) {
Key_element *key_element = new Key_element(Thread::myself(), 0); Key_element *key_element = new (object_alloc) Key_element(Thread::myself(), 0);
key_list[k].insert(key_element); key_list[k].insert(key_element);
*key = k; *key = k;
return 0; return 0;
@ -874,7 +874,7 @@ extern "C" {
while (Key_element * element = key_list[key].first()) { while (Key_element * element = key_list[key].first()) {
key_list[key].remove(element); key_list[key].remove(element);
delete element; destroy(object_alloc, element);
} }
return 0; return 0;
@ -898,7 +898,7 @@ extern "C" {
} }
/* key element does not exist yet - create a new one */ /* key element does not exist yet - create a new one */
Key_element *key_element = new Key_element(Thread::myself(), value); Key_element *key_element = new (object_alloc) Key_element(Thread::myself(), value);
key_list[key].insert(key_element); key_list[key].insert(key_element);
return 0; return 0;
} }
@ -929,7 +929,7 @@ extern "C" {
return EINTR; return EINTR;
if (!once->mutex) { if (!once->mutex) {
pthread_mutex_t p = new pthread_mutex(0); pthread_mutex_t p = new (object_alloc) pthread_mutex(0);
/* be paranoid */ /* be paranoid */
if (!p) if (!p)
return EINTR; return EINTR;
@ -948,7 +948,7 @@ extern "C" {
* free our mutex since it is not used. * free our mutex since it is not used.
*/ */
if (p) if (p)
delete p; destroy(object_alloc, p);
} }
once->mutex->lock(); once->mutex->lock();

View File

@ -14,18 +14,24 @@
* under the terms of the GNU Affero General Public License version 3. * under the terms of the GNU Affero General Public License version 3.
*/ */
#include <libc/allocator.h>
#include "thread_create.h" #include "thread_create.h"
#include "thread.h" #include "thread.h"
#include <errno.h> #include <errno.h>
static Libc::Allocator object_alloc;
int Libc::pthread_create(pthread_t *thread, int Libc::pthread_create(pthread_t *thread,
void *(*start_routine) (void *), void *arg, void *(*start_routine) (void *), void *arg,
size_t stack_size, char const * name, size_t stack_size, char const * name,
Genode::Cpu_session * cpu, Genode::Affinity::Location location) Genode::Cpu_session * cpu, Genode::Affinity::Location location)
{ {
pthread_t thread_obj = new pthread(start_routine, arg, pthread_t thread_obj = new (object_alloc)
stack_size, name, cpu, location); pthread(start_routine, arg,
stack_size, name, cpu, location);
if (!thread_obj) if (!thread_obj)
return EAGAIN; return EAGAIN;
@ -39,7 +45,7 @@ int Libc::pthread_create(pthread_t *thread,
int Libc::pthread_create(pthread_t *thread, Genode::Thread &t) int Libc::pthread_create(pthread_t *thread, Genode::Thread &t)
{ {
pthread_t thread_obj = new pthread(t); pthread_t thread_obj = new (object_alloc) pthread(t);
if (!thread_obj) if (!thread_obj)
return EAGAIN; return EAGAIN;