1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 2006-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Georg Richter <georg@mysql.com> |
16 : | Andrey Hristov <andrey@mysql.com> |
17 : | Ulf Wendel <uwendel@mysql.com> |
18 : +----------------------------------------------------------------------+
19 : */
20 :
21 : /* $Id: mysqlnd_qcache.c 272367 2008-12-31 11:12:40Z sebastian $ */
22 : #include "php.h"
23 : #include "mysqlnd.h"
24 : #include "mysqlnd_priv.h"
25 : #include "mysqlnd_statistics.h"
26 :
27 : #define MYSQLND_SILENT
28 :
29 : #ifdef ZTS
30 : #define LOCK_QCACHE(cache) tsrm_mutex_lock((cache)->LOCK_access)
31 : #define UNLOCK_QCACHE(cache) tsrm_mutex_unlock((cache)->LOCK_access)
32 : #else
33 : #define LOCK_QCACHE(cache)
34 : #define UNLOCK_QCACHE(cache)
35 : #endif
36 :
37 :
38 : /* {{{ mysqlnd_qcache_init_cache */
39 : PHPAPI MYSQLND_QCACHE * mysqlnd_qcache_init_cache()
40 34014 : {
41 34014 : MYSQLND_QCACHE *cache = calloc(1, sizeof(MYSQLND_QCACHE));
42 : #ifndef MYSQLND_SILENT
43 : php_printf("[mysqlnd_qcache_init_cache %p]\n", cache);
44 : #endif
45 :
46 34014 : cache->references = 1;
47 : #ifdef ZTS
48 : cache->LOCK_access = tsrm_mutex_alloc();
49 : #endif
50 34014 : cache->ht = malloc(sizeof(HashTable));
51 34014 : zend_hash_init(cache->ht, 10 /* init_elements */, NULL, NULL, TRUE /*pers*/);
52 :
53 34014 : return cache;
54 : }
55 : /* }}} */
56 :
57 :
58 : /* {{{ mysqlnd_qcache_get_cache_reference */
59 : PHPAPI MYSQLND_QCACHE * mysqlnd_qcache_get_cache_reference(MYSQLND_QCACHE * const cache)
60 0 : {
61 0 : if (cache) {
62 : #ifndef MYSQLND_SILENT
63 : php_printf("[mysqlnd_qcache_get_cache_reference %p will become %d]\n", cache, cache->references+1);
64 : #endif
65 : LOCK_QCACHE(cache);
66 0 : cache->references++;
67 : UNLOCK_QCACHE(cache);
68 : }
69 0 : return cache;
70 : }
71 : /* }}} */
72 :
73 :
74 : /* {{{ mysqlnd_qcache_free_cache */
75 : /*
76 : As this call will happen on MSHUTDOWN(), then we don't need to copy the zvals with
77 : copy_ctor but scrap what they point to with zval_dtor() and then just free our
78 : pre-allocated block. Precondition is that we ZVAL_NULL() the zvals when we put them
79 : to the free list after usage. We ZVAL_NULL() them when we allocate them in the
80 : constructor of the cache.
81 : */
82 : static
83 : void mysqlnd_qcache_free_cache(MYSQLND_QCACHE *cache)
84 34078 : {
85 : #ifndef MYSQLND_SILENT
86 : php_printf("[mysqlnd_qcache_free_cache %p]\n", cache);
87 : #endif
88 :
89 : #ifdef ZTS
90 : tsrm_mutex_free(cache->LOCK_access);
91 : #endif
92 34078 : zend_hash_destroy(cache->ht);
93 34078 : free(cache->ht);
94 34078 : free(cache);
95 34078 : }
96 : /* }}} */
97 :
98 :
99 : /* {{{ mysqlnd_qcache_free_cache_reference */
100 : PHPAPI void mysqlnd_qcache_free_cache_reference(MYSQLND_QCACHE **cache)
101 34078 : {
102 34078 : if (*cache) {
103 : zend_bool to_free;
104 : #ifndef MYSQLND_SILENT
105 : php_printf("[mysqlnd_qcache_free_cache_reference %p] refs=%d\n", *cache, (*cache)->references);
106 : #endif
107 : LOCK_QCACHE(*cache);
108 34078 : to_free = --(*cache)->references == 0;
109 : /* Unlock before destroying */
110 : UNLOCK_QCACHE(*cache);
111 34078 : if (to_free) {
112 34078 : mysqlnd_qcache_free_cache(*cache);
113 : }
114 34078 : *cache = NULL;
115 : }
116 34078 : }
117 : /* }}} */
118 :
119 :
120 : /* {{{ mysqlnd_qcache_free_cache_reference */
121 : PHPAPI void mysqlnd_qcache_stats(const MYSQLND_QCACHE * const cache, zval *return_value)
122 0 : {
123 0 : if (cache) {
124 : LOCK_QCACHE(cache);
125 0 : array_init(return_value);
126 0 : add_assoc_long_ex(return_value, "references", sizeof("references"), cache->references);
127 : UNLOCK_QCACHE(cache);
128 : } else {
129 0 : ZVAL_NULL(return_value);
130 : }
131 0 : }
132 : /* }}} */
133 :
134 : /*
135 : * Local variables:
136 : * tab-width: 4
137 : * c-basic-offset: 4
138 : * End:
139 : * vim600: noet sw=4 ts=4 fdm=marker
140 : * vim<600: noet sw=4 ts=4
141 : */
|