2011-08-07 13:47:43 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
descriptors.cpp
|
|
|
|
Handles file descriptors, socket descriptors, and whatnot for each process.
|
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
#include <libmaxsi/memory.h>
|
|
|
|
#include "descriptors.h"
|
2011-11-16 02:37:29 -05:00
|
|
|
#include "device.h"
|
2011-08-07 13:47:43 -04:00
|
|
|
|
|
|
|
using namespace Maxsi;
|
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
|
|
|
// When in doubt use brute-force. This class could easily be optimized.
|
|
|
|
|
|
|
|
Device* const reserveddevideptr = (Device*) 0x1UL;
|
|
|
|
|
|
|
|
DescriptorTable::DescriptorTable()
|
|
|
|
{
|
|
|
|
numdevices = 0;
|
|
|
|
devices = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DescriptorTable::~DescriptorTable()
|
2011-11-22 13:12:04 -05:00
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorTable::Reset()
|
2011-08-07 13:47:43 -04:00
|
|
|
{
|
|
|
|
for ( int i = 0; i < numdevices; i++ )
|
|
|
|
{
|
|
|
|
if ( devices[i] == NULL || devices[i] == reserveddevideptr ) { continue; }
|
|
|
|
|
2011-11-22 13:12:04 -05:00
|
|
|
devices[i]->Unref();
|
2011-08-07 13:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] devices;
|
2011-11-22 13:12:04 -05:00
|
|
|
devices = NULL;
|
|
|
|
numdevices = 0;
|
2011-08-07 13:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int DescriptorTable::Allocate(Device* object)
|
|
|
|
{
|
|
|
|
for ( int i = 0; i < numdevices; i++ )
|
|
|
|
{
|
|
|
|
if ( devices[i] == NULL )
|
|
|
|
{
|
2011-11-17 04:22:43 -05:00
|
|
|
object->Refer();
|
2011-08-07 13:47:43 -04:00
|
|
|
devices[i] = object;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int newlistlength = numdevices*2;
|
|
|
|
if ( newlistlength == 0 ) { newlistlength = 8; }
|
|
|
|
|
|
|
|
Device** newlist = new Device*[newlistlength];
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( newlist == NULL ) { return -1; }
|
2011-08-07 13:47:43 -04:00
|
|
|
|
|
|
|
if ( devices != NULL )
|
|
|
|
{
|
|
|
|
Memory::Copy(newlist, devices, sizeof(Device*) * numdevices);
|
|
|
|
}
|
|
|
|
|
|
|
|
Memory::Set(newlist + numdevices, 0, sizeof(Device*) * (newlistlength-numdevices));
|
|
|
|
|
|
|
|
delete[] devices;
|
|
|
|
|
|
|
|
devices = newlist;
|
|
|
|
numdevices = newlistlength;
|
|
|
|
|
|
|
|
return Allocate(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorTable::Free(int index)
|
|
|
|
{
|
2011-11-17 04:22:43 -05:00
|
|
|
ASSERT(index < numdevices);
|
2011-08-07 13:47:43 -04:00
|
|
|
ASSERT(devices[index] != NULL);
|
|
|
|
|
|
|
|
if ( devices[index] != reserveddevideptr )
|
|
|
|
{
|
2011-11-16 02:37:29 -05:00
|
|
|
devices[index]->Unref();
|
2011-08-07 13:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
devices[index] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DescriptorTable::Reserve()
|
|
|
|
{
|
|
|
|
return Allocate(reserveddevideptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DescriptorTable::UseReservation(int index, Device* object)
|
|
|
|
{
|
|
|
|
ASSERT(index < index);
|
|
|
|
ASSERT(devices[index] != NULL);
|
2011-09-21 14:52:29 -04:00
|
|
|
ASSERT(devices[index] == reserveddevideptr);
|
2011-08-07 13:47:43 -04:00
|
|
|
|
2011-11-17 04:22:43 -05:00
|
|
|
object->Refer();
|
2011-08-07 13:47:43 -04:00
|
|
|
devices[index] = object;
|
|
|
|
}
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
bool DescriptorTable::Fork(DescriptorTable* forkinto)
|
|
|
|
{
|
|
|
|
Device** newlist = new Device*[numdevices];
|
|
|
|
if ( newlist == NULL ) { return false; }
|
|
|
|
|
2011-11-17 04:22:43 -05:00
|
|
|
for ( int i = 0; i < numdevices; i++ )
|
|
|
|
{
|
|
|
|
// TODO: Possibly deal with a potential O_CLOFORK!
|
|
|
|
newlist[i] = devices[i];
|
|
|
|
if ( !devices[i] ) { continue; }
|
|
|
|
if ( devices[i] == reserveddevideptr ) { continue; }
|
|
|
|
newlist[i]->Refer();
|
|
|
|
}
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
ASSERT(forkinto->devices == NULL);
|
|
|
|
|
|
|
|
forkinto->devices = newlist;
|
|
|
|
forkinto->numdevices = numdevices;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-07 13:47:43 -04:00
|
|
|
}
|