Merge in all of the build tree.
[awesomized/libmemcached] / libmemcached / protocol / 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 <stdlib.h>
39 #include <string.h>
40 #include <inttypes.h>
41
42 #ifndef NDEBUG
43 #include <signal.h>
44 #endif
45
46 #include "cache.h"
47
48 #ifndef NDEBUG
49 const uint64_t redzone_pattern = 0xdeadbeefcafebabe;
50 int cache_error = 0;
51 #endif
52
53 const size_t initial_pool_size = 64;
54
55 cache_t* cache_create(const char *name, size_t bufsize, size_t align,
56 cache_constructor_t* constructor,
57 cache_destructor_t* destructor) {
58 cache_t* ret = calloc(1, sizeof(cache_t));
59 char* nm = strdup(name);
60 void** ptr = calloc(initial_pool_size, bufsize);
61 if (ret == NULL || nm == NULL || ptr == NULL ||
62 pthread_mutex_init(&ret->mutex, NULL) == -1) {
63 free(ret);
64 free(nm);
65 free(ptr);
66 return NULL;
67 }
68
69 ret->name = nm;
70 ret->ptr = ptr;
71 ret->freetotal = initial_pool_size;
72 ret->constructor = constructor;
73 ret->destructor = destructor;
74
75 #ifndef NDEBUG
76 ret->bufsize = bufsize + 2 * sizeof(redzone_pattern);
77 #else
78 ret->bufsize = bufsize;
79 #endif
80
81 (void)align;
82
83 return ret;
84 }
85
86 static inline void* get_object(void *ptr) {
87 #ifndef NDEBUG
88 uint64_t *pre = ptr;
89 return pre + 1;
90 #else
91 return ptr;
92 #endif
93 }
94
95 void cache_destroy(cache_t *cache) {
96 while (cache->freecurr > 0) {
97 void *ptr = cache->ptr[--cache->freecurr];
98 if (cache->destructor) {
99 cache->destructor(get_object(ptr), NULL);
100 }
101 free(ptr);
102 }
103 free(cache->name);
104 free(cache->ptr);
105 pthread_mutex_destroy(&cache->mutex);
106 }
107
108 void* cache_alloc(cache_t *cache) {
109 void *ret;
110 void *object;
111 pthread_mutex_lock(&cache->mutex);
112 if (cache->freecurr > 0) {
113 ret = cache->ptr[--cache->freecurr];
114 object = get_object(ret);
115 } else {
116 object = ret = malloc(cache->bufsize);
117 if (ret != NULL) {
118 object = get_object(ret);
119
120 if (cache->constructor != NULL &&
121 cache->constructor(object, NULL, 0) != 0) {
122 free(ret);
123 object = NULL;
124 }
125 }
126 }
127 pthread_mutex_unlock(&cache->mutex);
128
129 #ifndef NDEBUG
130 if (object != NULL) {
131 /* add a simple form of buffer-check */
132 uint64_t *pre = ret;
133 *pre = redzone_pattern;
134 ret = pre+1;
135 memcpy(((char*)ret) + cache->bufsize - (2 * sizeof(redzone_pattern)),
136 &redzone_pattern, sizeof(redzone_pattern));
137 }
138 #endif
139
140 return object;
141 }
142
143 void cache_free(cache_t *cache, void *ptr) {
144 pthread_mutex_lock(&cache->mutex);
145
146 #ifndef NDEBUG
147 /* validate redzone... */
148 if (memcmp(((char*)ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)),
149 &redzone_pattern, sizeof(redzone_pattern)) != 0) {
150 raise(SIGABRT);
151 cache_error = 1;
152 pthread_mutex_unlock(&cache->mutex);
153 return;
154 }
155 uint64_t *pre = ptr;
156 --pre;
157 if (*pre != redzone_pattern) {
158 raise(SIGABRT);
159 cache_error = -1;
160 pthread_mutex_unlock(&cache->mutex);
161 return;
162 }
163 ptr = pre;
164 #endif
165 if (cache->freecurr < cache->freetotal) {
166 cache->ptr[cache->freecurr++] = ptr;
167 } else {
168 /* try to enlarge free connections array */
169 size_t newtotal = cache->freetotal * 2;
170 void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal);
171 if (new_free) {
172 cache->freetotal = newtotal;
173 cache->ptr = new_free;
174 cache->ptr[cache->freecurr++] = ptr;
175 } else {
176 if (cache->destructor) {
177 cache->destructor(ptr, NULL);
178 }
179 free(ptr);
180
181 }
182 }
183 pthread_mutex_unlock(&cache->mutex);
184 }
185