diff --git a/Makefile.am b/Makefile.am index a50fe31..189453e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ EXTRA_DIST = reconf -SUBDIRS = src test utilities benchmarks +SUBDIRS = src test utilities benchmarks examples export GLOBAL_CFLAGS = -g -Wall -pedantic -std=gnu99 -DPBL_COMPAT ## GOAL: Make these not warn! #-Wextra -Wno-unused-parameter -Winline diff --git a/configure.in b/configure.in index 56c7f85..d197925 100644 --- a/configure.in +++ b/configure.in @@ -232,5 +232,6 @@ AC_CONFIG_FILES([Makefile test/monotree/Makefile test/pobj/Makefile utilities/Makefile + examples/Makefile ]) AC_OUTPUT diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..cf314aa --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,4 @@ +LDADD=$(top_builddir)/src/lladd/liblladd.a \ + $(top_builddir)/src/libdfa/librw.a +bin_PROGRAMS=ex1 ex2 +AM_CFLAGS=${GLOBAL_CFLAGS} diff --git a/examples/ex1.c b/examples/ex1.c new file mode 100644 index 0000000..b26ae9f --- /dev/null +++ b/examples/ex1.c @@ -0,0 +1,23 @@ +#include + +int main (int argc, char ** argv) { + + Tinit(); + + int i = 42; + + int xid = Tbegin(); + recordid rid = Talloc(xid, sizeof(int)); + Tset(xid, rid, &i); // the application is responsible for memory management. + // Here, stack-allocated integers are used, although memory + // from malloc() works as well. + Tcommit(xid); + + int j; + + xid = Tbegin(); + Tread(xid, rid, &j); // j is now 42. + Tdealloc(xid, rid); + Tabort(xid); + Tdeinit(); +} diff --git a/examples/ex2.c b/examples/ex2.c new file mode 100644 index 0000000..24297d4 --- /dev/null +++ b/examples/ex2.c @@ -0,0 +1,46 @@ +#include +#include +#include + +int main (int argc, char ** argv) { + + Tinit(); + + recordid rootEntry; + + int xid = Tbegin(); + if(TrecordType(xid, ROOT_RECORD) == UNINITIALIZED_RECORD) { + + // ThashAlloc() will work here as well. + rootEntry = Talloc(xid, sizeof(int)); + + assert(ROOT_RECORD.page == rootEntry.page); + assert(ROOT_RECORD.slot == rootEntry.slot); + // newRoot.size will be sizeof(something) from above. + int zero = 0; + + + Tset(xid, rootEntry, &zero); + Tcommit(xid); + + printf("New store; root = 0\n"); + } else { + + // The store already is initialized. + + rootEntry = ROOT_RECORD; + rootEntry.size = sizeof(int); // Same as sizeof(something) above. + + // Perform any application initialization based upon its contents... + int root; + + Tread(xid, rootEntry, &root); + printf("Old store: %d -> ", root); + root++; + Tset(xid, rootEntry, &root); + printf("%d\n", root); + Tcommit(xid); + + } + Tdeinit(); +}