blob: 8bf561e9a811c99f7f5ee3682f5717af59aec2ca [file] [log] [blame]
Dave Barach8a7fb0c2016-07-08 14:44:23 -04001/*
Ed Warnickecb9cada2015-12-08 15:45:58 -07002 *------------------------------------------------------------------
Dave Barach8a7fb0c2016-07-08 14:44:23 -04003 * svm.h - shared VM allocation, mmap(...MAP_FIXED...)
Ed Warnickecb9cada2015-12-08 15:45:58 -07004 * brain police
5 *
6 * Copyright (c) 2009 Cisco and/or its affiliates.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at:
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *------------------------------------------------------------------
19 */
20
21#ifndef __included_svm_h__
22#define __included_svm_h__
23
24#include <pthread.h>
25#include <vppinfra/clib.h>
26#include <vppinfra/mem.h>
Klement Sekera58eb8662017-06-09 06:06:49 +020027#include <svm/svm_common.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Dave Barach95bb8832015-12-12 10:37:00 -050029#define MMAP_PAGESIZE (clib_mem_get_page_size())
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
Dave Barach8a7fb0c2016-07-08 14:44:23 -040031static inline void *
32svm_mem_alloc (svm_region_t * rp, uword size)
Ed Warnickecb9cada2015-12-08 15:45:58 -070033{
Damjan Marionbfa75d62020-10-06 17:46:06 +020034 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040035 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
36 u8 *rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037
Dave Barach8a7fb0c2016-07-08 14:44:23 -040038 pthread_mutex_lock (&rp->mutex);
39 oldheap = clib_mem_set_heap (rp->data_heap);
40 rv = clib_mem_alloc (size);
41 clib_mem_set_heap (oldheap);
42 pthread_mutex_unlock (&rp->mutex);
43 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -070044}
45
Dave Barach8a7fb0c2016-07-08 14:44:23 -040046static inline void *
47svm_mem_alloc_aligned_at_offset (svm_region_t * rp,
48 uword size, uword align, uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
Damjan Marionbfa75d62020-10-06 17:46:06 +020050 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040051 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
52 u8 *rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Dave Barach8a7fb0c2016-07-08 14:44:23 -040054 pthread_mutex_lock (&rp->mutex);
55 oldheap = clib_mem_set_heap (rp->data_heap);
Dave Barach241e5222016-10-13 10:53:26 -040056 rv = clib_mem_alloc_aligned_at_offset (size, align, offset,
57 1 /* yes, call os_out_of_memory */ );
Dave Barach8a7fb0c2016-07-08 14:44:23 -040058 clib_mem_set_heap (oldheap);
59 pthread_mutex_unlock (&rp->mutex);
60 return (rv);
Ed Warnickecb9cada2015-12-08 15:45:58 -070061}
62
Dave Barach8a7fb0c2016-07-08 14:44:23 -040063static inline void
64svm_mem_free (svm_region_t * rp, void *ptr)
Ed Warnickecb9cada2015-12-08 15:45:58 -070065{
Damjan Marionbfa75d62020-10-06 17:46:06 +020066 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040067 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
Dave Barach8a7fb0c2016-07-08 14:44:23 -040069 pthread_mutex_lock (&rp->mutex);
70 oldheap = clib_mem_set_heap (rp->data_heap);
71 clib_mem_free (ptr);
72 clib_mem_set_heap (oldheap);
73 pthread_mutex_unlock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75}
76
Dave Barach8a7fb0c2016-07-08 14:44:23 -040077static inline void *
78svm_push_pvt_heap (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070079{
Damjan Marionbfa75d62020-10-06 17:46:06 +020080 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040081 oldheap = clib_mem_set_heap (rp->region_heap);
82 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083}
84
Dave Barach8a7fb0c2016-07-08 14:44:23 -040085static inline void *
86svm_push_data_heap (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Damjan Marionbfa75d62020-10-06 17:46:06 +020088 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040089 oldheap = clib_mem_set_heap (rp->data_heap);
90 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070091}
92
Dave Barach8a7fb0c2016-07-08 14:44:23 -040093static inline void
94svm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -070095{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040096 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070097}
98
Ed Warnickecb9cada2015-12-08 15:45:58 -070099#endif /* __included_svm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400100
101/*
102 * fd.io coding-style-patch-verification: ON
103 *
104 * Local Variables:
105 * eval: (c-set-style "gnu")
106 * End:
107 */