Skip to content

Commit

Permalink
Change function and symbols names
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Feb 9, 2022
1 parent 6830082 commit 73d75fb
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 256 deletions.
41 changes: 15 additions & 26 deletions ext/ox/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define M 0x5bd1e995

typedef struct _slot {
struct _slot * next;
struct _slot *next;
VALUE val;
uint64_t hash;
volatile uint32_t use_cnt;
Expand All @@ -37,7 +37,7 @@ typedef struct _slot {
} * Slot;

typedef struct _cache {
volatile Slot * slots;
volatile Slot *slots;
volatile size_t cnt;
VALUE (*form)(const char *str, size_t len);
uint64_t size;
Expand All @@ -54,10 +54,6 @@ typedef struct _cache {
bool mark;
} * Cache;

void cache_set_form(Cache c, VALUE (*form)(const char *str, size_t len)) {
c->form = form;
}

static uint64_t hash_calc(const uint8_t *key, size_t len) {
const uint8_t *end = key + len;
const uint8_t *endless = key + (len & 0xFFFFFFFC);
Expand Down Expand Up @@ -94,8 +90,8 @@ static uint64_t hash_calc(const uint8_t *key, size_t len) {

static void rehash(Cache c) {
uint64_t osize;
Slot * end;
Slot * sp;
Slot *end;
Slot *sp;

osize = c->size;
c->size = osize * 4;
Expand All @@ -110,7 +106,7 @@ static void rehash(Cache c) {
*sp = NULL;
for (; NULL != s; s = next) {
uint64_t h = s->hash & c->mask;
Slot * bucket = (Slot *)c->slots + h;
Slot *bucket = (Slot *)c->slots + h;

next = s->next;
s->next = *bucket;
Expand All @@ -119,9 +115,9 @@ static void rehash(Cache c) {
}
}

static VALUE lockless_intern(Cache c, const char *key, size_t len, const char **keyp) {
static VALUE ox_lockless_intern(Cache c, const char *key, size_t len, const char **keyp) {
uint64_t h = hash_calc((const uint8_t *)key, len);
Slot * bucket = (Slot *)c->slots + (h & c->mask);
Slot *bucket = (Slot *)c->slots + (h & c->mask);
Slot b;
volatile VALUE rkey;

Expand Down Expand Up @@ -169,9 +165,9 @@ static VALUE lockless_intern(Cache c, const char *key, size_t len, const char **
return rkey;
}

static VALUE locking_intern(Cache c, const char *key, size_t len, const char **keyp) {
static VALUE ox_locking_intern(Cache c, const char *key, size_t len, const char **keyp) {
uint64_t h;
Slot * bucket;
Slot *bucket;
Slot b;
uint64_t old_size;
volatile VALUE rkey;
Expand Down Expand Up @@ -239,10 +235,7 @@ static VALUE locking_intern(Cache c, const char *key, size_t len, const char **k
return rkey;
}

Cache cache_create(size_t size,
VALUE (*form)(const char *str, size_t len),
bool mark,
bool locking) {
Cache ox_cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking) {
Cache c = calloc(1, sizeof(struct _cache));
int shift = 0;

Expand All @@ -263,18 +256,14 @@ Cache cache_create(size_t size,
c->xrate = 1; // low
c->mark = mark;
if (locking) {
c->intern = locking_intern;
c->intern = ox_locking_intern;
} else {
c->intern = lockless_intern;
c->intern = ox_lockless_intern;
}
return c;
}

void cache_set_expunge_rate(Cache c, int rate) {
c->xrate = (uint8_t)rate;
}

void cache_free(Cache c) {
void ox_cache_free(Cache c) {
uint64_t i;

for (i = 0; i < c->size; i++) {
Expand All @@ -290,7 +279,7 @@ void cache_free(Cache c) {
free(c);
}

void cache_mark(Cache c) {
void ox_cache_mark(Cache c) {
uint64_t i;

#if !HAVE_PTHREAD_MUTEX_INIT
Expand Down Expand Up @@ -333,7 +322,7 @@ void cache_mark(Cache c) {
}

VALUE
cache_intern(Cache c, const char *key, size_t len, const char **keyp) {
ox_cache_intern(Cache c, const char *key, size_t len, const char **keyp) {
if (CACHE_MAX_KEY <= len) {
if (NULL != keyp) {
volatile VALUE rkey = c->form(key, len);
Expand Down
16 changes: 7 additions & 9 deletions ext/ox/cache.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) 2021 Peter Ohler. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license details.

#ifndef CACHE_H
#define CACHE_H
#ifndef OX_CACHE_H
#define OX_CACHE_H

#include <ruby.h>
#include <stdbool.h>
Expand All @@ -11,11 +11,9 @@

struct _cache;

extern struct _cache *cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
extern void cache_free(struct _cache *c);
extern void cache_mark(struct _cache *c);
extern void cache_set_form(struct _cache *c, VALUE (*form)(const char *str, size_t len));
extern VALUE cache_intern(struct _cache *c, const char *key, size_t len, const char **keyp);
extern void cache_set_expunge_rate(struct _cache *c, int rate);
extern struct _cache *ox_cache_create(size_t size, VALUE (*form)(const char *str, size_t len), bool mark, bool locking);
extern void ox_cache_free(struct _cache *c);
extern void ox_cache_mark(struct _cache *c);
extern VALUE ox_cache_intern(struct _cache *c, const char *key, size_t len, const char **keyp);

#endif /* CACHE_H */
#endif /* OX_CACHE_H */
57 changes: 26 additions & 31 deletions ext/ox/intern.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
#include "cache.h"
#include "ox.h"

static struct _cache *str_cache = NULL;
static VALUE str_cache_obj;
// These are statics but in an attempt to stop the cross linking or maybe
// something in Ruby they all have been given an ox prefix.
static struct _cache *ox_str_cache = NULL;
static VALUE ox_str_cache_obj;

static struct _cache *sym_cache = NULL;
static VALUE sym_cache_obj;
static struct _cache *ox_sym_cache = NULL;
static VALUE ox_sym_cache_obj;

static struct _cache *attr_cache = NULL;
static VALUE attr_cache_obj;
static struct _cache *ox_attr_cache = NULL;
static VALUE ox_attr_cache_obj;

static struct _cache *id_cache = NULL;
static VALUE id_cache_obj;
static struct _cache *ox_id_cache = NULL;
static VALUE ox_id_cache_obj;

static VALUE form_str(const char *str, size_t len) {
return rb_str_freeze(rb_utf8_str_new(str, len));
Expand Down Expand Up @@ -67,21 +69,21 @@ static VALUE form_id(const char *str, size_t len) {
void ox_hash_init() {
VALUE cache_class = rb_define_class_under(Ox, "Cache", rb_cObject);

str_cache = cache_create(0, form_str, true, false);
str_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, str_cache);
rb_gc_register_address(&str_cache_obj);
ox_str_cache = ox_cache_create(0, form_str, true, false);
ox_str_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_str_cache);
rb_gc_register_address(&ox_str_cache_obj);

sym_cache = cache_create(0, form_sym, true, false);
sym_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, sym_cache);
rb_gc_register_address(&sym_cache_obj);
ox_sym_cache = ox_cache_create(0, form_sym, true, false);
ox_sym_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_sym_cache);
rb_gc_register_address(&ox_sym_cache_obj);

attr_cache = cache_create(0, form_attr, false, false);
attr_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, attr_cache);
rb_gc_register_address(&attr_cache_obj);
ox_attr_cache = ox_cache_create(0, form_attr, false, false);
ox_attr_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_attr_cache);
rb_gc_register_address(&ox_attr_cache_obj);

id_cache = cache_create(0, form_id, false, false);
id_cache_obj = Data_Wrap_Struct(cache_class, cache_mark, cache_free, id_cache);
rb_gc_register_address(&id_cache_obj);
ox_id_cache = ox_cache_create(0, form_id, false, false);
ox_id_cache_obj = Data_Wrap_Struct(cache_class, ox_cache_mark, ox_cache_free, ox_id_cache);
rb_gc_register_address(&ox_id_cache_obj);
}

VALUE
Expand All @@ -92,21 +94,21 @@ ox_str_intern(const char *key, size_t len, const char **keyp) {
#if HAVE_RB_ENC_INTERNED_STR && 0
return rb_enc_interned_str(key, len, rb_utf8_encoding());
#else
return cache_intern(str_cache, key, len, keyp);
return ox_cache_intern(ox_str_cache, key, len, keyp);
#endif
}

VALUE
ox_sym_intern(const char *key, size_t len, const char **keyp) {
return cache_intern(sym_cache, key, len, keyp);
return ox_cache_intern(ox_sym_cache, key, len, keyp);
}

ID ox_attr_intern(const char *key, size_t len) {
return cache_intern(attr_cache, key, len, NULL);
return ox_cache_intern(ox_attr_cache, key, len, NULL);
}

ID ox_id_intern(const char *key, size_t len) {
return cache_intern(id_cache, key, len, NULL);
return ox_cache_intern(ox_id_cache, key, len, NULL);
}

char *ox_strndup(const char *s, size_t len) {
Expand All @@ -118,13 +120,6 @@ char *ox_strndup(const char *s, size_t len) {
return d;
}

void intern_cleanup() {
cache_free(str_cache);
cache_free(sym_cache);
cache_free(attr_cache);
cache_free(id_cache);
}

VALUE
ox_utf8_name(const char *str, size_t len, rb_encoding *encoding, const char **strp) {
return ox_str_intern(str, len, strp);
Expand Down
3 changes: 2 additions & 1 deletion ext/ox/obj_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "ruby.h"
#include "ruby/encoding.h"

#include "base64.h"
#include "ox.h"
#include "intern.h"
Expand Down Expand Up @@ -152,7 +153,7 @@ classname2class(const char *name, PInfo pi, VALUE base_class) {
VALUE *slot;
VALUE clas;

if (Qundef == (clas = ox_cache_get(ox_class_cache, name, &slot, 0))) {
if (Qundef == (clas = slot_cache_get(ox_class_cache, name, &slot, 0))) {
char class_name[1024];
char *s;
const char *n = name;
Expand Down
8 changes: 2 additions & 6 deletions ext/ox/ox.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ VALUE ox_struct_class;
VALUE ox_syntax_error_class;
VALUE ox_time_class;

Cache ox_symbol_cache = 0;
Cache ox_class_cache = 0;
Cache ox_attr_cache = 0;
SlotCache ox_class_cache = 0;

static VALUE abort_sym;
static VALUE active_sym;
Expand Down Expand Up @@ -1646,9 +1644,7 @@ void Init_ox() {
rb_gc_register_address(&ox_time_class);
rb_gc_register_address(&ox_version_sym);

ox_cache_new(&ox_symbol_cache);
ox_cache_new(&ox_class_cache);
ox_cache_new(&ox_attr_cache);
slot_cache_new(&ox_class_cache);

ox_sax_define();
ox_hash_init();
Expand Down
6 changes: 2 additions & 4 deletions ext/ox/ox.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern "C" {
#include "attr.h"
#include "err.h"
#include "helper.h"
#include "oxcache.h"
#include "slotcache.h"
#include "type.h"

#define raise_error(msg, xml, current) _ox_raise_error(msg, xml, current, __FILE__, __LINE__)
Expand Down Expand Up @@ -238,9 +238,7 @@ extern VALUE ox_raw_clas;
extern VALUE ox_doctype_clas;
extern VALUE ox_cdata_clas;

extern Cache ox_symbol_cache;
extern Cache ox_class_cache;
extern Cache ox_attr_cache;
extern SlotCache ox_class_cache;

extern void ox_init_builder(VALUE ox);

Expand Down
Loading

0 comments on commit 73d75fb

Please sign in to comment.