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

View File

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

View File

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

View File

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