Extend lp:655423 just to see if we can trigger it via any other method.
[awesomized/libmemcached] / libmemcached / protocol / cache.c
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5
6 #ifndef NDEBUG
7 #include <signal.h>
8 #endif
9
10 #include "cache.h"
11
12 #ifndef NDEBUG
13 const uint64_t redzone_pattern = 0xdeadbeefcafebabe;
14 int cache_error = 0;
15 #endif
16
17 const size_t initial_pool_size = 64;
18
19 cache_t* cache_create(const char *name, size_t bufsize, size_t align,
20 cache_constructor_t* constructor,
21 cache_destructor_t* destructor) {
22 cache_t* ret = calloc(1, sizeof(cache_t));
23 char* nm = strdup(name);
24 void** ptr = calloc(initial_pool_size, bufsize);
25 if (ret == NULL || nm == NULL || ptr == NULL ||
26 pthread_mutex_init(&ret->mutex, NULL) == -1) {
27 free(ret);
28 free(nm);
29 free(ptr);
30 return NULL;
31 }
32
33 ret->name = nm;
34 ret->ptr = ptr;
35 ret->freetotal = initial_pool_size;
36 ret->constructor = constructor;
37 ret->destructor = destructor;
38
39 #ifndef NDEBUG
40 ret->bufsize = bufsize + 2 * sizeof(redzone_pattern);
41 #else
42 ret->bufsize = bufsize;
43 #endif
44
45 (void)align;
46
47 return ret;
48 }
49
50 static inline void* get_object(void *ptr) {
51 #ifndef NDEBUG
52 uint64_t *pre = ptr;
53 return pre + 1;
54 #else
55 return ptr;
56 #endif
57 }
58
59 void cache_destroy(cache_t *cache) {
60 while (cache->freecurr > 0) {
61 void *ptr = cache->ptr[--cache->freecurr];
62 if (cache->destructor) {
63 cache->destructor(get_object(ptr), NULL);
64 }
65 free(ptr);
66 }
67 free(cache->name);
68 free(cache->ptr);
69 pthread_mutex_destroy(&cache->mutex);
70 }
71
72 void* cache_alloc(cache_t *cache) {
73 void *ret;
74 void *object;
75 pthread_mutex_lock(&cache->mutex);
76 if (cache->freecurr > 0) {
77 ret = cache->ptr[--cache->freecurr];
78 object = get_object(ret);
79 } else {
80 object = ret = malloc(cache->bufsize);
81 if (ret != NULL) {
82 object = get_object(ret);
83
84 if (cache->constructor != NULL &&
85 cache->constructor(object, NULL, 0) != 0) {
86 free(ret);
87 object = NULL;
88 }
89 }
90 }
91 pthread_mutex_unlock(&cache->mutex);
92
93 #ifndef NDEBUG
94 if (object != NULL) {
95 /* add a simple form of buffer-check */
96 uint64_t *pre = ret;
97 *pre = redzone_pattern;
98 ret = pre+1;
99 memcpy(((char*)ret) + cache->bufsize - (2 * sizeof(redzone_pattern)),
100 &redzone_pattern, sizeof(redzone_pattern));
101 }
102 #endif
103
104 return object;
105 }
106
107 void cache_free(cache_t *cache, void *ptr) {
108 pthread_mutex_lock(&cache->mutex);
109
110 #ifndef NDEBUG
111 /* validate redzone... */
112 if (memcmp(((char*)ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)),
113 &redzone_pattern, sizeof(redzone_pattern)) != 0) {
114 raise(SIGABRT);
115 cache_error = 1;
116 pthread_mutex_unlock(&cache->mutex);
117 return;
118 }
119 uint64_t *pre = ptr;
120 --pre;
121 if (*pre != redzone_pattern) {
122 raise(SIGABRT);
123 cache_error = -1;
124 pthread_mutex_unlock(&cache->mutex);
125 return;
126 }
127 ptr = pre;
128 #endif
129 if (cache->freecurr < cache->freetotal) {
130 cache->ptr[cache->freecurr++] = ptr;
131 } else {
132 /* try to enlarge free connections array */
133 size_t newtotal = cache->freetotal * 2;
134 void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal);
135 if (new_free) {
136 cache->freetotal = newtotal;
137 cache->ptr = new_free;
138 cache->ptr[cache->freecurr++] = ptr;
139 } else {
140 if (cache->destructor) {
141 cache->destructor(ptr, NULL);
142 }
143 free(ptr);
144
145 }
146 }
147 pthread_mutex_unlock(&cache->mutex);
148 }
149