2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief File-descriptor allocator implementation
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2010-01-21
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2013-01-10 21:44:47 +01:00
|
|
|
* Copyright (C) 2010-2013 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <base/env.h>
|
|
|
|
#include <base/printf.h>
|
|
|
|
|
|
|
|
/* libc plugin interface */
|
|
|
|
#include <libc-plugin/fd_alloc.h>
|
|
|
|
|
|
|
|
namespace Libc {
|
|
|
|
|
|
|
|
File_descriptor_allocator *file_descriptor_allocator()
|
|
|
|
{
|
|
|
|
static File_descriptor_allocator _file_descriptor_allocator;
|
|
|
|
return &_file_descriptor_allocator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
using namespace Libc;
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
|
|
|
File_descriptor_allocator::File_descriptor_allocator()
|
|
|
|
: Allocator_avl_tpl<File_descriptor>(Genode::env()->heap())
|
|
|
|
{
|
|
|
|
add_range(0, MAX_NUM_FDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-08 14:44:31 +02:00
|
|
|
File_descriptor *File_descriptor_allocator::alloc(Plugin *plugin,
|
|
|
|
Plugin_context *context,
|
|
|
|
int libc_fd)
|
2011-12-22 16:19:25 +01:00
|
|
|
{
|
|
|
|
/* we use addresses returned by the allocator as file descriptors */
|
2014-09-09 14:32:31 +02:00
|
|
|
addr_t addr = (libc_fd <= ANY_FD ? ANY_FD : libc_fd);
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
/* allocate fresh fd if the default value for 'libc_fd' was specified */
|
|
|
|
bool alloc_ok = false;
|
2014-09-09 14:32:31 +02:00
|
|
|
if (libc_fd <= ANY_FD)
|
2011-12-22 16:19:25 +01:00
|
|
|
alloc_ok = Allocator_avl_base::alloc(1, reinterpret_cast<void**>(&addr));
|
|
|
|
else
|
2012-11-28 22:50:08 +01:00
|
|
|
alloc_ok = (Allocator_avl_base::alloc_addr(1, addr).is_ok());
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
if (!alloc_ok) {
|
|
|
|
PERR("could not allocate libc_fd %d%s",
|
|
|
|
libc_fd, libc_fd == ANY_FD ? " (any)" : "");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
File_descriptor *fdo = metadata((void*)addr);
|
|
|
|
fdo->libc_fd = (int)addr;
|
2012-10-08 14:44:31 +02:00
|
|
|
fdo->fd_path = 0;
|
2011-12-22 16:19:25 +01:00
|
|
|
fdo->plugin = plugin;
|
|
|
|
fdo->context = context;
|
2013-11-13 13:36:18 +01:00
|
|
|
fdo->lock = Lock(Lock::UNLOCKED);
|
2011-12-22 16:19:25 +01:00
|
|
|
return fdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void File_descriptor_allocator::free(File_descriptor *fdo)
|
|
|
|
{
|
2014-04-12 00:03:40 +02:00
|
|
|
::free((void *)fdo->fd_path);
|
2011-12-22 16:19:25 +01:00
|
|
|
Allocator_avl_base::free(reinterpret_cast<void*>(fdo->libc_fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
File_descriptor *File_descriptor_allocator::find_by_libc_fd(int libc_fd)
|
|
|
|
{
|
|
|
|
return metadata(reinterpret_cast<void*>(libc_fd));
|
|
|
|
}
|