|
| 1 | +/* |
| 2 | + * Copyright 2019, Intel Corporation |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in |
| 13 | + * the documentation and/or other materials provided with the |
| 14 | + * distribution. |
| 15 | + * |
| 16 | + * * Neither the name of the copyright holder nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived |
| 18 | + * from this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +/* |
| 34 | + * hello.c -- demonstrate API for librpmem |
| 35 | + */ |
| 36 | + |
| 37 | +#include <assert.h> |
| 38 | +#include <errno.h> |
| 39 | +#include <unistd.h> |
| 40 | +#include <stdio.h> |
| 41 | +#include <stdlib.h> |
| 42 | +#include <string.h> |
| 43 | + |
| 44 | +#include <librpmem.h> |
| 45 | + |
| 46 | +/* |
| 47 | + * English and Spanish translation of the message |
| 48 | + */ |
| 49 | +enum lang_t {en, es}; |
| 50 | +static const char *hello_str[] = { |
| 51 | + [en] = "Hello world!", |
| 52 | + [es] = "¡Hola Mundo!" |
| 53 | +}; |
| 54 | + |
| 55 | +/* |
| 56 | + * structure to store the current message |
| 57 | + */ |
| 58 | +#define STR_SIZE 100 |
| 59 | +struct hello_t { |
| 60 | + enum lang_t lang; |
| 61 | + char str[STR_SIZE]; |
| 62 | +}; |
| 63 | + |
| 64 | +/* |
| 65 | + * write_hello_str -- write a message to the local memory |
| 66 | + */ |
| 67 | +static inline void |
| 68 | +write_hello_str(struct hello_t *hello, enum lang_t lang) |
| 69 | +{ |
| 70 | + hello->lang = lang; |
| 71 | + strncpy(hello->str, hello_str[hello->lang], STR_SIZE); |
| 72 | +} |
| 73 | + |
| 74 | +/* |
| 75 | + * remote_open -- setup the librpmem replication |
| 76 | + */ |
| 77 | +static inline RPMEMpool* |
| 78 | +remote_open(const char *target, const char *poolset, void *pool, |
| 79 | + size_t pool_size, int *created) |
| 80 | +{ |
| 81 | + /* fill pool_attributes */ |
| 82 | + struct rpmem_pool_attr pool_attr; |
| 83 | + memset(&pool_attr, 0, sizeof(pool_attr)); |
| 84 | + strncpy(pool_attr.signature, "HELLO", RPMEM_POOL_HDR_SIG_LEN); |
| 85 | + |
| 86 | + /* create a remote pool */ |
| 87 | + unsigned nlanes = 1; |
| 88 | + RPMEMpool *rpp = rpmem_create(target, poolset, pool, pool_size, &nlanes, |
| 89 | + &pool_attr); |
| 90 | + if (rpp) { |
| 91 | + *created = 1; |
| 92 | + return rpp; |
| 93 | + } |
| 94 | + |
| 95 | + /* create failed so open a remote pool */ |
| 96 | + assert(errno == EEXIST); |
| 97 | + rpp = rpmem_open(target, poolset, pool, pool_size, &nlanes, &pool_attr); |
| 98 | + assert(rpp != NULL); |
| 99 | + *created = 0; |
| 100 | + |
| 101 | + return rpp; |
| 102 | +} |
| 103 | + |
| 104 | +int |
| 105 | +main(int argc, char *argv[]) |
| 106 | +{ |
| 107 | + /* for this example, assume 32MiB pool */ |
| 108 | + size_t pool_size = 32 * 1024 * 1024; |
| 109 | + void *pool = NULL; |
| 110 | + int created; |
| 111 | + |
| 112 | + /* allocate a page size aligned local memory pool */ |
| 113 | + long pagesize = sysconf(_SC_PAGESIZE); |
| 114 | + assert(pagesize >= 0); |
| 115 | + int ret = posix_memalign(&pool, pagesize, pool_size); |
| 116 | + assert(ret == 0 && pool != NULL); |
| 117 | + |
| 118 | + /* skip to the beginning of the message */ |
| 119 | + size_t hello_off = 4096; /* rpmem header size */ |
| 120 | + struct hello_t *hello = (struct hello_t *)(pool + hello_off); |
| 121 | + |
| 122 | + RPMEMpool *rpp = remote_open("target", "pool.set", pool, pool_size, |
| 123 | + &created); |
| 124 | + if (created) { |
| 125 | + /* reset local memory pool */ |
| 126 | + memset(pool, 0, pool_size); |
| 127 | + write_hello_str(hello, en); |
| 128 | + } else { |
| 129 | + /* read message from the remote pool */ |
| 130 | + ret = rpmem_read(rpp, hello, hello_off, sizeof(*hello), 0); |
| 131 | + assert(ret == 0); |
| 132 | + |
| 133 | + /* translate the message */ |
| 134 | + const int lang_num = (sizeof(hello_str) / sizeof(hello_str[0])); |
| 135 | + enum lang_t lang = (enum lang_t)((hello->lang + 1) % lang_num); |
| 136 | + write_hello_str(hello, lang); |
| 137 | + } |
| 138 | + |
| 139 | + /* write message to the remote pool */ |
| 140 | + ret = rpmem_persist(rpp, hello_off, sizeof(*hello), 0, 0); |
| 141 | + printf("%s\n", hello->str); |
| 142 | + assert(ret == 0); |
| 143 | + |
| 144 | + /* close the remote pool */ |
| 145 | + ret = rpmem_close(rpp); |
| 146 | + assert(ret == 0); |
| 147 | + |
| 148 | + /* release local memory pool */ |
| 149 | + free(pool); |
| 150 | + return 0; |
| 151 | +} |
0 commit comments