add memory tag example

This commit is contained in:
Joris Vink 2017-08-30 11:50:02 +02:00
parent 40c93e66a2
commit 92b4225814
4 changed files with 118 additions and 0 deletions

6
examples/memtag/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*.o
.flavor
.objs
memtag.so
assets.h
cert

View File

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

View File

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

View File

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