Skip to content
Browse files

Modifies hklr API to Simplify Meaning

  • Loading branch information...
1 parent 9e02fc8 commit 60eaf7550a6653d17de4094d7344d680b9b9d9e6 @pyrated pyrated committed Sep 27, 2012
Showing with 109 additions and 26 deletions.
  1. +2 −1 .gitignore
  2. +1 −1 owl/expected/gc_test_2_free.result
  3. +1 −1 src/Makefile.am
  4. +6 −6 src/hklr.c
  5. +80 −0 src/hklr_common.c
  6. +12 −0 src/hklr_common.h
  7. +7 −17 src/test/gc.c
View
3 .gitignore
@@ -39,5 +39,6 @@ src/y.tab.output
# Temporaries that some text editors make
*~
-# Python Temps
+# Python generated files
__pycache__/
+*.pyc
View
2 owl/expected/gc_test_2_free.result
@@ -1,4 +1,4 @@
{
"stderr": "",
- "stdout": "Created: 2\nFreed: 2\nCycles: 0\nRuns: 1\n"
+ "stdout": "Created: 3\nFreed: 3\nCycles: 0\nRuns: 1\n"
}
View
2 src/Makefile.am
@@ -9,4 +9,4 @@ hkl_SOURCES = y.tab.y lex.yy.l hkl_string.c hkl_hash.c hkl_tree.c hklc.c \
hklr.c hkl_object.c
testbed_SOURCES = hkl_tree.c hkl_string.c hkl_test.c hklr.c hkl_object.c \
-hkl_hash.c test/hash.c test/gc.c
+hklr_common.c hkl_hash.c test/hash.c test/gc.c
View
12 src/hklr.c
@@ -70,9 +70,9 @@ static void hklr_gc_release(HklObject* object)
// If the object is a reference
case HKL_TYPE_REF:
hklr_gc_dec(object->ref);
- break;
+ break;
- default: assert(false); break;
+ default: break;
}
object->color = HKL_COLOR_BLACK;
@@ -126,7 +126,7 @@ static void hklr_gc_scanblack(HklObject* object)
hklr_gc_scanblack(object->ref);
break;
- default: assert(false); break;
+ default: break;
}
}
@@ -160,7 +160,7 @@ static void hklr_gc_markgray(HklObject* object)
hklr_gc_markgray(object->ref);
break;
- default: assert(false); break;
+ default: break;
}
}
}
@@ -226,7 +226,7 @@ static void hklr_gc_scan(HklObject* object)
hklr_gc_scan(object->ref);
break;
- default: assert(false); break;
+ default: break;
}
}
}
@@ -267,7 +267,7 @@ static void hklr_gc_collectwhite(HklObject* object)
hklr_gc_collectwhite(object->ref);
break;
- default: assert(false); break;
+ default: break;
}
HKLR.gc_freed++;
View
80 src/hklr_common.c
@@ -0,0 +1,80 @@
+#include <assert.h>
+
+#include "hklr_common.h"
+#include "hkl_hash.h"
+#include "hkl_string.h"
+
+HklObject* hklr_create(HklTypes type, HklFlags flags, void* value)
+{
+ assert(type != HKL_TYPE_NONE);
+
+ HklObject* object = hkl_object_new();
+ HKLR.gc_created++;
+ object->flags = flags;
+ object->type = type;
+ hklr_gc_inc(object);
+
+ switch (type)
+ {
+ case HKL_TYPE_HASH:
+ if (value != NULL)
+ {
+ object->hash = value;
+ }
+ else
+ {
+ object->hash = hkl_hash_new();
+ }
+ break;
+
+ case HKL_TYPE_REF:
+ if (value != NULL)
+ {
+ hklr_reference(object, value);
+ }
+ else
+ {
+ object->ref = NULL;
+ }
+ break;
+
+ case HKL_TYPE_STRING:
+ if (value != NULL)
+ {
+ object->string = value;
+ }
+ else
+ {
+ object->string = hkl_string_new();
+ }
+ break;
+
+ default: assert(false); break;
+ }
+
+ return object;
+}
+
+void hklr_reference(HklObject* object, HklObject* reference)
+{
+ assert(object != NULL);
+ assert(reference != NULL);
+
+ assert(object->type == HKL_TYPE_REF);
+
+ object->ref = reference;
+ hklr_gc_inc(reference);
+}
+
+void hklr_member_insert(HklObject* object, HklObject* key, HklObject* value)
+{
+ assert(object != NULL);
+ assert(key != NULL);
+ assert(value != NULL);
+
+ assert(object->type == HKL_TYPE_HASH);
+ assert(key->type == HKL_TYPE_STRING);
+
+ hkl_hash_insert(object->hash, key->string, value);
+ hklr_gc_inc(value);
+}
View
12 src/hklr_common.h
@@ -0,0 +1,12 @@
+#ifndef HKLR_OBJECT_MANAGER_H
+#define HKLR_OBJECT_MANAGER_H
+
+#include "hklr.h"
+
+HklObject* hklr_create(HklTypes type, HklFlags flags, void* value);
+
+void hklr_reference(HklObject* object, HklObject* reference);
+
+void hklr_member_insert(HklObject* object, HklObject* name, HklObject* value);
+
+#endif // HKLR_OBJECT_MANAGER_H
View
24 src/test/gc.c
@@ -1,37 +1,27 @@
#include <stdio.h>
-#include "../hklr.h"
+#include "../hklr_common.h"
void gctest(const char* argv[])
{
hklr_init();
- HklString* self = hkl_string_new_from_utf8("self");
+ // Create a string "self"
+ HklObject* self = hklr_create(HKL_TYPE_STRING, HKL_FLAG_NONE, hkl_string_new_from_utf8("self"));
// Create a hash table
- HklObject* object = hkl_object_new();
- HKLR.gc_created++;
- hklr_gc_inc(object);
-
- object->type = HKL_TYPE_HASH;
- object->hash = hkl_hash_new();
+ HklObject* object = hklr_create(HKL_TYPE_HASH, HKL_FLAG_NONE, NULL);
// Create a reference to the hash table
- HklObject* ref = hkl_object_new();
- HKLR.gc_created++;
- hklr_gc_inc(ref);
-
- ref->type = HKL_TYPE_REF;
- ref->ref = object;
- hklr_gc_inc(object);
+ HklObject* ref = hklr_create(HKL_TYPE_REF, HKL_FLAG_NONE, object);
// Insert the reference into the table
- hkl_hash_insert(object->hash, self, ref);
- hklr_gc_inc(ref);
+ hklr_member_insert(object, self, ref);
// End of stack. Free all locals
hklr_gc_dec(object);
hklr_gc_dec(ref);
+ hklr_gc_dec(self);
// A cycle exists. Comment out this line to see the difference.
hklr_gc_collect();

0 comments on commit 60eaf75

Please sign in to comment.
Something went wrong with that request. Please try again.