Created
February 13, 2012 18:52
-
-
Save tenderlove/1819039 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 4237aa29a9771208a2206ff2f7ec4745caee0cbf Mon Sep 17 00:00:00 2001 | |
From: Aaron Patterson <[email protected]> | |
Date: Mon, 13 Feb 2012 10:40:06 -0800 | |
Subject: [PATCH 1/2] mkmf should abort if the user is missing libhashring | |
--- | |
ext/hash_ring/extconf.rb | 2 +- | |
1 files changed, 1 insertions(+), 1 deletions(-) | |
diff --git a/ext/hash_ring/extconf.rb b/ext/hash_ring/extconf.rb | |
index 7c08ca9..688ed43 100644 | |
--- a/ext/hash_ring/extconf.rb | |
+++ b/ext/hash_ring/extconf.rb | |
@@ -4,6 +4,6 @@ extension_name = 'hash_ring' | |
dir_config(extension_name) | |
-have_library("hashring") | |
+abort "libhashring is required" unless have_library("hashring") | |
create_makefile("hash_ring/hash_ring") | |
-- | |
1.7.6 | |
From 176d66f3fb3ffa95326b6495a78fda4aff88a4bf Mon Sep 17 00:00:00 2001 | |
From: Aaron Patterson <[email protected]> | |
Date: Mon, 13 Feb 2012 10:50:05 -0800 | |
Subject: [PATCH 2/2] initialize the underlying struct to NULL, add checks for | |
initialization | |
--- | |
ext/hash_ring/hash_ring.c | 29 ++++++++++++++++++----------- | |
1 files changed, 18 insertions(+), 11 deletions(-) | |
diff --git a/ext/hash_ring/hash_ring.c b/ext/hash_ring/hash_ring.c | |
index 23ed7e8..c0dd4d7 100644 | |
--- a/ext/hash_ring/hash_ring.c | |
+++ b/ext/hash_ring/hash_ring.c | |
@@ -1,18 +1,21 @@ | |
#include "ruby.h" | |
#include "hash_ring.h" | |
+#define CHECK_RING(_data) \ | |
+ if (!data) \ | |
+ rb_raise(rb_eRuntimeError, "uninitialized data"); | |
+ | |
VALUE cHashRing; | |
static VALUE hash_ring_dealloc(void * ring) | |
{ | |
- hash_ring_free(ring); | |
+ if (ring) | |
+ hash_ring_free(ring); | |
} | |
static VALUE hash_ring_alloc(VALUE klass) | |
{ | |
- hash_ring_t * ring; | |
- VALUE obj = Data_Make_Struct(klass, hash_ring_t, NULL, hash_ring_dealloc, ring); | |
- return obj; | |
+ return Data_Wrap_Struct(klass, NULL, hash_ring_dealloc, NULL); | |
} | |
static VALUE method_initialize(VALUE self, VALUE num_replicas, VALUE hash_function) | |
@@ -21,18 +24,18 @@ static VALUE method_initialize(VALUE self, VALUE num_replicas, VALUE hash_functi | |
Check_Type(hash_function, T_FIXNUM); | |
hash_ring_t * ring; | |
- hash_ring_t * new_ring; | |
+ Data_Get_Struct(self, hash_ring_t, ring); | |
+ | |
+ /* If this method is called twice, free the old ring. */ | |
+ if (ring) | |
+ hash_ring_free(ring); | |
int nr_int = FIX2INT(num_replicas); | |
int hf_int = FIX2INT(hash_function); | |
- Data_Get_Struct(self, hash_ring_t, ring); | |
- | |
- new_ring = hash_ring_create(nr_int, hf_int); | |
- | |
- MEMCPY(ring, new_ring, hash_ring_t, 1); | |
+ ring = hash_ring_create(nr_int, hf_int); | |
- hash_ring_free(new_ring); | |
+ DATA_PTR(self) = ring; | |
return self; | |
} | |
@@ -43,6 +46,7 @@ static VALUE method_add_node(VALUE self, VALUE name) | |
hash_ring_t *ring; | |
Data_Get_Struct(self, hash_ring_t, ring); | |
+ CHECK_RING(ring); | |
char * cname = StringValuePtr(name); | |
hash_ring_add_node(ring, cname, strlen(cname)); | |
@@ -59,6 +63,7 @@ static VALUE method_remove_node(VALUE self, VALUE name) | |
char * cname = StringValuePtr(name); | |
Data_Get_Struct(self, hash_ring_t, ring); | |
+ CHECK_RING(ring); | |
hash_ring_remove_node(ring, cname, strlen(cname)); | |
@@ -69,6 +74,7 @@ static VALUE method_print_ring(VALUE self) | |
{ | |
hash_ring_t * ring; | |
Data_Get_Struct(self, hash_ring_t, ring); | |
+ CHECK_RING(ring); | |
hash_ring_print(ring); | |
return self; | |
@@ -81,6 +87,7 @@ static VALUE method_find_node(VALUE self, VALUE key){ | |
hash_ring_t * ring; | |
Data_Get_Struct(self, hash_ring_t, ring); | |
+ CHECK_RING(ring); | |
hash_ring_node_t *node = hash_ring_find_node(ring, ckey, strlen(ckey)); | |
-- | |
1.7.6 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment