#include "dstm.h"
-obj_store_t *obj_begin;
-obj_listnode_t *obj_lnode_begin;
+obj_store_t *obj_begin; // points to the first location of the object store linked list
+obj_listnode_t **hash; // points to beginning of hash table
+unsigned int hash_size; // number of entries in hash table
extern int classsize[];
/* BEGIN - object store */
-
+// Initializes the pointers...currently invoked inside main()
void dstm_init(void) {
obj_begin = NULL;
- obj_lnode_begin = NULL;
+ hash = NULL;
+ hash_size = 0;
return;
}
-
+// Create a new object store of a given "size" as a linked list
void create_objstr(unsigned int size) {
obj_store_t *tmp, *ptr;
- static int id = 0;
+ static int id = 0; // keeps track of which object store is it when there are more than one object store
if ((tmp = (obj_store_t *) malloc(sizeof(obj_store_t))) < 0) {
printf("DSTM: Malloc error %s %d\n", __FILE__, __LINE__);
tmp->size = size;
tmp->top = tmp->base;
tmp->id = id++;
- if (obj_begin == NULL) { // First entry
- obj_begin = tmp;
- tmp->next = NULL;
- } else { // Insert to the front of the linked list
- tmp->next = obj_begin;
- obj_begin = tmp;
- }
+ tmp->next = obj_begin;
+ obj_begin = tmp;
return;
}
+// Delete the object store numbered "id"
void delete_objstr(int id) {
//TODO Implement this along with garbage collector
return;
/* END object store */
/* BEGIN object header */
+// Get a new object id
int get_newID(void) {
static int id = 0;
return ++id;
}
-
+// Insert the object header and object into the object store
int insertObject(obj_header_t h) {
- int left, req;
+ unsigned int left, req; // Keeps track of the space available in the current object store
+ obj_header_t *header;
left = obj_begin->size - (obj_begin->top - obj_begin->base);
req = getObjSize(h) + sizeof(obj_header_t);
if (req < left) {
memcpy(obj_begin->top, &h, sizeof(obj_header_t));
+ header = (obj_header_t *) obj_begin->top;
obj_begin->top = obj_begin->top + sizeof(obj_header_t) + getObjSize(h);
} else {
return -1;
}
//TODO Update obj_addr_table
+ addKey(h.oid, header);
+
return 0;
}
-
+// Get the size of the object for a given type
int getObjSize(obj_header_t h) {
return classsize[h.type];
}
-
+// Initial object when it is created at first
void createObject(unsigned short type) {
obj_header_t h;
h.version = 0;
h.rcount = 1;
h.status = CLEAN;
- insert_object(h);
+ insertObject(h);
}
/* END object header */
-/* BEGIN obj_listnode */
+/* BEGIN hash*/
+
+//hash table is an array of pointers that point to the beginning of a obj_listnode_t DS
+// "size" in hash table is the no of indices in the hash table and each index points to a pointer for an object_header
+void createHash(int size) {
+ int i;
-int IsEmpty(obj_listnode_t *node) {
- return obj_lnode_begin == NULL;
+ if ((hash = (obj_listnode_t **) malloc(sizeof(obj_listnode_t *)*size)) == NULL) {
+ printf("Malloc error %s %d\n", __FILE__, __LINE__);
+ exit(-1);
+ }
+ for (i = 0; i < size; i++) { // initialize the hash elements
+ hash[i] = NULL;
+ }
+ hash_size = size;
+ return;
+}
+
+// Hashing for the Key
+int hashkey(unsigned int oid) {
+ //TODO: Design a better hash function
+// return (hash_size % oid);
+ return (oid % hash_size);
}
-void insert_lnode(obj_listnode_t *node) {
- if(obj_lnode_begin == NULL) { // Enter "node" as the first node in linked list
- obj_lnode_begin = node;
- node->next = NULL;
- } else { // Enter "node" to the beginning of the linked list
- node->next = obj_lnode_begin;
- obj_lnode_begin = node ;
+//Add oid and its address to the new ob_listnode_t
+void addKey(unsigned int oid, obj_header_t *ptr) {
+ int index;
+ obj_listnode_t *node;
+ index = hashkey(oid);
+ if ((node = (obj_listnode_t *) malloc(sizeof(obj_listnode_t))) == NULL) {
+ printf("Malloc error %s %d\n", __FILE__, __LINE__);
+ exit(-1);
}
-
+ node->oid = oid;
+ node->object = ptr;
+ node->next = hash[index];
+ hash[index] = node;
+ return;
}
-
-int delete_lnode(obj_listnode_t *node, unsigned int oid) {
- obj_listnode_t pre, curr;
- if( obj_lnode_begin == NULL){
- printf(" No nodes to delete ");
- return -1;
- }else {
- //Traverse list
- pre = curr = obj_lnode_begin;
- while(node->next != NULL) {
- pre = curr;
- curr= curr->next;
- if(oid == node->oid){
+// Get the address of the object header for a given oid
+obj_header_t *findKey(unsigned int oid) {
+ int index;
+ obj_listnode_t *ptr;
+
+ index = hashkey(oid);
+ ptr = hash[index];
+ while(ptr != NULL) {
+ if (ptr->oid == oid) {
+ return ptr->object;
}
+ ptr = ptr->next;
}
+ return NULL;
+}
+// Remove the pointer to the object header from a linked list of obj_listnode_t given an oid
+int removeKey(unsigned int oid) {
+ int index;
+ obj_listnode_t *curr, *prev; // prev points to previous node and curr points to the node to be deleted
+
+ index = hashKey(oid);
+ prev = curr = hash[index];
+ for (; curr != NULL; curr = curr->next) {
+ if (curr->oid == oid) { // Find a match in the hash table
+ prev->next = curr->next;
+ if (hash[index] == curr) { // Special case when there is one element pointed by the hash table
+ hash[index] = NULL;
+ }
+ free(curr);
+ return 0;
+ }
+ prev = curr;
+ }
+ return -1;
}
-
-
-/* END obj_listnode */