commit 92b42258145346f8841d56f0a4510207a49f6e11
parent 40c93e66a2a29dd3d3e737101640a8014e53d836
Author: Joris Vink <joris@coders.se>
Date: Wed, 30 Aug 2017 11:50:02 +0200
add memory tag example
Diffstat:
4 files changed, 118 insertions(+), 0 deletions(-)
diff --git a/examples/memtag/.gitignore b/examples/memtag/.gitignore
@@ -0,0 +1,6 @@
+*.o
+.flavor
+.objs
+memtag.so
+assets.h
+cert
diff --git a/examples/memtag/conf/build.conf b/examples/memtag/conf/build.conf
@@ -0,0 +1,34 @@
+# memtag build config
+# You can switch flavors using: kodev flavor [newflavor]
+
+# Set to yes if you wish to produce a single binary instead
+# of a dynamic library. If you set this to yes you must also
+# set kore_source together with kore_flavor.
+#single_binary=no
+#kore_source=/home/joris/src/kore
+#kore_flavor=
+
+# The flags below are shared between flavors
+cflags=-Wall -Wmissing-declarations -Wshadow
+cflags=-Wstrict-prototypes -Wmissing-prototypes
+cflags=-Wpointer-arith -Wcast-qual -Wsign-compare
+
+cxxflags=-Wall -Wmissing-declarations -Wshadow
+cxxflags=-Wpointer-arith -Wcast-qual -Wsign-compare
+
+# Mime types for assets served via the builtin asset_serve_*
+#mime_add=txt:text/plain; charset=utf-8
+#mime_add=png:image/png
+#mime_add=html:text/html; charset=utf-8
+
+dev {
+ # These flags are added to the shared ones when
+ # you build the "dev" flavor.
+ cflags=-g
+ cxxflags=-g
+}
+
+#prod {
+# You can specify additional flags here which are only
+# included if you build with the "prod" flavor.
+#}
diff --git a/examples/memtag/conf/memtag.conf b/examples/memtag/conf/memtag.conf
@@ -0,0 +1,13 @@
+# memtag configuration
+
+bind 127.0.0.1 8888
+load ./memtag.so init
+
+tls_dhparam dh2048.pem
+
+domain * {
+ certfile cert/server.pem
+ certkey cert/key.pem
+
+ static / page
+}
diff --git a/examples/memtag/src/memtag.c b/examples/memtag/src/memtag.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2017 Joris Vink <joris@coders.se>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <kore/kore.h>
+#include <kore/http.h>
+
+/*
+ * This example demonstrates how dynamically reloadable modules
+ * can use the memory tagging system in Kore in order to restore
+ * the global pointers in the module.
+ */
+
+/* Some unique value. */
+#define MEM_TAG_HELLO 100
+
+int init(int);
+int page(struct http_request *);
+
+/* Global pointer, gets initialized to NULL when module loads/reloads. */
+char *fixed_ptr = NULL;
+
+int
+init(int state)
+{
+ /* Ignore unload(s). */
+ if (state == KORE_MODULE_UNLOAD)
+ return (KORE_RESULT_OK);
+
+ printf("fixed_ptr: %p\n", (void *)fixed_ptr);
+
+ /* Attempt to lookup the original pointer. */
+ if ((fixed_ptr = kore_mem_lookup(MEM_TAG_HELLO)) == NULL) {
+ /* Failed, grab a new chunk of memory and tag it. */
+ printf(" allocating fixed_ptr for the first time\n");
+ fixed_ptr = kore_malloc_tagged(6, MEM_TAG_HELLO);
+ kore_strlcpy(fixed_ptr, "hello", 6);
+ } else {
+ printf(" fixed_ptr address resolved\n");
+ }
+
+ printf(" fixed_ptr: %p\n", (void *)fixed_ptr);
+ printf(" value : %s\n", fixed_ptr);
+
+ return (KORE_RESULT_OK);
+}
+
+int
+page(struct http_request *req)
+{
+ http_response(req, 200, fixed_ptr, strlen(fixed_ptr));
+ return (KORE_RESULT_OK);
+}