+#ifdef USE_PTHREADS
+/*
+ * Returns a non-null pointer unless a new screen should be allocated because
+ * no match was found in the pre-screen cache.
+ */
+NCURSES_EXPORT(SCREEN *)
+_nc_find_prescr(void)
+{
+ SCREEN *result = 0;
+ PRESCREEN_LIST *p;
+ pthread_t id = GetThreadID();
+ for (p = _nc_prescreen.allocated; p != 0; p = p->next) {
+ if (p->id == id) {
+ result = p->sp;
+ break;
+ }
+ }
+ return result;
+}
+
+/*
+ * Tells ncurses to forget that this thread was associated with the pre-screen
+ * cache. It does not modify the pre-screen cache itself, since that is used
+ * for creating new screens.
+ */
+NCURSES_EXPORT(void)
+_nc_forget_prescr(void)
+{
+ PRESCREEN_LIST *p, *q;
+ pthread_t id = GetThreadID();
+ for (p = _nc_prescreen.allocated, q = 0; p != 0; q = p, p = p->next) {
+ if (p->id == id) {
+ if (q) {
+ q->next = p->next;
+ } else {
+ _nc_prescreen.allocated = p->next;
+ }
+ free(p);
+ break;
+ }
+ }
+}
+#endif /* USE_PTHREADS */
+
+#if NCURSES_SP_FUNCS
+/*
+ * In case of handling multiple screens, we need to have a screen before
+ * initialization in _nc_setupscreen takes place. This is to extend the
+ * substitute for some of the stuff in _nc_prescreen, especially for slk and
+ * ripoff handling which should be done per screen.
+ */
+NCURSES_EXPORT(SCREEN *)
+new_prescr(void)
+{
+ SCREEN *sp;
+
+ START_TRACE();
+ T((T_CALLED("new_prescr()")));
+
+ _nc_lock_global(screen);
+ if ((sp = _nc_find_prescr()) == 0) {
+ sp = _nc_alloc_screen_sp();
+ T(("_nc_alloc_screen_sp %p", (void *) sp));
+ if (sp != 0) {
+#ifdef USE_PTHREADS
+ PRESCREEN_LIST *p = typeCalloc(PRESCREEN_LIST, 1);
+ if (p != 0) {
+ p->id = GetThreadID();
+ p->sp = sp;
+ p->next = _nc_prescreen.allocated;
+ _nc_prescreen.allocated = p;
+ }
+#else
+ _nc_prescreen.allocated = sp;
+#endif
+ sp->rsp = sp->rippedoff;
+ sp->_filtered = _nc_prescreen.filter_mode;
+ sp->_use_env = _nc_prescreen.use_env;
+#if NCURSES_NO_PADDING
+ sp->_no_padding = _nc_prescreen._no_padding;
+#endif
+ sp->slk_format = 0;
+ sp->_slk = 0;
+ sp->_prescreen = TRUE;
+ SP_PRE_INIT(sp);
+#if USE_REENTRANT
+ sp->_TABSIZE = _nc_prescreen._TABSIZE;
+ sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
+#endif
+ }
+ } else {
+ T(("_nc_alloc_screen_sp %p (reuse)", (void *) sp));
+ }
+ _nc_unlock_global(screen);
+ returnSP(sp);
+}
+#endif
+