diff --git a/include/fs.hh b/include/fs.hh index 57f4a0aa2002ef1e841c2b33082f2fcd8037fb2a..8f915aebd96a775deb175be924784d11723f8f2e 100644 --- a/include/fs.hh +++ b/include/fs.hh @@ -3,17 +3,17 @@ #include "common.h" #include "ipc.hh" +#include "klib.hh" namespace fs{ + using klib::SmartPtr; struct INode{ enum INodeType{ dir, file, dev }; - int valid; - xlen_t ref; - INodeType type; - xlen_t size; - //xlen_t addrs[]; + bool m_valid; + INodeType m_type; + size_t m_size; }; struct File{ enum FileType{ @@ -21,15 +21,14 @@ namespace fs{ stdin,stdout,stderr }; FileType type; - INode *in; - xlen_t ref; + SmartPtr<INode> in; union Data { pipe::Pipe* pipe; }obj; - File(FileType ftype=none, INode *inptr=nullptr): type(ftype), in(inptr), ref(0) {}; + File(FileType a_type=none, SmartPtr<INode> a_in=nullptr): type(a_type), in(a_in) {}; + ~File(); // 使用智能指针åŽï¼Œå…³é—é€»è¾‘åœ¨æžæž„ä¸å¤„ç† void write(xlen_t addr,size_t len); - void fileClose(); }; } #endif \ No newline at end of file diff --git a/include/klib.hh b/include/klib.hh index 2061e615b646686880b87d39fbcdf79237df4dfb..08315d1e349ecd04591c9356a0863a6f5461b792 100644 --- a/include/klib.hh +++ b/include/klib.hh @@ -24,104 +24,104 @@ namespace klib }; -template<typename T,size_t buffsize=128> -struct ringbuf -{ - T buff[128]; - size_t head,tail; - FORCEDINLINE size_t next(size_t cur){return (cur+1)%buffsize;} - inline void put(T d){ - buff[tail]=d; - tail=next(tail); - } - inline void pop(){ - head=next(head); - } - inline T get(){ - return buff[head]; - } - inline bool empty(){ - return head==tail; - } - inline bool full(){ - return next(tail)==head; - } -}; + template<typename T,size_t buffsize=128> + struct ringbuf + { + T buff[128]; + size_t head,tail; + FORCEDINLINE size_t next(size_t cur){return (cur+1)%buffsize;} + inline void put(T d){ + buff[tail]=d; + tail=next(tail); + } + inline void pop(){ + head=next(head); + } + inline T get(){ + return buff[head]; + } + inline bool empty(){ + return head==tail; + } + inline bool full(){ + return next(tail)==head; + } + }; -template<typename T> -struct ListNode{ - T data; - union { - ListNode *prev; - ListNode *next; - }iter; - ListNode(const T& data):data(data){this->iter.next=nullptr; } -}; + template<typename T> + struct ListNode{ + T data; + union { + ListNode *prev; + ListNode *next; + }iter; + ListNode(const T& data):data(data){this->iter.next=nullptr; } + }; -template<typename T> -class Seq{}; -// @todo listæ’入或è¿ä»£å™¨é历时具有bug -template<typename T,bool LOOPBACK=false> -struct list:public Seq<T>{ - typedef ListNode<T>* listndptr; - typedef const ListNode<T>* listndptr_const; - listndptr head; - listndptr tail; - inline list(){ head=tail=nullptr; } - inline list(const std::initializer_list<T> &il):list(){ - for(const auto &i:il)push_back(i); - } - inline list(const list<T> &other):list(){ - for(const auto i:other)push_back(i); - } - static inline void insertAfter(listndptr cur,listndptr nd){ - nd->iter.next=cur->iter.next; - cur->iter.next=nd; - } - static inline void insertBefore(listndptr cur,listndptr nd){ - nd->iter.next=cur; - } + template<typename T> + class Seq{}; + // @todo listæ’入或è¿ä»£å™¨é历时具有bug + template<typename T,bool LOOPBACK=false> + struct list:public Seq<T>{ + typedef ListNode<T>* listndptr; + typedef const ListNode<T>* listndptr_const; + listndptr head; + listndptr tail; + inline list(){ head=tail=nullptr; } + inline list(const std::initializer_list<T> &il):list(){ + for(const auto &i:il)push_back(i); + } + inline list(const list<T> &other):list(){ + for(const auto i:other)push_back(i); + } + static inline void insertAfter(listndptr cur,listndptr nd){ + nd->iter.next=cur->iter.next; + cur->iter.next=nd; + } + static inline void insertBefore(listndptr cur,listndptr nd){ + nd->iter.next=cur; + } - inline void push_back(listndptr newNode){ - if(tail) - list::insertAfter(tail,newNode),tail=newNode; - else - head=tail=newNode; - } - inline void push_back(const T &data){ - listndptr newNode=new ListNode<T>(data); - push_back(newNode); - } - inline void push_front(listndptr newNode){ - if(head) - list::insertBefore(head,newNode),head=newNode; - else - head=tail=newNode; - } - inline void push_front(const T &data){ - listndptr newNode=new ListNode<T>(data); - push_front(newNode); - } - inline T pop_front(){ - T rt=head->data; - if(head==tail)tail=nullptr; - head=head->iter.next; - delete head; - return rt; - } - template<bool isConst> - class iteratorbase{ - using ndptr=std::conditional_t<isConst,listndptr_const,listndptr>; - using refT=std::conditional_t<isConst,const T,T>; - ndptr ptr; - const list<T,LOOPBACK> *parent; - public: + inline void push_back(listndptr newNode){ + if(tail) + list::insertAfter(tail,newNode),tail=newNode; + else + head=tail=newNode; + } + inline void push_back(const T &data){ + listndptr newNode=new ListNode<T>(data); + push_back(newNode); + } + inline void push_front(listndptr newNode){ + if(head) + list::insertBefore(head,newNode),head=newNode; + else + head=tail=newNode; + } + inline void push_front(const T &data){ + listndptr newNode=new ListNode<T>(data); + push_front(newNode); + } + inline T pop_front(){ + T rt=head->data; + if(head==tail)tail=nullptr; + head=head->iter.next; + delete head; + return rt; + } + template<bool isConst> + class iteratorbase{ + using ndptr=std::conditional_t<isConst,listndptr_const,listndptr>; + using refT=std::conditional_t<isConst,const T,T>; + ndptr ptr; + const list<T,LOOPBACK> *parent; + public: iteratorbase(ndptr p,const list<T,LOOPBACK> *parent):ptr(p),parent(parent){} const refT& operator*() const { - return ptr->data; + return ptr->data; } refT* operator->() const { - return &(ptr->data); + return &(ptr->data); } iteratorbase& operator++() { if(LOOPBACK&&ptr==parent->tail) @@ -131,66 +131,64 @@ struct list:public Seq<T>{ return *this; } iteratorbase operator++(int) { - iteratorbase temp(*this); - if(LOOPBACK&&ptr==parent->tail) - ptr=parent->head; - else - ptr=ptr->iter.next; - return temp; + iteratorbase temp(*this); + if(LOOPBACK&&ptr==parent->tail) + ptr=parent->head; + else + ptr=ptr->iter.next; + return temp; } - // iteratorbase operator+(size_t n) const { - // listndptr temp=ptr; - // while(n-- && temp!=nullptr)temp=temp->iter.next; - // return iteratorbase(temp); + // listndptr temp=ptr; + // while(n-- && temp!=nullptr)temp=temp->iter.next; + // return iteratorbase(temp); // } // iteratorbase& operator+=(size_t n) { - // while(n-- && ptr!=nullptr)ptr=ptr->iter.next; - // return *this; + // while(n-- && ptr!=nullptr)ptr=ptr->iter.next; + // return *this; // } bool operator==(const iteratorbase& other) const { - return ptr == other.ptr; + return ptr == other.ptr; } bool operator!=(const iteratorbase& other) const { - return ptr != other.ptr; + return ptr != other.ptr; } - }; - typedef iteratorbase<false> iterator; - typedef iteratorbase<true> const_iterator; - iterator begin() { - return iterator(head,this); - } - iterator end() { - return iterator(tail->iter.next,this); - //return iterator(tail,this); - } - const_iterator begin() const{ - return const_iterator(head,this); - } - const_iterator end() const{ - return const_iterator(tail->iter.next,this); - } - inline bool empty(){ - return head==nullptr; - } - inline void remove(const T &data){ - // iterator prev; - // for(auto i:*this){ - // if(*i==data){ - // prev.ptr->iter.next=i.ptr->iter.next; - // delete i->ptr; - // } - // prev=i; - // } - } - inline void print(void (*printhook)(const T&)){ - printf("{head=0x%lx, tail=0x%lx} [\t",head,tail); - for(listndptr cur=head;cur;cur=cur->iter.next) - printhook(cur->data); - // printf("\t]\n"); - } -}; + typedef iteratorbase<false> iterator; + typedef iteratorbase<true> const_iterator; + iterator begin() { + return iterator(head,this); + } + iterator end() { + return iterator(tail->iter.next,this); + //return iterator(tail,this); + } + const_iterator begin() const{ + return const_iterator(head,this); + } + const_iterator end() const{ + return const_iterator(tail->iter.next,this); + } + inline bool empty(){ + return head==nullptr; + } + inline void remove(const T &data){ + // iterator prev; + // for(auto i:*this){ + // if(*i==data){ + // prev.ptr->iter.next=i.ptr->iter.next; + // delete i->ptr; + // } + // prev=i; + // } + } + inline void print(void (*printhook)(const T&)){ + printf("{head=0x%lx, tail=0x%lx} [\t",head,tail); + for(listndptr cur=head;cur;cur=cur->iter.next) + printhook(cur->data); + // printf("\t]\n"); + } + }; template<typename T> struct ArrayBuff{ @@ -198,65 +196,145 @@ struct list:public Seq<T>{ T *buff; ArrayBuff(size_t len):len(len){buff=new T[len];} ArrayBuff(T* addr,size_t len):ArrayBuff(len){ - memcpy(buff,addr,len*sizeof(T)); + memcpy(buff,addr,len*sizeof(T)); } class iterator{ T* ptr; public: iterator(T* p) : ptr(p) {} T& operator*() const { - return *ptr; - } - T* operator->() const { - return ptr; - } - iterator& operator++() { - ++ptr; - return *this; - } - iterator operator++(int) { - iterator temp(*this); - ++ptr; - return temp; - } - iterator& operator--() { - --ptr; - return *this; - } - iterator operator--(int) { - iterator temp(*this); - --ptr; - return temp; - } - iterator operator+(size_t n) const { - return iterator(ptr + n); - } - iterator operator-(size_t n) const { - return iterator(ptr - n); - } - iterator& operator+=(size_t n) { - ptr += n; - return *this; - } - iterator& operator-=(size_t n) { - ptr -= n; - return *this; - } - T& operator[](size_t n) const { - return *(ptr + n); - } - bool operator==(const iterator& other) const { - return ptr == other.ptr; - } - bool operator!=(const iterator& other) const { - return ptr != other.ptr; - } + return *ptr; + } + T* operator->() const { + return ptr; + } + iterator& operator++() { + ++ptr; + return *this; + } + iterator operator++(int) { + iterator temp(*this); + ++ptr; + return temp; + } + iterator& operator--() { + --ptr; + return *this; + } + iterator operator--(int) { + iterator temp(*this); + --ptr; + return temp; + } + iterator operator+(size_t n) const { + return iterator(ptr + n); + } + iterator operator-(size_t n) const { + return iterator(ptr - n); + } + iterator& operator+=(size_t n) { + ptr += n; + return *this; + } + iterator& operator-=(size_t n) { + ptr -= n; + return *this; + } + T& operator[](size_t n) const { + return *(ptr + n); + } + bool operator==(const iterator& other) const { + return ptr == other.ptr; + } + bool operator!=(const iterator& other) const { + return ptr != other.ptr; + } }; iterator begin(){return iterator(buff);} iterator end(){return iterator(buff+len);} const char* c_str(){return reinterpret_cast<char*>(buff);} }; typedef ArrayBuff<uint8_t> ByteArray; + + // meta data block + struct MDB{ + size_t m_ref; + MDB(): m_ref(0) {}; + }; + template<typename T> + /* + SmartPointer, by Ct_Unvs + SmartPtråªèƒ½ç”¨äºŽåЍæ€å¯¹è±¡ï¼Œä¸”SmartPtr本身ä¸åº”使用new创建 + */ + class SmartPtr { + private: + T *m_ptr; + MDB *m_meta; + public: + // æž„é€ ä¸Žæžæž„ + SmartPtr(): m_ptr(nullptr), m_meta(nullptr) {} + SmartPtr(T *a_ptr): m_ptr(a_ptr), m_meta((a_ptr!=nullptr)?(new MDB):nullptr) {} + SmartPtr(const SmartPtr<T> &a_sptr): m_ptr(a_sptr.m_ptr), m_meta(a_sptr.m_meta) { ++(m_meta->m_ref); } + ~SmartPtr() { deRef(); } + // 赋值è¿ç®— + const SmartPtr<T> operator=(T *a_ptr) { + deRef(); + m_ptr = a_ptr; + if(a_ptr != nullptr) { m_meta = new MDB; } + else { m_meta = nullptr; } + return *this; + } + const SmartPtr<T> operator=(const SmartPtr<T> &a_sptr) { + deRef(); + m_ptr = a_sptr.m_ptr; + m_meta = a_sptr.m_meta; + if(m_meta != nullptr) { ++(m_meta->m_ref); } + return *this; + } + // 引用è¿ç®— + T& operator*() const { return *m_ptr; } + T* operator->() const { return m_ptr; } + T& operator[](int a_offset) const { return m_ptr[a_offset]; } + // 算术è¿ç®— + T *const operator+(int a_offset) const { + if(m_ptr != nullptr) { return m_ptr + a_offset; } + else { return nullptr; } + } + T *const operator-(int a_offset) const { + if(m_ptr != nullptr) { return m_ptr - a_offset; } + else { return nullptr; } + } + const int operator-(T *a_ptr) const { return m_ptr - a_ptr; } + const int operator-(const SmartPtr<T> &a_sptr) const { return m_ptr - a_sptr.m_ptr; } + // 逻辑è¿ç®— + const bool operator>(T *a_ptr) const { return m_ptr > a_ptr; } + const bool operator<(T *a_ptr) const { return m_ptr < a_ptr; } + const bool operator>=(T *a_ptr) const { return m_ptr >= a_ptr; } + const bool operator<=(T *a_ptr) const { return m_ptr <= a_ptr; } + const bool operator==(T *a_ptr) const { return m_ptr == a_ptr; } + const bool operator>(const SmartPtr<T> &a_sptr) const { return m_ptr > a_sptr.m_ptr; } + const bool operator<(const SmartPtr<T> &a_sptr) const { return m_ptr < a_sptr.m_ptr; } + const bool operator>=(const SmartPtr<T> &a_sptr) const { return m_ptr >= a_sptr.m_ptr; } + const bool operator<=(const SmartPtr<T> &a_sptr) const { return m_ptr <= a_sptr.m_ptr; } + const bool operator==(const SmartPtr<T> &a_sptr) const { return m_ptr == a_sptr.m_ptr; } + // 功能函数 + void deRef() { + if(m_meta != nullptr) { + if(--(m_meta->m_ref) <= 0){ + delete m_meta; + delete m_ptr; + } + } + m_ptr = nullptr; + m_meta = nullptr; + return; + } + T *const rawPtr() const { return m_ptr; } + const MDB& metaData() const { return *m_meta; } + void print() const { + printf("SmartPointer: [addr=0x%lx, MDB:(addr=0x%lx, ref=%d)]\n", m_ptr, m_meta, (m_meta!=nullptr)?(m_meta->m_ref):0); + } + }; } // namespace klib static klib::ringbuf<char> buf; diff --git a/include/proc.hh b/include/proc.hh index f02b97e250519afb8c5040cae4203ca95ccd7f31..f8980984710ba2f1adbe8c3a0e9b69bbc095ded8 100644 --- a/include/proc.hh +++ b/include/proc.hh @@ -5,6 +5,7 @@ #include "vm.hh" #include "fs.hh" #include "resmgr.hh" +#include "klib.hh" namespace proc { @@ -12,6 +13,7 @@ namespace proc using sched::Scheduable; using sched::prior_t; using vm::VMAR; + using klib::SmartPtr; struct Context { @@ -36,7 +38,7 @@ namespace proc tid_t parent; VMAR vmar; klib::list<Task*> tasks; - File* files[MaxOpenFile]; + SmartPtr<File> files[MaxOpenFile]; Process(prior_t prior,tid_t parent); Process(tid_t pid,prior_t prior,tid_t parent); @@ -46,11 +48,11 @@ namespace proc inline tid_t pid(){return id;} inline prior_t priority(){return prior;} inline xlen_t satp(){return vmar.satp();} - inline File *ofile(int fd){return files[fd];} + inline SmartPtr<File> ofile(int fd){return files[fd];} Task* newTask(); Task* newTask(const Task &other,bool allocStack=true); void print(); - int fdAlloc(File *file, int newfd=-1); + int fdAlloc(SmartPtr<File> a_file, int a_fd=-1); private: xlen_t newUstack(); xlen_t newKstack(); diff --git a/kernel/fs.cc b/kernel/fs.cc index 618058571fa21aa1c7ffeecd565c7b1bd73ffef2..e3f11f7d9487b5162d5e4a11d9600b186bca23ff 100644 --- a/kernel/fs.cc +++ b/kernel/fs.cc @@ -19,17 +19,8 @@ void fs::File::write(xlen_t addr,size_t len){ } } -void fs::File::fileClose(){ - --ref; - if(ref <= 0){ - /* - switch(type){ - 对管é“ã€è®¾å¤‡ç‰çš„å¤„ç† - } - */ - ref = 0; - type = none; - // 需ä¸éœ€è¦deleteç‰æ“作? - } - return; +fs::File::~File() { + /* + å…³é—逻辑 + */ } \ No newline at end of file diff --git a/kernel/proc.cc b/kernel/proc.cc index 3e4e9d8643c8cfa74f72d58123cb392bb9598c7b..84322bf0cdee599dbe2c1492e1539afd1cc569b3 100644 --- a/kernel/proc.cc +++ b/kernel/proc.cc @@ -104,21 +104,19 @@ Process::Process(const Process &other,tid_t pid):IdManagable(pid),Scheduable(oth } -int Process::fdAlloc(File *file, int fd){ // fd缺çœå€¼ä¸º-1,在头文件ä¸å®šä¹‰ - if(fd < 0){ - for(fd = 0; fd < MaxOpenFile; ++fd){ +int Process::fdAlloc(SmartPtr<File> a_file, int a_fd){ // fd缺çœå€¼ä¸º-1,在头文件ä¸å®šä¹‰ + if(a_fd < 0) { + for(int fd = 0; fd < MaxOpenFile; ++fd){ if(files[fd] == nullptr){ - files[fd] = file; - ++file->ref; + files[fd] = a_file; return fd; } } } - else{ - if((fd<MaxOpenFile) && (files[fd]==nullptr)){ - files[fd] = file; - ++file->ref; - return fd; + else { + if((a_fd<MaxOpenFile) && (files[a_fd]==nullptr)){ + files[a_fd] = a_file; + return a_fd; } } return -1; // 返回错误ç diff --git a/kernel/syscall.cc b/kernel/syscall.cc index de7d41d8a6c07e7d1d35da7b73d048a953b3c3e4..01b79ab1f216e2bd2e666f3123a98280fb84e995 100644 --- a/kernel/syscall.cc +++ b/kernel/syscall.cc @@ -6,6 +6,7 @@ extern void _strapexit(); namespace syscall { using sys::statcode; + using klib::SmartPtr; int none(){return 0;} int testexit(){ static bool b=false; @@ -49,16 +50,16 @@ namespace syscall proc::clone(kHartObjs.curtask); return statcode::ok; } - int openAt(){ + int openAt() { auto &ctx = kHartObjs.curtask->ctx; - int dirfd = ctx.x(10); - const char *path = (const char*)ctx.x(11); - int flags = ctx.x(12); - mode_t mode = ctx.x(13); // uint32 + int a_dirfd = ctx.x(10); + const char *a_path = (const char*)ctx.x(11); + int a_flags = ctx.x(12); + mode_t a_mode = ctx.x(13); // uint32 int fd; - fs::File *f; - fs::INode *in; + SmartPtr<fs::File> f; + SmartPtr<fs::INode> in; auto curproc = kHartObjs.curtask->getProcess(); /* @@ -66,81 +67,63 @@ namespace syscall */ // 测试用 in = new fs::INode; - in->type = fs::INode::file; + in->m_type = fs::INode::file; // f = new fs::File; // åŽé¢åº”改æˆä»Žå…¨å±€æ•°æ®ç»“æž„ä¸åˆ†é… fd = curproc->fdAlloc(f); - if(fd < 0){ - return statcode::err; - } + if(fd < 0) { return statcode::err; } - if(in->type == fs::INode::dev){ - f->type = fs::File::dev; - } - else{ - f->type = fs::File::inode; - } + if(in->m_type == fs::INode::dev) { f->type = fs::File::dev; } + else { f->type = fs::File::inode; } f->in = in; return fd; } - int close(){ + int close() { auto &ctx = kHartObjs.curtask->ctx; - int fd = ctx.x(10); + int a_fd = ctx.x(10); - fs::File *f; + SmartPtr<fs::File> f; auto curproc = kHartObjs.curtask->getProcess(); // 判æ–fd范围也许å¯ä»¥å†™æˆå®â€¦â€¦ - if((fd<0) || (fd>proc::MaxOpenFile)){ - return statcode::err; - } + if((a_fd<0) || (a_fd>proc::MaxOpenFile)) { return statcode::err; } - f = curproc->files[fd]; - if(f == nullptr){ - return statcode::err; - } + f = curproc->files[a_fd]; + if(f == nullptr) { return statcode::err; } - f->fileClose(); - curproc->files[fd] = nullptr; + curproc->files[a_fd].deRef(); return statcode::ok; } - int dupArgsIn(int fd, int newfd=-1){ - fs::File *f; + int dupArgsIn(int a_fd, int a_newfd=-1) { + int newfd; + SmartPtr<fs::File> f; auto curproc = kHartObjs.curtask->getProcess(); // 判æ–fd范围也许å¯ä»¥å†™æˆå®â€¦â€¦ - if((fd<0) || (fd>proc::MaxOpenFile)){ - return statcode::err; - } + if((a_fd<0) || (a_fd>proc::MaxOpenFile)) { return statcode::err; } - f = curproc->files[fd]; - if(f == nullptr){ - return statcode::err; - } + f = curproc->files[a_fd]; + if(f == nullptr) { return statcode::err; } // dupArgsIn内部newfd<0时视作由æ“ä½œç³»ç»Ÿåˆ†é…æè¿°ç¬¦ï¼ˆåŒfdAllocï¼‰ï¼Œå› æ¤å¯¹newfdéžè´Ÿçš„判æ–应在外层dup3ä¸å®Œæˆ - newfd = curproc->fdAlloc(f, newfd); - if(newfd < 0){ - return statcode::err; - } + newfd = curproc->fdAlloc(f, a_newfd); + if(newfd < 0) { return statcode::err; } return newfd; } - int dup(){ + int dup() { auto &ctx = kHartObjs.curtask->ctx; - int fd = ctx.x(10); + int a_fd = ctx.x(10); - return dupArgsIn(fd); + return dupArgsIn(a_fd); } - int dup3(){ + int dup3() { auto &ctx = kHartObjs.curtask->ctx; - int fd = ctx.x(10); - int newfd = ctx.x(11); + int a_fd = ctx.x(10); + int a_newfd = ctx.x(11); // 判æ–fd范围也许å¯ä»¥å†™æˆå®â€¦â€¦ - if((newfd<0) || (newfd>proc::MaxOpenFile)){ - return statcode::err; - } + if((a_newfd<0) || (a_newfd>proc::MaxOpenFile)){ return statcode::err; } - return dupArgsIn(fd, newfd); + return dupArgsIn(a_fd, a_newfd); } void init(){ using sys::syscalls;