blob: cdc9d90cab0ae5d9879eb1958f174fe7f6857864 [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_free (svm_region_t * rp, void *ptr)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
Damjan Marionbfa75d62020-10-06 17:46:06 +020049 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040050 ASSERT (rp->flags & SVM_FLAGS_MHEAP);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Dave Barach8a7fb0c2016-07-08 14:44:23 -040052 pthread_mutex_lock (&rp->mutex);
53 oldheap = clib_mem_set_heap (rp->data_heap);
54 clib_mem_free (ptr);
55 clib_mem_set_heap (oldheap);
56 pthread_mutex_unlock (&rp->mutex);
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58}
59
Dave Barach8a7fb0c2016-07-08 14:44:23 -040060static inline void *
61svm_push_pvt_heap (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
Damjan Marionbfa75d62020-10-06 17:46:06 +020063 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040064 oldheap = clib_mem_set_heap (rp->region_heap);
65 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066}
67
Dave Barach8a7fb0c2016-07-08 14:44:23 -040068static inline void *
69svm_push_data_heap (svm_region_t * rp)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
Damjan Marionbfa75d62020-10-06 17:46:06 +020071 clib_mem_heap_t *oldheap;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040072 oldheap = clib_mem_set_heap (rp->data_heap);
73 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074}
75
Dave Barach8a7fb0c2016-07-08 14:44:23 -040076static inline void
77svm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
Dave Barach8a7fb0c2016-07-08 14:44:23 -040079 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070080}
81
Ed Warnickecb9cada2015-12-08 15:45:58 -070082#endif /* __included_svm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040083
84/*
85 * fd.io coding-style-patch-verification: ON
86 *
87 * Local Variables:
88 * eval: (c-set-style "gnu")
89 * End:
90 */