mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Refactor file descriptor allocation.
This commit is contained in:
parent
8c0e0235d6
commit
42f00f5d79
2 changed files with 24 additions and 5 deletions
|
@ -101,12 +101,15 @@ bool DescriptorTable::Enlargen(int atleast)
|
|||
return true;
|
||||
}
|
||||
|
||||
int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags)
|
||||
int DescriptorTable::AllocateInternal(Ref<Descriptor> desc, int flags,
|
||||
int min_index)
|
||||
{
|
||||
// dtablelock is held.
|
||||
if ( flags & ~__FD_ALLOWED_FLAGS )
|
||||
return errno = EINVAL, -1;
|
||||
ScopedLock lock(&dtablelock);
|
||||
for ( int i = 0; i < numentries; i++ )
|
||||
if ( min_index < 0 )
|
||||
return errno = EINVAL, -1;
|
||||
for ( int i = min_index; i < numentries; i++ )
|
||||
if ( !entries[i].desc )
|
||||
{
|
||||
entries[i].desc = desc;
|
||||
|
@ -114,7 +117,7 @@ int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags)
|
|||
return i;
|
||||
}
|
||||
int oldnumentries = numentries;
|
||||
if ( !Enlargen(0) )
|
||||
if ( !Enlargen(min_index) )
|
||||
return -1;
|
||||
int i = oldnumentries++;
|
||||
entries[i].desc = desc;
|
||||
|
@ -122,6 +125,20 @@ int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags)
|
|||
return i;
|
||||
}
|
||||
|
||||
int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags, int min_index)
|
||||
{
|
||||
ScopedLock lock(&dtablelock);
|
||||
return AllocateInternal(desc, flags, min_index);
|
||||
}
|
||||
|
||||
int DescriptorTable::Allocate(int src_index, int flags, int min_index)
|
||||
{
|
||||
ScopedLock lock(&dtablelock);
|
||||
if ( !IsGoodEntry(src_index) )
|
||||
return errno = EBADF, -1;
|
||||
return AllocateInternal(entries[src_index].desc, flags, min_index);
|
||||
}
|
||||
|
||||
int DescriptorTable::Copy(int from, int to, int flags)
|
||||
{
|
||||
if ( flags & ~__FD_ALLOWED_FLAGS )
|
||||
|
|
|
@ -44,7 +44,8 @@ public:
|
|||
~DescriptorTable();
|
||||
Ref<DescriptorTable> Fork();
|
||||
Ref<Descriptor> Get(int index);
|
||||
int Allocate(Ref<Descriptor> desc, int flags);
|
||||
int Allocate(Ref<Descriptor> desc, int flags, int min_index = 0);
|
||||
int Allocate(int src_index, int flags, int min_index = 0);
|
||||
int Copy(int from, int to, int flags);
|
||||
void Free(int index);
|
||||
Ref<Descriptor> FreeKeep(int index);
|
||||
|
@ -56,6 +57,7 @@ private:
|
|||
void Reset(); // Hey, reference counted. Don't call this.
|
||||
bool IsGoodEntry(int i);
|
||||
bool Enlargen(int atleast);
|
||||
int AllocateInternal(Ref<Descriptor> desc, int flags, int min_index);
|
||||
|
||||
private:
|
||||
kthread_mutex_t dtablelock;
|
||||
|
|
Loading…
Reference in a new issue