1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #include "src/config.h"
3 #ifndef HAVE_UMEM_H
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include <inttypes.h>
8 
9 #ifndef NDEBUG
10 #include <signal.h>
11 #endif
12 
13 #include "cache.h"
14 
15 #ifndef NDEBUG
16 const uint64_t redzone_pattern = 0xdeadbeefcafebabe;
17 int cache_error = 0;
18 #endif
19 
20 const int initial_pool_size = 64;
21 
cache_create(const char *name, size_t bufsize, size_t align, cache_constructor_t* constructor, cache_destructor_t* destructor)22 cache_t* cache_create(const char *name, size_t bufsize, size_t align,
23                       cache_constructor_t* constructor,
24                       cache_destructor_t* destructor) {
25     cache_t* ret = calloc(1, sizeof(cache_t));
26     char* nm = strdup(name);
27     void** ptr = calloc(initial_pool_size, bufsize);
28     if (ret == NULL || nm == NULL || ptr == NULL) {
29         free(ret);
30         free(nm);
31         free(ptr);
32         return NULL;
33     }
34     cb_mutex_initialize(&ret->mutex);
35     ret->name = nm;
36     ret->ptr = ptr;
37     ret->freetotal = initial_pool_size;
38     ret->constructor = constructor;
39     ret->destructor = destructor;
40 
41 #ifndef NDEBUG
42     ret->bufsize = bufsize + 2 * sizeof(redzone_pattern);
43 #else
44     ret->bufsize = bufsize;
45 #endif
46     (void)align;
47 
48     return ret;
49 }
50 
get_object(void *ptr)51 static void* get_object(void *ptr) {
52 #ifndef NDEBUG
53     uint64_t *pre = ptr;
54     return pre + 1;
55 #endif
56     return ptr;
57 }
58 
cache_destroy(cache_t *cache)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     cb_mutex_destroy(&cache->mutex);
70 }
71 
cache_alloc(cache_t *cache)72 void* cache_alloc(cache_t *cache) {
73     void *ret;
74     void *object;
75     cb_mutex_enter(&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     cb_mutex_exit(&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 
cache_free(cache_t *cache, void *ptr)107 void cache_free(cache_t *cache, void *ptr) {
108 #ifndef NDEBUG
109     uint64_t *pre;
110 #endif
111 
112     cb_mutex_enter(&cache->mutex);
113 
114 #ifndef NDEBUG
115     /* validate redzone... */
116     if (memcmp(((char*)ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)),
117                &redzone_pattern, sizeof(redzone_pattern)) != 0) {
118         raise(SIGABRT);
119         cache_error = 1;
120         cb_mutex_exit(&cache->mutex);
121         return;
122     }
123     pre = ptr;
124     --pre;
125     if (*pre != redzone_pattern) {
126         raise(SIGABRT);
127         cache_error = -1;
128         cb_mutex_exit(&cache->mutex);
129         return;
130     }
131     ptr = pre;
132 #endif
133     if (cache->freecurr < cache->freetotal) {
134         cache->ptr[cache->freecurr++] = ptr;
135     } else {
136         /* try to enlarge free connections array */
137         size_t newtotal = cache->freetotal * 2;
138         void **new_free = realloc(cache->ptr, sizeof(char *) * newtotal);
139         if (new_free) {
140             cache->freetotal = (int)newtotal;
141             cache->ptr = new_free;
142             cache->ptr[cache->freecurr++] = ptr;
143         } else {
144             if (cache->destructor) {
145                 cache->destructor(ptr, NULL);
146             }
147             free(ptr);
148 
149         }
150     }
151     cb_mutex_exit(&cache->mutex);
152 }
153 #endif
154