18a597
--- a/src/SFtp.cc	
18a597
+++ a/src/SFtp.cc	
18a597
@@ -299,12 +299,7 @@ void SFtp::MoveConnectionHere(SFtp *o)
18a597
    recv_translate=o->recv_translate.borrow();
18a597
    send_translate=o->send_translate.borrow();
18a597
    rate_limit=o->rate_limit.borrow();
18a597
-   expect_queue_size=o->expect_queue_size; o->expect_queue_size=0;
18a597
-   expect_chain=o->expect_chain; o->expect_chain=0;
18a597
-   expect_chain_end=o->expect_chain_end;
18a597
-   if(expect_chain_end==&o->expect_chain)
18a597
-      expect_chain_end=&expect_chain;
18a597
-   o->expect_chain_end=&o->expect_chain;
18a597
+   expect_queue.move_here(o->expect_queue);
18a597
    timeout_timer.Reset(o->timeout_timer);
18a597
    ssh_id=o->ssh_id;
18a597
    state=CONNECTED;
18a597
@@ -337,10 +332,6 @@ void SFtp::Init()
18a597
    eof=false;
18a597
    received_greeting=false;
18a597
    password_sent=0;
18a597
-   expect_queue_size=0;
18a597
-   expect_chain=0;
18a597
-   expect_chain_end=&expect_chain;
18a597
-   ooo_chain=0;
18a597
    protocol_version=0;
18a597
    send_translate=0;
18a597
    recv_translate=0;
18a597
@@ -693,8 +684,7 @@ void SFtp::Close()
18a597
    CloseHandle(Expect::IGNORE);
18a597
    super::Close();
18a597
    // don't need these out-of-order packets anymore
18a597
-   while(ooo_chain)
18a597
-      DeleteExpect(&ooo_chain);
18a597
+   ooo_chain.truncate();
18a597
    if(recv_buf)
18a597
       recv_buf->Resume();
18a597
 }
18a597
@@ -796,7 +786,7 @@ void SFtp::HandleExpect(Expect *e)
18a597
 	    SetError(NO_FILE,strerror(ENOTDIR));
18a597
 	    break;
18a597
 	 }
18a597
-	 if(mode==CHANGE_DIR && RespQueueIsEmpty())
18a597
+	 if(mode==CHANGE_DIR && GetExpectCount(Expect::CWD)==0)
18a597
 	 {
18a597
 	    cwd.Set(file);
18a597
 	    eof=true;
18a597
@@ -869,11 +859,15 @@ void SFtp::HandleExpect(Expect *e)
18a597
 	 }
18a597
 	 else
18a597
 	 {
18a597
-	    if(e->next!=ooo_chain)
18a597
-	       LogNote(9,"put a packet with id=%d on out-of-order chain (need_pos=%lld packet_pos=%lld)",
18a597
-		  reply->GetID(),(long long)(pos+file_buf->Size()),(long long)r->pos);
18a597
-	    e->next=ooo_chain;
18a597
-	    ooo_chain=e;
18a597
+	    LogNote(9,"put a packet with id=%d on out-of-order chain (need_pos=%lld packet_pos=%lld)",
18a597
+	       reply->GetID(),(long long)(pos+file_buf->Size()),(long long)r->pos);
18a597
+	    if(ooo_chain.count()>=64)
18a597
+	    {
18a597
+	       LogError(0,"Too many out-of-order packets");
18a597
+	       Disconnect();
18a597
+	       return;
18a597
+	    }
18a597
+	    ooo_chain.append(e);
18a597
 	    return;
18a597
 	 }
18a597
       }
18a597
@@ -925,7 +919,7 @@ void SFtp::HandleExpect(Expect *e)
18a597
 		  LogNote(9,"eof");
18a597
 	       eof=true;
18a597
 	       state=DONE;
18a597
-	       if(file_buf && !ooo_chain)
18a597
+	       if(file_buf && ooo_chain.count()==0)
18a597
 		  file_buf->PutEOF();
18a597
 	       break;
18a597
 	    }
18a597
@@ -1006,22 +1000,20 @@ int SFtp::HandleReplies()
18a597
    if(!recv_buf)
18a597
       return MOVED;
18a597
 
18a597
-   int i=0;
18a597
-   Expect *ooo_scan=ooo_chain;
18a597
-   while(ooo_scan)
18a597
-   {
18a597
-      Expect *next=ooo_scan->next;
18a597
-      ooo_chain=next;
18a597
-      HandleExpect(ooo_scan);
18a597
-      ooo_scan=next;
18a597
-      if(++i>64)
18a597
-      {
18a597
-	 LogError(0,"Too many out-of-order packets");
18a597
-	 Disconnect();
18a597
-	 return MOVED;
18a597
+   if(file_buf) {
18a597
+      off_t need_pos=pos+file_buf->Size();
18a597
+      // there are usually a few of out-of-order packets, no need for fast search
18a597
+      for(int i=0; i
18a597
+	 if(ooo_chain[i]->has_data_at_pos(need_pos)) {
18a597
+	    Expect *e=ooo_chain[i];
18a597
+	    ooo_chain[i]=0; // to keep the Expect
18a597
+	    ooo_chain.remove(i);
18a597
+	    HandleExpect(e);
18a597
+	 }
18a597
       }
18a597
    }
18a597
-   if(!ooo_chain && eof && file_buf && !file_buf->Eof())
18a597
+
18a597
+   if(ooo_chain.count()==0 && eof && file_buf && !file_buf->Eof())
18a597
       file_buf->PutEOF();
18a597
 
18a597
    if(recv_buf->Size()<4)
18a597
@@ -1066,51 +1058,20 @@ int SFtp::HandleReplies()
18a597
    HandleExpect(e);
18a597
    return MOVED;
18a597
 }
18a597
-SFtp::Expect **SFtp::FindExpect(Packet *p)
18a597
-{
18a597
-   unsigned id=p->GetID();
18a597
-   for(Expect **scan=&expect_chain; *scan; scan=&scan[0]->next)
18a597
-   {
18a597
-      if(scan[0]->request->GetID()==id)
18a597
-      {
18a597
-	 assert(!scan[0]->reply);
18a597
-	 scan[0]->reply=p;
18a597
-	 return scan;
18a597
-      }
18a597
-   }
18a597
-   return 0;
18a597
-}
18a597
 void SFtp::PushExpect(Expect *e)
18a597
 {
18a597
-   e->next=*expect_chain_end;
18a597
-   *expect_chain_end=e;
18a597
-   expect_chain_end=&e->next;
18a597
-   expect_queue_size++;
18a597
-}
18a597
-void SFtp::DeleteExpect(Expect **e)
18a597
-{
18a597
-   if(expect_chain_end==&e[0]->next)
18a597
-      expect_chain_end=e;
18a597
-   Expect *d=*e;
18a597
-   *e=e[0]->next;
18a597
-   delete d;
18a597
-   expect_queue_size--;
18a597
+   expect_queue.add(e->request->GetKey(),e);
18a597
 }
18a597
 SFtp::Expect *SFtp::FindExpectExclusive(Packet *p)
18a597
 {
18a597
-   Expect **e=FindExpect(p);
18a597
-   if(!e || !*e)
18a597
-      return 0;
18a597
-   Expect *res=*e;
18a597
-   if(expect_chain_end==&res->next)
18a597
-      expect_chain_end=e;
18a597
-   *e=res->next;
18a597
-   expect_queue_size--;
18a597
-   return res;
18a597
+   Expect *e=expect_queue.borrow(p->GetKey());
18a597
+   if(e)
18a597
+      e->reply=p;
18a597
+   return e;
18a597
 }
18a597
 void SFtp::CloseExpectQueue()
18a597
 {
18a597
-   for(Expect  *e=expect_chain; e; e=e->next)
18a597
+   for(Expect *e=expect_queue.each_begin(); e; e=expect_queue.each_next())
18a597
    {
18a597
       switch(e->tag)
18a597
       {
18a597
@@ -1133,6 +1094,14 @@ void SFtp::CloseExpectQueue()
18a597
    }
18a597
 }
18a597
 
18a597
+int SFtp::GetExpectCount(Expect::expect_t tag)
18a597
+{
18a597
+   int count=0;
18a597
+   for(Expect *e=expect_queue.each_begin(); e; e=expect_queue.each_next())
18a597
+      count+=(e->tag==tag);
18a597
+   return count;
18a597
+}
18a597
+
18a597
 Glob *SFtp::MakeGlob(const char *pat)
18a597
 {
18a597
    return new GenericGlob(this,pat);
18a597
--- a/src/SFtp.h	
18a597
+++ a/src/SFtp.h	
18a597
@@ -28,6 +28,7 @@ 
18a597
 #include <sys/types.h>
18a597
 #include <sys/stat.h>
18a597
 #include "FileSet.h"
18a597
+#include "xmap.h"
18a597
 
18a597
 class SFtp : public SSH_Access
18a597
 {
18a597
@@ -291,6 +292,7 @@ public:
18a597
       packet_type GetPacketType() { return type; }
18a597
       const char *GetPacketTypeText();
18a597
       unsigned GetID() const { return id; }
18a597
+      const xstring& GetKey() { return xstring::get_tmp((const char*)&id,sizeof(id)); }
18a597
       void SetID(unsigned new_id) { id=new_id; }
18a597
       void DropData(Buffer *b) { b->Skip(4+(length>0?length:0)); }
18a597
       bool TypeIs(packet_type t) const { return type==t; }
18a597
@@ -674,10 +676,15 @@ private:
18a597
 
18a597
       Ref<Packet> request;
18a597
       Ref<Packet> reply;
18a597
-      Expect *next;
18a597
       int i;
18a597
       expect_t tag;
18a597
       Expect(Packet *req,expect_t t,int j=0) : request(req), i(j), tag(t) {}
18a597
+
18a597
+      bool has_data_at_pos(off_t pos) const {
18a597
+	 if(!reply->TypeIs(SSH_FXP_DATA) || !request->TypeIs(SSH_FXP_READ))
18a597
+	    return false;
18a597
+	 return request.Cast<Request_READ>()->pos==pos;
18a597
+      }
18a597
    };
18a597
 
18a597
    void PushExpect(Expect *);
18a597
@@ -685,26 +692,22 @@ private:
18a597
    int HandlePty();
18a597
    void HandleExpect(Expect *);
18a597
    void CloseExpectQueue();
18a597
+   int GetExpectCount(Expect::expect_t tag);
18a597
    void CloseHandle(Expect::expect_t e);
18a597
    int ReplyLogPriority(int);
18a597
 
18a597
-   int expect_queue_size;
18a597
-   Expect *expect_chain;
18a597
-   Expect **expect_chain_end;
18a597
-   Expect **FindExpect(Packet *reply);
18a597
-   void DeleteExpect(Expect **);
18a597
-   Expect *FindExpectExclusive(Packet *reply);
18a597
-   Expect *ooo_chain; 	// out of order replies buffered
18a597
+   xmap_p<Expect> expect_queue;
18a597
+   const xstring& expect_key(unsigned id);
18a597
 
18a597
-   int   RespQueueIsEmpty() { return expect_chain==0; }
18a597
-   int	 RespQueueSize() { return expect_queue_size; }
18a597
-   void  EmptyRespQueue()
18a597
-      {
18a597
-	 while(expect_chain)
18a597
-	    DeleteExpect(&expect_chain);
18a597
-	 while(ooo_chain)
18a597
-	    DeleteExpect(&ooo_chain);
18a597
-      }
18a597
+   Expect *FindExpectExclusive(Packet *reply);
18a597
+   xarray_p<Expect> ooo_chain; 	// out of order replies buffered
18a597
+
18a597
+   int	 RespQueueSize() const { return expect_queue.count(); }
18a597
+   int   RespQueueIsEmpty() const { return RespQueueSize()==0; }
18a597
+   void  EmptyRespQueue() {
18a597
+      expect_queue.empty();
18a597
+      ooo_chain.truncate();
18a597
+   }
18a597
 
18a597
    bool GetBetterConnection(int level,bool limit_reached);
18a597
    void MoveConnectionHere(SFtp *o);
18a597
--- a/src/xmap.cc
18a597
+++ a/src/xmap.cc
18a597
@@ -133,8 +137,9 @@ void _xmap::_remove(entry **ep)
18a597
    if(!ep || !*ep)
18a597
       return;
18a597
    entry *e=*ep;
18a597
+   e->key.unset();
18a597
    *ep=e->next;
18a597
-   delete e;
18a597
+   xfree(e);
18a597
    entry_count--;
18a597
 }
18a597
 
18a597
--- a/src/xmap.h
18a597
+++ b/src/xmap.h
18a597
@@ -103,6 +103,7 @@ template<class T> T xmap<T>::zero;
18a597
 
18a597
 template<class T> class xmap_p : public _xmap
18a597
 {
18a597
+void dispose(T *p) { delete p; }
18a597
 public:
18a597
    xmap_p() : _xmap(sizeof(T*)) {}
18a597
    ~xmap_p() {
18a597
@@ -136,7 +137,7 @@ public:
18a597
    }
18a597
    void add(const xstring& key,T *e0) {
18a597
       entry *e=_add(key);
18a597
-      delete(payload(e));
18a597
+      dispose(payload(e));
18a597
       payload_Lv(e)=e0;
18a597
    }
18a597
    void add(const char *key,T *e0) { add(xstring::get_tmp(key),e0); }
18a597
@@ -148,7 +148,7 @@ public:
18a597
    void empty() {
18a597
       for(int i=0; i
18a597
 	 while(map[i]) {
18a597
-	    delete(payload(map[i]));
18a597
+	    dispose(payload(map[i]));
18a597
 	    _remove(&map[i]);
18a597
 	 }
18a597
       }