summaryrefslogtreecommitdiff
path: root/ext/mysqlnd/mysqlnd_qcache.c
blob: e7f9fcaa8256fdb148b26c25b201dd70703f5f5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
  +----------------------------------------------------------------------+
  | PHP Version 6                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006-2008 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Authors: Georg Richter <georg@mysql.com>                             |
  |          Andrey Hristov <andrey@mysql.com>                           |
  |          Ulf Wendel <uwendel@mysql.com>                              |
  +----------------------------------------------------------------------+
*/

/* $Id$ */
#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_statistics.h"

#define MYSQLND_SILENT

#ifdef ZTS
#define LOCK_QCACHE(cache)		tsrm_mutex_lock((cache)->LOCK_access)
#define UNLOCK_QCACHE(cache)	tsrm_mutex_unlock((cache)->LOCK_access)
#else
#define LOCK_QCACHE(cache)
#define UNLOCK_QCACHE(cache)
#endif


/* {{{ mysqlnd_qcache_init_cache */
PHPAPI MYSQLND_QCACHE * mysqlnd_qcache_init_cache()
{
	MYSQLND_QCACHE *cache = calloc(1, sizeof(MYSQLND_QCACHE));
#ifndef MYSQLND_SILENT
	php_printf("[mysqlnd_qcache_init_cache %p]\n", cache);
#endif

	cache->references = 1;
#ifdef ZTS
	cache->LOCK_access = tsrm_mutex_alloc();
#endif
	cache->ht = malloc(sizeof(HashTable));
	zend_hash_init(cache->ht, 10 /* init_elements */, NULL, NULL, TRUE /*pers*/);

	return cache;
}
/* }}} */


/* {{{ mysqlnd_qcache_get_cache_reference */
PHPAPI MYSQLND_QCACHE * mysqlnd_qcache_get_cache_reference(MYSQLND_QCACHE * const cache)
{
	if (cache) {
#ifndef MYSQLND_SILENT
		php_printf("[mysqlnd_qcache_get_cache_reference %p will become %d]\n", cache, cache->references+1);
#endif
		LOCK_QCACHE(cache);
		cache->references++;
		UNLOCK_QCACHE(cache);
	}
	return cache;
}
/* }}} */


/* {{{ mysqlnd_qcache_free_cache */
/*
  As this call will happen on MSHUTDOWN(), then we don't need to copy the zvals with
  copy_ctor but scrap what they point to with zval_dtor() and then just free our
  pre-allocated block. Precondition is that we ZVAL_NULL() the zvals when we put them
  to the free list after usage. We ZVAL_NULL() them when we allocate them in the 
  constructor of the cache.
*/
static
void mysqlnd_qcache_free_cache(MYSQLND_QCACHE *cache)
{
#ifndef MYSQLND_SILENT
	php_printf("[mysqlnd_qcache_free_cache %p]\n", cache);
#endif

#ifdef ZTS
	tsrm_mutex_free(cache->LOCK_access);
#endif
	zend_hash_destroy(cache->ht);
	free(cache->ht);
	free(cache);
}
/* }}} */


/* {{{ mysqlnd_qcache_free_cache_reference */
PHPAPI void mysqlnd_qcache_free_cache_reference(MYSQLND_QCACHE **cache)
{
	if (*cache) {
		zend_bool to_free;
#ifndef MYSQLND_SILENT
		php_printf("[mysqlnd_qcache_free_cache_reference %p] refs=%d\n", *cache, (*cache)->references);
#endif
		LOCK_QCACHE(*cache);
		to_free = --(*cache)->references == 0;
		/* Unlock before destroying */
		UNLOCK_QCACHE(*cache);
		if (to_free) {
			mysqlnd_qcache_free_cache(*cache);
		}
		*cache = NULL;
	}
}
/* }}} */


/* {{{ mysqlnd_qcache_free_cache_reference */
PHPAPI void mysqlnd_qcache_stats(const MYSQLND_QCACHE * const cache, zval *return_value)
{
	if (cache) {
		LOCK_QCACHE(cache);
		array_init(return_value);
		add_assoc_long_ex(return_value, "references",	sizeof("references"),	cache->references);
		UNLOCK_QCACHE(cache);
	} else {
		ZVAL_NULL(return_value);
	}
}
/* }}} */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */