RPA Toolkit
adding hash and list functionality to rlib
authormstoilov <mstoilov@b0bb84a5-424d-4f98-af44-3ef3f117eb03>
Thu, 19 Aug 2010 06:49:57 +0000 (23:49 -0700)
committermstoilov <mstoilov@b0bb84a5-424d-4f98-af44-3ef3f117eb03>
Thu, 19 Aug 2010 06:49:57 +0000 (23:49 -0700)
git-svn-id: svn+ssh://svn.crossrain.com/svn/rpase/trunk/rtk@159 b0bb84a5-424d-4f98-af44-3ef3f117eb03

arch/linux/x86_64/rtypes.h
rlib/build/linux/rlib.mk
rlib/rarray.c
rlib/rhash.c [new file with mode: 0644]
rlib/rhash.h [new file with mode: 0644]
rlib/rlist.c [new file with mode: 0644]
rlib/rlist.h [new file with mode: 0644]
rvm/rvm.c
rvm/rvm.h

index f6dd730..ba6c82e 100644 (file)
@@ -18,7 +18,6 @@ typedef rint rboolean;
 typedef void *rpointer;
 typedef const void *rconstpointer;
 
-
 /* 
  * Architecture dependent types. These types have to be redifined 
  * for every architecture
index ec28b0d..f9b48db 100644 (file)
@@ -7,6 +7,8 @@ RLIB_OBJECTS =  \
        $(OUTDIR)/ratomic.o \
        $(OUTDIR)/rspinlock.o \
        $(OUTDIR)/rarray.o \
+       $(OUTDIR)/rlist.o \
+       $(OUTDIR)/rhash.o \
        $(OUTDIR)/rstring.o \
 
 
index 590f0f5..7b3e7e2 100644 (file)
@@ -52,7 +52,12 @@ rarray_t *r_array_create(ruint elt_size)
        rarray_t *array;
        if ((array = (rarray_t*)r_malloc(sizeof(*array))) == NULL)
                return NULL;
-       return r_array_init(array, elt_size);
+       r_memset(array, 0, sizeof(*array));
+       if (!r_array_init(array, elt_size)) {
+               r_array_destroy(array);
+               return NULL;
+       }
+       return array;
 }
 
 
diff --git a/rlib/rhash.c b/rlib/rhash.c
new file mode 100644 (file)
index 0000000..c29c63f
--- /dev/null
@@ -0,0 +1,41 @@
+#include "rmem.h"
+#include "rhash.h"
+
+
+
+rhash_t *r_hash_create(ruint nbits, r_hash_equalfunc eqfunc, r_hash_hashfun hfunc)
+{
+       rhash_t *hash;
+
+       hash = (rhash_t*)r_malloc(sizeof(*hash));
+       if (!hash)
+               return NULL;
+       r_memset(hash, 0, sizeof(*hash));
+       if (!r_hash_init(hash, nbits, eqfunc, hfunc)) {
+               r_hash_destroy(hash);
+               return NULL;
+       }
+       return hash;
+}
+
+
+rhash_t *r_hash_init(rhash_t *hash, ruint nbits, r_hash_equalfunc eqfunc, r_hash_hashfun hfunc)
+{
+       hash->nbits = nbits;
+       hash->eqfunc = eqfunc;
+       hash->hfunc = hfunc;
+       hash->buckets = (rlist_t*)r_malloc(sizeof(rlist_t) * r_hash_size(hash));
+       return NULL;
+}
+
+
+void r_hash_destroy(rhash_t *hash)
+{
+       r_free(hash);
+}
+
+
+void r_hash_cleanup(rhash_t *hash)
+{
+       r_free(hash->buckets);
+}
diff --git a/rlib/rhash.h b/rlib/rhash.h
new file mode 100644 (file)
index 0000000..36900f0
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef _RHASH_H_
+#define _RHASH_H_
+
+#include "rtypes.h"
+#include "rlist.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+typedef struct rhash_s rhash_t;
+typedef rboolean (*r_hash_equalfunc)(rconstpointer p1, rconstpointer p2);
+typedef ruint (*r_hash_hashfun)(rconstpointer key);
+
+struct rhash_s {
+       rlist_t *buckets;
+       ruint nbits;
+       r_hash_equalfunc eqfunc;
+       r_hash_hashfun hfunc;
+};
+
+
+#define r_hash_size(__h__) (1 << (__h__)->nbits)
+#define r_hash_mask(__h__) (R_HASH_SIZE(__h__) - 1)
+rhash_t *r_hash_create(ruint nbits, r_hash_equalfunc eqfunc, r_hash_hashfun hfunc);
+rhash_t *r_hash_init(rhash_t *hash, ruint nbits, r_hash_equalfunc eqfunc, r_hash_hashfun hfunc);
+void r_hash_destroy(rhash_t *hash);
+void r_hash_cleanup(rhash_t *hash);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/rlib/rlist.c b/rlib/rlist.c
new file mode 100644 (file)
index 0000000..b7215ef
--- /dev/null
@@ -0,0 +1,93 @@
+#include "rlist.h"
+
+
+void rpa_list_init(rlist_t *ptr)
+{
+       ptr->next = ptr;
+       ptr->prev = ptr;
+}
+
+
+void rpa_list_add(rlist_t *pNew, rlist_t *pPrev, rlist_t *pNext)
+{
+       pNext->prev = pNew;
+       pNew->next = pNext;
+       pNew->prev = pPrev;
+       pPrev->next = pNew;
+}
+
+
+void rpa_list_addh(rlist_t *pHead, rlist_t *pNew)
+{
+       rpa_list_add(pNew, pHead, (pHead)->next);
+}
+
+
+void rpa_list_addt(rlist_t *pHead, rlist_t *pNew)
+{
+       rpa_list_add(pNew, (pHead)->prev, pHead);
+}
+
+
+void rpa_list_unlink(rlist_t *pPrev, rlist_t *pNext)
+{
+       pNext->prev = pPrev;
+       pPrev->next = pNext;
+}
+
+
+void rpa_list_del(rlist_t *pEntry)
+{
+       rpa_list_unlink((pEntry)->prev, (pEntry)->next);
+}
+
+
+rlist_t *rpa_list_first(rlist_t *pHead)
+{
+       return (((pHead)->next != (pHead)) ? (pHead)->next: ((void*)0));
+}
+
+
+rlist_t *rpa_list_last(rlist_t *pHead)
+{
+       return (((pHead)->prev != (pHead)) ? (pHead)->prev: ((void*)0));
+}
+
+
+rlist_t *rpa_list_prev(rlist_t *pHead, rlist_t *pElem)
+{
+       return (pElem && pElem->prev != pHead) ? (pElem)->prev: ((void*)0);
+}
+
+
+rlist_t *rpa_list_next(rlist_t *pHead, rlist_t *pElem)
+{
+       return (pElem && pElem->next != pHead) ? pElem->next: ((void*)0);
+}
+
+
+void rpa_list_splice(rlist_t *pList, rlist_t *pHead)
+{
+       rlist_t *pFirst;
+
+       pFirst = pList->next;
+       if (pFirst != pList) {
+               rlist_t *pLast = pList->prev;
+               rlist_t *pAt = pHead->next;
+               pFirst->prev = pHead;
+               pHead->next = pFirst;
+               pLast->next = pAt;
+               pAt->prev = pLast;
+       }
+}
+
+
+int rpa_list_count(rlist_t *pHead)
+{
+       rlist_t *pCur;
+       int n = 0;
+
+       r_list_foreach(pCur, pHead)
+               n++;
+       return n;
+}
diff --git a/rlib/rlist.h b/rlib/rlist.h
new file mode 100644 (file)
index 0000000..6b0139d
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef _RLIST_H_
+#define _RLIST_H_
+
+#include "rtypes.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+typedef struct rlist_s rlist_t, rlink_t, rhead_t;
+
+#define R_LIST_HEAD(__name__) {&(__name__), &(__name__) }
+#define r_list(__name__) t __name__ = { &(__name__), &(__name__) }
+#define r_list_empty(__head__) ((__head__)->next == ((void*)0) || (__head__)->next == __head__)
+#define r_list_entry(_ptr, _type, __member__) ((_type *)((char *)(_ptr)-(ruint)(&((_type *)0)->__member__)))
+#define r_list_foreach(__pos__, __head__) for (__pos__ = (__head__)->next; __pos__ != (__head__); __pos__ = (__pos__)->next)
+#define r_list_foreachreverse(__pos__, __head__) for (__pos__ = (__head__)->prev; __pos__ != (__head__); __pos__ = (__pos__)->prev)
+#define r_list_fo_eachsafe(__pos__, __n__, __head__) \
+                 for (__pos__ = (__head__)->next, __n__ = __pos__->next; __pos__ != (__head__); __pos__ = __n__, __n__ = __pos__->next)
+
+struct rlist_s {
+       rlist_t *next;
+       rlist_t *prev;
+};
+
+
+void r_list_init(rlist_t *ptr);
+void r_list_check(rlist_t *ptr);
+void r_list_add(rlist_t *pNew, rlist_t *pPrev, rlist_t *pNext);
+void r_list_addh(rlist_t *pHead, rlist_t *pNew);
+void r_list_addt(rlist_t *pHead, rlist_t *pNew);
+void r_list_unlink(rlist_t *pPrev, rlist_t *pNext);
+void r_list_del(rlist_t *pEntry);
+rlist_t *r_list_first(rlist_t *pHead);
+rlist_t *r_list_last(rlist_t *pHead);
+rlist_t *r_list_prev(rlist_t *pHead, rlist_t *pElem);
+rlist_t *r_list_next(rlist_t *pHead, rlist_t *pElem);
+void r_list_splice(rlist_t *pList, rlist_t *pHead);
+int r_list_count(rlist_t *pHead);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
index e2c0909..2bae92c 100644 (file)
--- a/rvm/rvm.c
+++ b/rvm/rvm.c
@@ -762,10 +762,10 @@ static void rvm_cpu_dumpregs(rvm_asmins_t *pi, rvm_cpu_t *vm)
        buffer[50] = '\0';
        rvm_printf("%s", buffer);
 
-       rvm_printf("0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, SP=0x%lx, LR=%ld, PC=%ld, S( %c%c%c%c )",
+       rvm_printf("0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, 0x%lx, SP=%ld, LR=%ld, PC=%ld, S( %c%c%c%c )",
                RVM_GET_REGU(vm, 0), RVM_GET_REGU(vm, 1), RVM_GET_REGU(vm, 2), RVM_GET_REGU(vm, 3),
                RVM_GET_REGU(vm, 4), RVM_GET_REGU(vm, 5), RVM_GET_REGU(vm, 6), RVM_GET_REGU(vm, 7),
-               RVM_GET_REGU(vm, 8), RVM_GET_REGU(vm, SP), RVM_GET_REGU(vm, LR), (long int)RVM_GET_REGU(vm, PC),
+               RVM_GET_REGU(vm, 8), (long int)RVM_GET_REGU(vm, SP), (long int)RVM_GET_REGU(vm, LR), (long int)RVM_GET_REGU(vm, PC),
                vm->status & RVM_STATUS_V ? 'V' : '_',
                vm->status & RVM_STATUS_C ? 'C' : '_',
                vm->status & RVM_STATUS_N ? 'N' : '_',
index 1750eba..90e0691 100644 (file)
--- a/rvm/rvm.h
+++ b/rvm/rvm.h
@@ -87,14 +87,14 @@ enum {
 };
 
 
-enum {
-       RVM_DTYPE_WORD = 0,
-       RVM_DTYPE_POINTER,
-       RVM_DTYPE_DOUBLE,
-       RVM_DTYPE_STRING,
-       RVM_DTYPE_STRINGPTR,
-       RVM_DTYPE_RELOCPTR,
-};
+#define RVM_DTYPE_WORD 0
+#define RVM_DTYPE_POINTER 1
+#define RVM_DTYPE_DOUBLE 2
+#define RVM_DTYPE_STRING 3
+#define RVM_DTYPE_STRINGPTR 4
+#define RVM_DTYPE_RELOCPTR 5
+#define RVM_DTYPE_USER 64
+#define RVM_DTYPE_USERDEF(__n__) (RVM_DTYPE_USER + (__n__))
 
 
 #define RVM_REGISTER_BITS (8 * sizeof(rword))