Browse Source

Use size_t handles.

woollybah 9 years ago
parent
commit
00a346af41
3 changed files with 68 additions and 6 deletions
  1. 2 2
      blitz.mod/blitz.bmx
  2. 63 1
      blitz.mod/blitz_handle.c
  3. 3 3
      blitz.mod/blitz_handle.h

+ 2 - 2
blitz.mod/blitz.bmx

@@ -444,13 +444,13 @@ about:
 After converting an object to an integer handle, you must later
 After converting an object to an integer handle, you must later
 release it using the #Release command.
 release it using the #Release command.
 End Rem
 End Rem
-Function HandleFromObject:Int( obj:Object )="bbHandleFromObject"
+Function HandleFromObject:size_t( obj:Object )="bbHandleFromObject"
 
 
 Rem
 Rem
 bbdoc: Convert integer handle to object
 bbdoc: Convert integer handle to object
 returns: The object associated with the integer handle
 returns: The object associated with the integer handle
 End Rem
 End Rem
-Function HandleToObject:Object( handle:Int )="bbHandleToObject"
+Function HandleToObject:Object( handle:size_t )="bbHandleToObject"
 
 
 End Extern
 End Extern
 
 

+ 63 - 1
blitz.mod/blitz_handle.c

@@ -1,6 +1,7 @@
 
 
 #include "blitz.h"
 #include "blitz.h"
 
 
+/*
 #define HASH_SIZE 1024
 #define HASH_SIZE 1024
 #define HASH_SLOT(X) (((X)/8)&(HASH_SIZE-1))	// divide-by-8 for better void* mapping.
 #define HASH_SLOT(X) (((X)/8)&(HASH_SIZE-1))	// divide-by-8 for better void* mapping.
 
 
@@ -61,9 +62,70 @@ BBObject *bbHandleToObject( int handle ){
 	return o ? o : &bbNullObject;
 	return o ? o : &bbNullObject;
 }
 }
 
 
-void bbHandleRelease( int handle ){
+void bbHandleRelease( int  handle ){
 	BBObject *o=(BBObject*)hashRemove( handle_hash,handle*8 );
 	BBObject *o=(BBObject*)hashRemove( handle_hash,handle*8 );
 	if( !o ) return;
 	if( !o ) return;
 	hashRemove( object_hash,(int)o );
 	hashRemove( object_hash,(int)o );
 	BBRELEASE( o );
 	BBRELEASE( o );
 }
 }
+
+*/
+
+struct handle_node {
+	struct avl_root link;
+	BBOBJECT obj;
+};
+
+static struct avl_root *handle_root = 0;
+
+#define generic_compare(x, y) (((x) > (y)) - ((x) < (y)))
+
+static int node_compare(const void *x, const void *y) {
+
+        struct handle_node * node_x = (struct handle_node *)x;
+        struct handle_node * node_y = (struct handle_node *)y;
+
+        return generic_compare(node_x->obj, node_y->obj);
+}
+
+size_t bbHandleFromObject( BBObject *o ) {
+	struct handle_node * node = (struct handle_node *)malloc(sizeof(struct handle_node));
+	node->obj = o;
+	
+	struct handle_node * old_node = (struct handle_node *)avl_map(&node->link, node_compare, &handle_root );
+
+	if (&node->link != &old_node->link) {
+		// delete the new node, since we don't need it
+		free(node);
+	} else {
+		BBRETAIN(o);
+	}
+	
+	return (size_t)o;
+}
+
+BBObject *bbHandleToObject( size_t handle ) {
+	struct handle_node node;
+	node.obj = (BBOBJECT)handle;
+	
+	struct handle_node * found = (struct handle_node *)tree_search(&node, node_compare, handle_root );
+
+	if (found) {
+		return (BBOBJECT)handle;
+	}
+	
+	return &bbNullObject;
+}
+
+void bbHandleRelease( size_t handle ) {
+	struct handle_node node;
+	node.obj = (BBOBJECT)handle;
+	
+	struct handle_node * found = (struct handle_node *)tree_search(&node, node_compare, handle_root);
+	
+	if (found) {
+		BBRELEASE(found->obj);
+		avl_del(&found->link, &handle_root);
+		free(found);
+	}
+}

+ 3 - 3
blitz.mod/blitz_handle.h

@@ -8,9 +8,9 @@
 extern "C"{
 extern "C"{
 #endif
 #endif
 
 
-int			bbHandleFromObject( BBObject *o );
-BBObject*   bbHandleToObject( int handle );
-void		bbHandleRelease( int handle );
+size_t		bbHandleFromObject( BBObject *o );
+BBObject*   bbHandleToObject( size_t handle );
+void		bbHandleRelease( size_t handle );
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }