Fixed bug #50333 Improving multi-threaded scalability by using emalloc/efree/estrdup

Things done

- The original patch was fixed and applied to the current master fork
- Additional fixes was developed (windows)
- Manually tested so far with CLI TS, CLI NTS and Apache TS