EM-ODP  3.7.0
Event Machine on ODP
env_sharedmem.c
1 /*
2  * Copyright (c) 2017, Nokia Solutions and Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <odp_api.h>
32 #include <event_machine.h>
34 
35 /**
36  * Shared memory header
37  */
38 typedef struct {
39  odp_shm_t odp_shm;
41 
42 /**
43  * Shared memory buffer incl. header
44  */
45 typedef struct {
46  env_shm_hdr_t hdr;
47  /**
48  * Actual data buffer returned to user, needs to be aligned to cache
49  * line, even if wasting a bit of memory in the hdr, to ensure that
50  * the user data is properly aligned.
51  */
52  uint8_t data[] ENV_CACHE_LINE_ALIGNED;
54 
55 ODP_STATIC_ASSERT(offsetof(env_shm_buf_t, data) == ENV_CACHE_LINE_SIZE,
56  "ENV_SHM_ALIGNMENT_ERROR");
57 
58 static void *
59 shared_reserve(const char *name, size_t size)
60 {
61  env_shm_buf_t *env_shm_buf;
62  odp_shm_t shm;
63  uint32_t flags = 0;
64 
65  odp_shm_capability_t shm_capa;
66  int ret = odp_shm_capability(&shm_capa);
67 
68  if (unlikely(ret))
69  return NULL;
70 
71  if (shm_capa.flags & ODP_SHM_SINGLE_VA)
72  flags |= ODP_SHM_SINGLE_VA;
73 
74  shm = odp_shm_reserve(name, offsetof(env_shm_buf_t, data) + size,
75  ODP_CACHE_LINE_SIZE, flags);
76 
77  if (unlikely(shm == ODP_SHM_INVALID))
78  return NULL;
79 
80  env_shm_buf = odp_shm_addr(shm);
81  env_shm_buf->hdr.odp_shm = shm;
82 
83  return (void *)env_shm_buf->data;
84 }
85 
86 void *
87 env_shared_reserve(const char *name, size_t size)
88 {
89  if (unlikely(name == NULL || size == 0))
90  return NULL;
91 
92  return shared_reserve(name, size);
93 }
94 
95 void *
96 env_shared_lookup(const char *name)
97 {
98  env_shm_buf_t *env_shm_buf;
99  odp_shm_t shm;
100 
101  if (unlikely(name == NULL))
102  return NULL;
103 
104  shm = odp_shm_lookup(name);
105  if (unlikely(shm == ODP_SHM_INVALID))
106  return NULL;
107 
108  env_shm_buf = odp_shm_addr(shm);
109 
110  return (void *)env_shm_buf->data;
111 }
112 
113 void *
114 env_shared_malloc(size_t size)
115 {
116  if (unlikely(size == 0))
117  return NULL;
118 
119  return shared_reserve(NULL, size);
120 }
121 
122 void
123 env_shared_free(void *buf)
124 {
125  const env_shm_buf_t *env_shm_buf;
126  odp_shm_t shm;
127  int ret;
128 
129  if (buf == NULL)
130  return;
131 
132  env_shm_buf = (env_shm_buf_t *)((uintptr_t)buf -
133  offsetof(env_shm_buf_t, data));
134 
135  shm = env_shm_buf->hdr.odp_shm;
136  ret = odp_shm_free(shm);
137  if (unlikely(ret != 0))
138  env_panic("");
139 }
ENV_CACHE_LINE_SIZE
#define ENV_CACHE_LINE_SIZE
Definition: environment.h:62
event_machine.h
env_panic
#define env_panic(...)
Definition: environment.h:98
env_shm_buf_t::ENV_CACHE_LINE_ALIGNED
uint8_t data[] ENV_CACHE_LINE_ALIGNED
Definition: env_sharedmem.c:52
env_shm_hdr_t
Definition: env_sharedmem.c:38
environment.h
env_shm_buf_t
Definition: env_sharedmem.c:45