memtag.c (1986B)
1 /*
2 * Copyright (c) 2017-2018 Joris Vink <joris@coders.se>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <kore/kore.h>
18 #include <kore/http.h>
19
20 /*
21 * This example demonstrates how dynamically reloadable modules
22 * can use the memory tagging system in Kore in order to restore
23 * the global pointers in the module.
24 */
25
26 /* Some unique value. */
27 #define MEM_TAG_HELLO 100
28
29 int init(int);
30 int page(struct http_request *);
31
32 /* Global pointer, gets initialized to NULL when module loads/reloads. */
33 char *fixed_ptr = NULL;
34
35 int
36 init(int state)
37 {
38 /* Ignore unload(s). */
39 if (state == KORE_MODULE_UNLOAD)
40 return (KORE_RESULT_OK);
41
42 printf("fixed_ptr: %p\n", (void *)fixed_ptr);
43
44 /* Attempt to lookup the original pointer. */
45 if ((fixed_ptr = kore_mem_lookup(MEM_TAG_HELLO)) == NULL) {
46 /* Failed, grab a new chunk of memory and tag it. */
47 printf(" allocating fixed_ptr for the first time\n");
48 fixed_ptr = kore_malloc_tagged(6, MEM_TAG_HELLO);
49 kore_strlcpy(fixed_ptr, "hello", 6);
50 } else {
51 printf(" fixed_ptr address resolved\n");
52 }
53
54 printf(" fixed_ptr: %p\n", (void *)fixed_ptr);
55 printf(" value : %s\n", fixed_ptr);
56
57 return (KORE_RESULT_OK);
58 }
59
60 int
61 page(struct http_request *req)
62 {
63 http_response(req, 200, fixed_ptr, strlen(fixed_ptr));
64 return (KORE_RESULT_OK);
65 }