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