From 58bed5becaea2ce81d482b8470dda35a772e84cd Mon Sep 17 00:00:00 2001 From: Gilad Arnold Date: Thu, 16 Dec 2004 23:04:21 +0000 Subject: [PATCH] Some changes to pobj.c to support operations for non-persistent (transient) objects. Enhancement to test-list program to work with transient objects as well. Minor fix to header file (typo). --- pobj/pobj.h | 2 +- src/pobj/pobj.c | 88 +++++++++++++++++++++---------------------- test/pobj/test-list.c | 41 ++++++++++++++------ 3 files changed, 72 insertions(+), 59 deletions(-) diff --git a/pobj/pobj.h b/pobj/pobj.h index 4851335..4647f8f 100644 --- a/pobj/pobj.h +++ b/pobj/pobj.h @@ -98,7 +98,7 @@ int pobj_set_ref (void *, void *, void *); int pobj_update_range (void *, void *, size_t); #define POBJ_UPDATE_FLD(obj,fld) \ pobj_update_range((obj), &((obj)->fld), sizeof((obj)->fld)) -#define pobj_UPDATE(obj) pobj_update_range ((obj), NULL, 0) +#define POBJ_UPDATE(obj) pobj_update_range ((obj), NULL, 0) int pobj_update_recursive (void *); int pobj_static_set_ref (void *, void *); diff --git a/src/pobj/pobj.c b/src/pobj/pobj.c index 181d16e..b281567 100644 --- a/src/pobj/pobj.c +++ b/src/pobj/pobj.c @@ -211,7 +211,7 @@ pobj_persistify (void *obj) size_t pobj_size; if (! g_is_init) - return; + return -1; if (p->rep_index >= 0) return 0; /* already persistent. */ @@ -669,15 +669,9 @@ pobj_ref_flag_update (void *obj, void **fld, int set) int bit; unsigned long *flags_ptr; unsigned long flags, mask; - int xid; + int xid = -1; int ret; - if (p->rep_index < 0) { - debug ("error: object is non-persistent"); - return -1; - } - pobj_slot = POBJ2REPSLOT (p); - /* Bound check. */ /* TODO: is this worthwhile? */ if ((char *) obj > (char *) fld @@ -691,11 +685,12 @@ pobj_ref_flag_update (void *obj, void **fld, int set) return -1; } - if ((xid = pobj_start ()) < 0) { + /* Open transaction context (persistent objects only). */ + if (p->rep_index >= 0 && (xid = pobj_start ()) < 0) { debug ("error: begin transaction failed, aborted"); return -1; } - + /* Update reference flags to allow reference adjustment during recovery. */ /* TODO: do we want to protect manipulation of object meta-data? Basically, * it should be protected, although changes are monotonic, but that will @@ -720,11 +715,15 @@ pobj_ref_flag_update (void *obj, void **fld, int set) *flags_ptr = flags & ~mask; } - /* Update corresponding record. */ - /* TODO: switch to set_range update. */ - Tset (xid, pobj_slot->rid, p); + /* Update corresponding record (persistent objects only). */ + if (p->rep_index >= 0) { + pobj_slot = POBJ2REPSLOT (p); + + /* TODO: switch to set_range update. */ + Tset (xid, pobj_slot->rid, p); - pobj_end (); + pobj_end (); + } return ret; } @@ -751,21 +750,16 @@ pobj_ref_typify (void *obj, int *reflist) int flags_offset; int bit; unsigned long *flags_ptr; - int xid; + int xid = -1; int count; - if (p->rep_index < 0) { - debug ("error: object is non-persistent"); - return -1; - } - pobj_slot = POBJ2REPSLOT (p); - if (! reflist) { debug ("error: null reference list provided, aborted"); return -1; } - if ((xid = pobj_start ()) < 0) { + /* Open transaction context (persistent objects only). */ + if (p->rep_index >= 0 && (xid = pobj_start ()) < 0) { debug ("error: begin transaction failed, aborted"); return -1; } @@ -803,11 +797,15 @@ pobj_ref_typify (void *obj, int *reflist) debug ("...done (%d total)", count); - /* Update corresponding record. */ - /* TODO: switch to set_range update. */ - Tset (xid, pobj_slot->rid, p); + /* Update corresponding record (persistent objects only). */ + if (p->rep_index >= 0) { + pobj_slot = POBJ2REPSLOT (p); - pobj_end (); + /* TODO: switch to set_range update. */ + Tset (xid, pobj_slot->rid, p); + + pobj_end (); + } return 0; } @@ -822,13 +820,7 @@ pobj_memcpy_memset_typed (void *obj, void *fld, void *data, int c, size_t len, #ifdef HAVE_IMPLICIT_TYPES int is_changed = 0; #endif /* HAVE_IMPLICIT_TYPES */ - int xid; - - if (p->rep_index < 0) { - debug ("error: object is non-persistent"); - return -1; - } - pobj_slot = POBJ2REPSLOT (p); + int xid = -1; /* Safety check. */ /* TODO: is this worthwhile? */ @@ -839,6 +831,12 @@ pobj_memcpy_memset_typed (void *obj, void *fld, void *data, int c, size_t len, return -1; } + /* Open transaction context (persistent objects only). */ + if (p->rep_index >= 0 && (xid = pobj_start ()) < 0) { + debug ("error: begin transaction failed, aborted"); + return -1; + } + #ifdef HAVE_IMPLICIT_TYPES if (is_typed) { switch (pobj_ref_type_enforce (obj, fld, len, is_ref)) { @@ -857,22 +855,21 @@ pobj_memcpy_memset_typed (void *obj, void *fld, void *data, int c, size_t len, } #endif /* HAVE_IMPLICIT_TYPES */ - if ((xid = pobj_start ()) < 0) { - debug ("error: begin transaction failed, aborted"); - return -1; - } - /* Update memory object field. */ if (is_copy) memcpy (fld, data, len); else memset (fld, c, len); - /* Update corresponding record. */ - /* TODO: switch to set_range update; not the is_changed field! */ - Tset (xid, pobj_slot->rid, p); + /* Update corresponding record (persistent objects only). */ + if (p->rep_index >= 0) { + pobj_slot = POBJ2REPSLOT (p); - pobj_end (); + /* TODO: switch to set_range update; note the is_changed field! */ + Tset (xid, pobj_slot->rid, p); + + pobj_end (); + } return 0; } @@ -932,10 +929,9 @@ pobj_update_range (void *obj, void *fld, size_t len) struct pobj_rep_list_item *pobj_slot; int xid; - if (p->rep_index < 0) { - debug ("error: object is non-persistent"); - return -1; - } + if (p->rep_index < 0) + return 0; /* transient mode. */ + pobj_slot = POBJ2REPSLOT (p); /* Safety check (only if len is non-zero). */ diff --git a/test/pobj/test-list.c b/test/pobj/test-list.c index a10a0a0..0ef7983 100644 --- a/test/pobj/test-list.c +++ b/test/pobj/test-list.c @@ -3,12 +3,18 @@ #include +struct data { + int val; +}; + struct item { int val; + struct data *data; struct item *next; int dummy[45]; }; int item_ref_fields[] = { + member_offset(struct item, data), member_offset(struct item, next), -1 }; @@ -17,6 +23,7 @@ int main (int argc, char **argv) { static struct item *list = NULL; + struct data *data; struct item *tmp, *next; int i; @@ -27,29 +34,38 @@ main (int argc, char **argv) next = NULL; for (i = 0; i < 15000; i++) { pobj_start (); + tmp = (struct item *) pobj_malloc (sizeof (struct item)); - if (! tmp) { + data = (struct data *) pobj_malloc_transient (sizeof (struct data)); + if (! (tmp && data)) { printf ("allocation error\n"); abort (); } - /* pobj_ref_typify (tmp, item_ref_fields); */ + pobj_ref_typify (tmp, item_ref_fields); -#if 0 - tmp->val = i; - tmp->next = next; -#endif pobj_static_set_ref (&list, tmp); - POBJ_SET_INT (tmp, val, i); - /* Intended crash code... */ + + data->val = -i; + + tmp->val = i; + tmp->data = data; + tmp->next = next; + #if 0 + POBJ_SET_INT (tmp, val, i); + + /* Intended crash code... */ if (i == 7) abort (); -#endif + POBJ_SET_REF (tmp, next, next); +#endif + + POBJ_UPDATE (tmp); + pobj_end (); - /* pobj_update (tmp); */ next = tmp; } /* pobj_update_recursive (tmp); */ @@ -73,8 +89,9 @@ main (int argc, char **argv) /* Print list. */ printf ("printing list...\n"); for (tmp = list; tmp; tmp = tmp->next) - printf ("%p: val=%d next=%p\n", - (void *) tmp, tmp->val, (void *) tmp->next); + printf ("%p: val=%d next=%p data=%p data->val=%d\n", + (void *) tmp, tmp->val, (void *) tmp->next, + (void *) tmp->data, (tmp->data ? tmp->data->val : 0)); printf ("...done\n"); pobj_shutdown ();