ext/oj/cache.c in oj-3.14.1 vs ext/oj/cache.c in oj-3.14.2
- old
+ new
@@ -4,10 +4,11 @@
#if HAVE_PTHREAD_MUTEX_INIT
#include <pthread.h>
#endif
#include <stdlib.h>
+#include "mem.h"
#include "cache.h"
// The stdlib calloc, realloc, and free are used instead of the Ruby ALLOC,
// ALLOC_N, REALLOC, and xfree since the later could trigger a GC which will
// either corrupt memory or if the mark function locks will deadlock.
@@ -98,11 +99,11 @@
Slot * sp;
osize = c->size;
c->size = osize * 4;
c->mask = c->size - 1;
- c->slots = realloc((void *)c->slots, sizeof(Slot) * c->size);
+ c->slots = OJ_REALLOC((void *)c->slots, sizeof(Slot) * c->size);
memset((Slot *)c->slots + osize, 0, sizeof(Slot) * osize * 3);
end = (Slot *)c->slots + osize;
for (sp = (Slot *)c->slots; sp < end; sp++) {
Slot s = *sp;
Slot next = NULL;
@@ -126,11 +127,11 @@
volatile VALUE rkey;
while (REUSE_MAX < c->rcnt) {
if (NULL != (b = c->reuse)) {
c->reuse = b->next;
- free(b);
+ OJ_FREE(b);
c->rcnt--;
} else {
// An accounting error occured somewhere so correct it.
c->rcnt = 0;
}
@@ -141,11 +142,11 @@
return b->val;
}
}
rkey = c->form(key, len);
if (NULL == (b = c->reuse)) {
- b = calloc(1, sizeof(struct _slot));
+ b = OJ_CALLOC(1, sizeof(struct _slot));
} else {
c->reuse = b->next;
c->rcnt--;
}
b->hash = h;
@@ -172,11 +173,11 @@
CACHE_LOCK(c);
while (REUSE_MAX < c->rcnt) {
if (NULL != (b = c->reuse)) {
c->reuse = b->next;
- free(b);
+ OJ_FREE(b);
c->rcnt--;
} else {
// An accounting error occured somewhere so correct it.
c->rcnt = 0;
}
@@ -198,11 +199,11 @@
c->reuse = b->next;
c->rcnt--;
}
CACHE_UNLOCK(c);
if (NULL == b) {
- b = calloc(1, sizeof(struct _slot));
+ b = OJ_CALLOC(1, sizeof(struct _slot));
}
rkey = c->form(key, len);
b->hash = h;
memcpy(b->key, key, len);
b->klen = (uint8_t)len;
@@ -226,11 +227,11 @@
return rkey;
}
Cache cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking) {
- Cache c = calloc(1, sizeof(struct _cache));
+ Cache c = OJ_CALLOC(1, sizeof(struct _cache));
int shift = 0;
for (; REHASH_LIMIT < size; size /= 2, shift++) {
}
if (shift < MIN_SHIFT) {
@@ -241,11 +242,11 @@
#else
c->mutex = rb_mutex_new();
#endif
c->size = 1 << shift;
c->mask = c->size - 1;
- c->slots = calloc(c->size, sizeof(Slot));
+ c->slots = OJ_CALLOC(c->size, sizeof(Slot));
c->form = form;
c->xrate = 1; // low
c->mark = mark;
if (locking) {
c->intern = locking_intern;
@@ -266,14 +267,14 @@
Slot next;
Slot s;
for (s = c->slots[i]; NULL != s; s = next) {
next = s->next;
- free(s);
+ OJ_FREE(s);
}
}
- free((void *)c->slots);
- free(c);
+ OJ_FREE((void *)c->slots);
+ OJ_FREE(c);
}
void cache_mark(Cache c) {
uint64_t i;