New examples directory, with two example programs.

This commit is contained in:
Sears Russell 2007-05-25 18:21:09 +00:00
parent 91cbaa607d
commit 9eb191d852
5 changed files with 75 additions and 1 deletions

View file

@ -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

View file

@ -232,5 +232,6 @@ AC_CONFIG_FILES([Makefile
test/monotree/Makefile
test/pobj/Makefile
utilities/Makefile
examples/Makefile
])
AC_OUTPUT

4
examples/Makefile.am Normal file
View file

@ -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}

23
examples/ex1.c Normal file
View file

@ -0,0 +1,23 @@
#include <lladd/transactional.h>
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();
}

46
examples/ex2.c Normal file
View file

@ -0,0 +1,46 @@
#include <lladd/transactional.h>
#include <stdio.h>
#include <assert.h>
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();
}