Re: Re: [PATCH] Automatic module globals management
| From: | Steph Fox | Date: | Sun, 11 Jun 2006 08:49:24 +0000 |
| Subject: | Re: Re: [PATCH] Automatic module globals management | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-24089@lists.php.net to get a copy of this message | ||
Hi Stas
SF>>resource id, and it would need to be a ZTS build. Whatever way you look at SF>>it, that's a screwed up configuration. So we would blame the user for our broken code. Do you think it's a good idea.No I don't, so I wrote something completely different. Is the attached a better solution? Again, it's intended to work _alongside_ Dmitry's, not replace it. It checks for globals_id_ptr, you need to change that to globals_id if you're trying it against current 5_2. - Steph
-- Stanislav Malyshev, Zend Products Engineer stas@zend.com http://www.zend.com/ +972-3-6139665 ext.115
Index: TSRM/TSRM.c =================================================================== RCS file: /repository/TSRM/TSRM.c,v retrieving revision 1.68.2.3 diff -u -r1.68.2.3 TSRM.c --- TSRM/TSRM.c 14 Mar 2006 15:16:07 -0000 1.68.2.3 +++ TSRM/TSRM.c 11 Jun 2006 07:38:38 -0000 @@ -31,6 +31,7 @@ typedef struct { + char *name; size_t size; ts_allocate_ctor ctor; ts_allocate_dtor dtor; @@ -212,9 +213,12 @@ /* allocates a new thread-safe-resource id */ -TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor) +TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor, ...) { int i; + va_list args; + char *name = NULL; + unsigned char a; TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtaining a new resource id, %d bytes", size)); @@ -222,6 +226,20 @@ /* obtain a resource id */ *rsrc_id = TSRM_SHUFFLE_RSRC_ID(id_count++); + + /* pick up internal module name, if one exists */ + va_start(args, dtor); + name = (char *)va_arg(args, int); + if (!name || (name && (int) name > CHAR_MIN && (int) name < CHAR_MAX)) { + name = NULL; /* missing name or uchar */ + } else { + a = name[0]; + if (a < 97 || a > 122) { + name = NULL; /* not lowercase ASCII */ + } + } + va_end(args); + TSRM_ERROR((TSRM_ERROR_LEVEL_CORE, "Obtained resource id %d", *rsrc_id)); /* store the new resource type in the resource sizes table */ @@ -235,6 +253,8 @@ } resource_types_table_size = id_count; } + + resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].name = name; resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].size = size; resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].ctor = ctor; resource_types_table[TSRM_UNSHUFFLE_RSRC_ID(*rsrc_id)].dtor = dtor; @@ -542,7 +562,7 @@ while (p) { if (p->count > j && p->storage[j]) { - if (resource_types_table && resource_types_table[j].dtor) { + if (resource_types_table && resource_types_table[j].dtor && !resource_types_table[j].done) { resource_types_table[j].dtor(p->storage[j], &p->storage); } free(p->storage[j]); @@ -587,6 +607,37 @@ } +/* Obtain thread id from module name */ +TSRM_API ts_rsrc_id tsrm_get_id(char *name) +{ + int i, j; + + tsrm_mutex_lock(tsmm_mutex); + + if (tsrm_tls_table) { + for (i = 0; i < tsrm_tls_table_size; i++) { + tsrm_tls_entry *p = tsrm_tls_table[i]; + + while (p) { + + for (j = 0; j < id_count; j++) { + if (p->count > j && p->storage[j]) { + if (resource_types_table[j].name && strcasecmp(name, resource_types_table[j].name) == 0) { + tsrm_mutex_unlock(tsmm_mutex); + return TSRM_SHUFFLE_RSRC_ID(j); + } + } + } + p = p->next; + } + } + } + + tsrm_mutex_unlock(tsmm_mutex); + return FALSE; +} + + /* Allocate a mutex */ TSRM_API MUTEX_T tsrm_mutex_alloc(void) { Index: TSRM/TSRM.h =================================================================== RCS file: /repository/TSRM/TSRM.h,v retrieving revision 1.50.2.2 diff -u -r1.50.2.2 TSRM.h --- TSRM/TSRM.h 14 Mar 2006 15:16:07 -0000 1.50.2.2 +++ TSRM/TSRM.h 11 Jun 2006 05:43:50 -0000 @@ -20,7 +20,7 @@ #ifdef WIN32 # define TSRM_WIN32 -# include "tsrm_config.w32.h" +# include "tsrm_config_common.h" #endif #ifdef TSRM_WIN32 @@ -97,7 +97,7 @@ TSRM_API void tsrm_shutdown(void); /* allocates a new thread-safe-resource id */ -TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor); +TSRM_API ts_rsrc_id ts_allocate_id(ts_rsrc_id *rsrc_id, size_t size, ts_allocate_ctor ctor, ts_allocate_dtor dtor, ...); /* fetches the requested resource for the current thread */ TSRM_API void *ts_resource_ex(ts_rsrc_id id, THREAD_T *th_id); @@ -127,6 +127,7 @@ /* utility functions */ TSRM_API THREAD_T tsrm_thread_id(void); +TSRM_API ts_rsrc_id tsrm_get_id(char* name); TSRM_API MUTEX_T tsrm_mutex_alloc(void); TSRM_API void tsrm_mutex_free(MUTEX_T mutexp); TSRM_API int tsrm_mutex_lock(MUTEX_T mutexp); Index: TSRM/tsrm_config.w32.h =================================================================== RCS file: /repository/TSRM/tsrm_config.w32.h,v retrieving revision 1.5 diff -u -r1.5 tsrm_config.w32.h --- TSRM/tsrm_config.w32.h 30 Sep 2003 09:49:40 -0000 1.5 +++ TSRM/tsrm_config.w32.h 11 Jun 2006 07:38:14 -0000 @@ -4,10 +4,12 @@ #define HAVE_UTIME 1 #define HAVE_ALLOCA 1 #define HAVE_REALPATH 1 +#define HAVE_LIMITS_H 1 #include <malloc.h> #include <stdlib.h> #include <crtdbg.h> +#include <string.h> #undef inline #ifdef ZEND_WIN32_FORCE_INLINE @@ -16,5 +18,9 @@ # define inline #endif +#ifndef strcasecmp +#define strcasecmp(s1, s2) _stricmp(s1, s2) +#define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n) +#endif #endif Index: TSRM/tsrm_config_common.h =================================================================== RCS file: /repository/TSRM/tsrm_config_common.h,v retrieving revision 1.17.2.1 diff -u -r1.17.2.1 tsrm_config_common.h --- TSRM/tsrm_config_common.h 20 Dec 2005 14:24:14 -0000 1.17.2.1 +++ TSRM/tsrm_config_common.h 11 Jun 2006 07:24:20 -0000 @@ -57,4 +57,10 @@ # define tsrm_free_alloca(p) free(p) #endif +/* the path of least danger */ +#ifndef CHAR_MAX +# define CHAR_MAX 127 +# define CHAR_MIN 0 +#endif + #endif /* TSRM_CONFIG_COMMON_H */ Index: Zend/zend_API.c =================================================================== RCS file: /repository/ZendEngine2/zend_API.c,v retrieving revision 1.296.2.27.2.17 diff -u -r1.296.2.27.2.17 zend_API.c --- Zend/zend_API.c 7 Jun 2006 09:43:54 -0000 1.296.2.27.2.17 +++ Zend/zend_API.c 11 Jun 2006 07:20:23 -0000 @@ -1889,6 +1889,17 @@ #if HAVE_LIBDL || defined(HAVE_MACH_O_DYLD_H) #if !(defined(NETWARE) && defined(APACHE_1_BUILD)) if (module->handle) { +#ifdef ZTS + if (!module->globals_id_ptr) { + ts_rsrc_id tsrm_id; + + tsrm_id = tsrm_get_id(module->name); + + if (tsrm_id) { + ts_free_id(tsrm_id); + } + } +#endif DL_UNLOAD(module->handle); } #endif Index: Zend/zend_API.h =================================================================== RCS file: /repository/ZendEngine2/zend_API.h,v retrieving revision 1.207.2.8.2.3 diff -u -r1.207.2.8.2.3 zend_API.h --- Zend/zend_API.h 7 Jun 2006 09:43:54 -0000 1.207.2.8.2.3 +++ Zend/zend_API.h 11 Jun 2006 06:05:04 -0000 @@ -105,7 +105,7 @@ #define ZEND_EXTERN_MODULE_GLOBALS(module_name) \ extern ts_rsrc_id module_name##_globals_id; #define ZEND_INIT_MODULE_GLOBALS(module_name, globals_ctor, globals_dtor) \ - ts_allocate_id(&module_name##_globals_id, sizeof(zend_##module_name##_globals), (ts_allocate_ctor) globals_ctor, (ts_allocate_dtor) globals_dtor); + ts_allocate_id(&module_name##_globals_id, sizeof(zend_##module_name##_globals), (ts_allocate_ctor) globals_ctor, (ts_allocate_dtor) globals_dtor, #module_name); #else