From 07b409c1a06a5b3c351526f9afb0418d3da96e21 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 7 Aug 2011 19:47:43 +0200 Subject: [PATCH] Added a wrapper class for handling file descriptors. --- sortix/Makefile | 2 +- sortix/descriptors.cpp | 117 +++++++++++++++++++++++++++++++++++++++++ sortix/descriptors.h | 21 +++----- sortix/scheduler.h | 3 ++ 4 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 sortix/descriptors.cpp diff --git a/sortix/Makefile b/sortix/Makefile index 80a2fc59..f6cc9579 100644 --- a/sortix/Makefile +++ b/sortix/Makefile @@ -43,7 +43,7 @@ DEFINES:=$(DEFINES) -DINITRD CPPFLAGSRELEASE=-s -O3 CPPFLAGSDEBUG= CPPFLAGS=-I.. $(CPUDEFINES) $(CPUFLAGS) -std=gnu++0x -Wall -Wextra -nostdlib -fno-builtin -nostartfiles -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector $(DEFINES) $(CPPFLAGSRELEASE) -OBJS=$(CPUOBJS) kernel.o descriptor_tables.o isr.o time.o log.o iprintable.o panic.o keyboard.o memorymanagement.o scheduler.o syscall.o application.o pong.o sound.o pci.o uart.o conway.o test.o http.o vgaterminal.o serialterminal.o ../libmaxsi/libmaxsi-sortix.a +OBJS=$(CPUOBJS) kernel.o descriptor_tables.o isr.o time.o log.o iprintable.o panic.o keyboard.o memorymanagement.o scheduler.o syscall.o application.o pong.o sound.o pci.o uart.o conway.o test.o http.o vgaterminal.o serialterminal.o descriptors.o ../libmaxsi/libmaxsi-sortix.a JSOBJS:=$(subst .o,-js.o,$(OBJS)) all: sortix.bin diff --git a/sortix/descriptors.cpp b/sortix/descriptors.cpp new file mode 100644 index 00000000..cfc232b0 --- /dev/null +++ b/sortix/descriptors.cpp @@ -0,0 +1,117 @@ +/****************************************************************************** + + 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 . + + descriptors.cpp + Handles file descriptors, socket descriptors, and whatnot for each process. + +******************************************************************************/ + +#include "platform.h" +#include +#include "descriptors.h" + +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() + { + for ( int i = 0; i < numdevices; i++ ) + { + if ( devices[i] == NULL || devices[i] == reserveddevideptr ) { continue; } + + // TODO: unref any device here! + } + + delete[] devices; + } + + int DescriptorTable::Allocate(Device* object) + { + for ( int i = 0; i < numdevices; i++ ) + { + if ( devices[i] == NULL ) + { + devices[i] = object; + return i; + } + } + + int newlistlength = numdevices*2; + if ( newlistlength == 0 ) { newlistlength = 8; } + + Device** newlist = new Device*[newlistlength]; + if ( newlist == NULL ) + { + // TODO: Set errno! + return -1; + } + + 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) + { + ASSERT(index < index); + ASSERT(devices[index] != NULL); + + if ( devices[index] != reserveddevideptr ) + { + // TODO: Unref device here? + } + + devices[index] = NULL; + } + + int DescriptorTable::Reserve() + { + return Allocate(reserveddevideptr); + } + + void DescriptorTable::UseReservation(int index, Device* object) + { + ASSERT(index < index); + ASSERT(devices[index] != NULL); + ASSERT(devices[index] != reserveddevideptr); + + devices[index] = object; + } +} diff --git a/sortix/descriptors.h b/sortix/descriptors.h index ef370f17..71a11504 100644 --- a/sortix/descriptors.h +++ b/sortix/descriptors.h @@ -29,12 +29,6 @@ namespace Sortix { class Device; - struct Description - { - nat type; - Device* object; - }; - class DescriptorTable { public: @@ -42,21 +36,22 @@ namespace Sortix ~DescriptorTable(); private: - int _numDescs - Description* _descs; + int numdevices; + Device** devices; public: + int Allocate(Device* object); int Reserve(); + void Free(int index); void UseReservation(int index, Device* object); - int Allocate(Device* object; - bool Free(int index); public: - inline Description* get(int index) + inline Device* Get(int index) { - if ( _numDescs <= index ) { return NULL; } - return _descs + index; + if ( numdevices <= index ) { return NULL; } + return devices[index]; } + }; } diff --git a/sortix/scheduler.h b/sortix/scheduler.h index a958b3b0..a4bf18cb 100644 --- a/sortix/scheduler.h +++ b/sortix/scheduler.h @@ -25,6 +25,8 @@ #ifndef SORTIX_SCHEDULER_H #define SORTIX_SCHEDULER_H +#include "descriptors.h" + namespace Sortix { class Thread; @@ -38,6 +40,7 @@ namespace Sortix private: addr_t _addrspace; + DescriptorTable descriptors; public: addr_t GetAddressSpace() { return _addrspace; }