Refactor file descriptor allocation.
This commit is contained in:
parent
8c0e0235d6
commit
42f00f5d79
|
@ -101,12 +101,15 @@ bool DescriptorTable::Enlargen(int atleast)
|
||||||
return true;
|
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 )
|
if ( flags & ~__FD_ALLOWED_FLAGS )
|
||||||
return errno = EINVAL, -1;
|
return errno = EINVAL, -1;
|
||||||
ScopedLock lock(&dtablelock);
|
if ( min_index < 0 )
|
||||||
for ( int i = 0; i < numentries; i++ )
|
return errno = EINVAL, -1;
|
||||||
|
for ( int i = min_index; i < numentries; i++ )
|
||||||
if ( !entries[i].desc )
|
if ( !entries[i].desc )
|
||||||
{
|
{
|
||||||
entries[i].desc = desc;
|
entries[i].desc = desc;
|
||||||
|
@ -114,7 +117,7 @@ int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags)
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
int oldnumentries = numentries;
|
int oldnumentries = numentries;
|
||||||
if ( !Enlargen(0) )
|
if ( !Enlargen(min_index) )
|
||||||
return -1;
|
return -1;
|
||||||
int i = oldnumentries++;
|
int i = oldnumentries++;
|
||||||
entries[i].desc = desc;
|
entries[i].desc = desc;
|
||||||
|
@ -122,6 +125,20 @@ int DescriptorTable::Allocate(Ref<Descriptor> desc, int flags)
|
||||||
return i;
|
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)
|
int DescriptorTable::Copy(int from, int to, int flags)
|
||||||
{
|
{
|
||||||
if ( flags & ~__FD_ALLOWED_FLAGS )
|
if ( flags & ~__FD_ALLOWED_FLAGS )
|
||||||
|
|
|
@ -44,7 +44,8 @@ public:
|
||||||
~DescriptorTable();
|
~DescriptorTable();
|
||||||
Ref<DescriptorTable> Fork();
|
Ref<DescriptorTable> Fork();
|
||||||
Ref<Descriptor> Get(int index);
|
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);
|
int Copy(int from, int to, int flags);
|
||||||
void Free(int index);
|
void Free(int index);
|
||||||
Ref<Descriptor> FreeKeep(int index);
|
Ref<Descriptor> FreeKeep(int index);
|
||||||
|
@ -56,6 +57,7 @@ private:
|
||||||
void Reset(); // Hey, reference counted. Don't call this.
|
void Reset(); // Hey, reference counted. Don't call this.
|
||||||
bool IsGoodEntry(int i);
|
bool IsGoodEntry(int i);
|
||||||
bool Enlargen(int atleast);
|
bool Enlargen(int atleast);
|
||||||
|
int AllocateInternal(Ref<Descriptor> desc, int flags, int min_index);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
kthread_mutex_t dtablelock;
|
kthread_mutex_t dtablelock;
|
||||||
|
|
Loading…
Reference in New Issue