Merge up to gearmand.
[m6w6/libmemcached] / libmemcachedprotocol / cache.c
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
38 #include "config.h"
39
40 #include <stdlib.h>
41 #include <string.h>
42 #include <inttypes.h>
43
44 #ifndef NDEBUG
45 #include <signal.h>
46 #endif
47
48 #include <libmemcachedprotocol/common.h>
49
50 #ifndef NDEBUG
51 const uint64_t redzone_pattern = 0xdeadbeefcafebabe;
52 int cache_error = 0;
53 #endif
54
55 const size_t initial_pool_size = 64;
56
57 cache_t* cache_create(const char *name, size_t bufsize, size_t align,
58 cache_constructor_t* constructor,
59 cache_destructor_t* destructor) {
60 cache_t* ret = calloc(1, sizeof(cache_t));
61 size_t name_length= strlen(name);
62 char* nm= calloc(1, (sizeof(char) * name_length) +1);
63 memcpy(nm, name, name_length);
64 void** ptr = calloc(initial_pool_size, bufsize);
65 if (ret == NULL || nm == NULL || ptr == NULL ||
66 pthread_mutex_init(&ret->mutex, NULL) == -1) {
67 free(ret);
68 free(nm);
69 free(ptr);
70 return NULL;
71 }
72
73 ret->name = nm;
74 ret->ptr = ptr;
75 ret->freetotal = initial_pool_size;
76 ret->constructor = constructor;
77 ret->destructor = destructor;
78
79 #ifndef NDEBUG
80 ret->bufsize = bufsize + 2 * sizeof(redzone_pattern);
81 #else
82 ret->bufsize = bufsize;
83 #endif
84
85 (void)align;
86
87 return ret;
88 }
89
90 static inline void* get_object(void *ptr) {
91 #ifndef NDEBUG
92 uint64_t *pre = ptr;
93 return pre + 1;
94 #else
95 return ptr;
96 #endif
97 }
98
99 void cache_destroy(cache_t *cache) {
100 while (cache->freecurr > 0) {
101 void *ptr = cache->ptr[--cache->freecurr];
102 if (cache->destructor) {
103 cache->destructor(get_object(ptr), NULL);
104 }
105 free(ptr);
106 }
107 free(cache->name);
108 free(cache->ptr);
109 pthread_mutex_destroy(&cache->mutex);
110 }
111
112 void* cache_alloc(cache_t *cache) {
113 void *ret;
114 void *object;
115 pthread_mutex_lock(&cache->mutex);
116 if (cache->freecurr > 0) {
117 ret = cache->ptr[--cache->freecurr];
118 object = get_object(ret);
119 } else {
120 object = ret = malloc(cache->bufsize);
121 if (ret != NULL) {
122 object = get_object(ret);
123
124 if (cache->constructor != NULL &&
125 cache->constructor(object, NULL, 0) != 0) {
126 free(ret);
127 object = NULL;
128 }
129 }
130 }
131 pthread_mutex_unlock(&cache->mutex);
132
133 #ifndef NDEBUG
134 if (object != NULL) {
135 /* add a simple form of buffer-check */
136 uint64_t *pre = ret;
137 *pre = redzone_pattern;
138 ret = pre+1;
139 memcpy(((char*)ret) + cache->bufsize - (2 * sizeof(redzone_pattern)),
140 &redzone_pattern, sizeof(redzone_pattern));
141 }
142 #endif
143
144 return object;
145 }
146
147 void cache_free(cache_t *cache, void *ptr) {
148 pthread_mutex_lock(&cache->mutex);
149
150 #ifndef NDEBUG
151 /* validate redzone... */
152 if (memcmp(((char*)ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)),
153 &redzone_pattern, sizeof(redzone_pattern)) != 0) {
154 raise(SIGABRT);
155 cache_error = 1;
156 pthread_mutex_unlock(&cache->mutex);
157 return;
158 }
159 uint64_t *pre = ptr;
160 --pre;
161 if (*pre != redzone_pattern) {
162 raise(SIGABRT);
163 cache_error = -1;
164 pthread_mutex_unlock(&cache->mutex);
165 return;
166 }
167 ptr = pre;
168 #endif
169 if (cache->freecurr < cache->freetotal) {
170 cache->ptr[cache->freecurr++] = ptr;
171 } else {
172 /* try to enlarge free connections array */
173 size_t newtotal = cache->freetotal * 2;
174 void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal);
175 if (new_free) {
176 cache->freetotal = newtotal;
177 cache->ptr = new_free;
178 cache->ptr[cache->freecurr++] = ptr;
179 } else {
180 if (cache->destructor) {
181 cache->destructor(ptr, NULL);
182 }
183 free(ptr);
184
185 }
186 }
187 pthread_mutex_unlock(&cache->mutex);
188 }
189