USB Stack (Host & Device) with single port

 We are making ECR (Electronic Cash Register)(billing Machine) with NXP uC (ARM7  & Cortex M4). The existing product is very much stable and in running production.
Now for cost effectiveness we are migrating (One of our base Model)  to Renesas with synergy S3A3, 100-pin (Flash: 512KB,     SRAM: 96KB(R7FS3A37A3A3A01CFP). 
As per existing features in our model we want to achieve the same with  following:-  
A)     Internal  Memory (SRAM)  for data storage only  upto   51KB    ; to achieve this(for data retention) a dedicated battery backup is provided  with deep standby mode implementation
B)     USB interface with host and device. At a time only one mode will be used. For this we have implemented  a switching IC  for changeover. 
       As in existing algorithm (for NXP) we have not used OTG feature, so here we will  also avoid this .
C)   We are avoiding multiple threads  using e2studio so as to avoid complexity  during migration from previous (NXP) code.
For  S3A3 we have done with basic functionality of uC initialization, timers, RTC, ADC, SCI  using SINGLE thread
 D) for USB interface , we need in HOST mode : Mass storage interface  and simple HID device connectivity .
                                                   In DEVICE mode:  as HID connectivity with PC.
       So from above mentioned three interfaces,  only one  will be operational at a time that will be handled by our code logic with hardware switching IC.
This has been already implemented effectively  in our existing product using NXP.
We have tested (in S3A3) USB interface as HOST and DEVICE individually  .  But in Host mode for MSD , only FILEX framework is consuming around 65KB. Rest for DEVICE interfaces are taking upto 19KB .
SO how we can implement all three interfaces with common memory pool size (if possible within 20KB) considering  at a time only one interface be operational??