Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 1 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2 | *------------------------------------------------------------------ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 3 | * svm.h - shared VM allocation, mmap(...MAP_FIXED...) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4 | * 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 Sekera | 58eb866 | 2017-06-09 06:06:49 +0200 | [diff] [blame] | 27 | #include <svm/svm_common.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | |
Dave Barach | 95bb883 | 2015-12-12 10:37:00 -0500 | [diff] [blame] | 29 | #define MMAP_PAGESIZE (clib_mem_get_page_size()) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 30 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 31 | static inline void * |
| 32 | svm_mem_alloc (svm_region_t * rp, uword size) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 34 | clib_mem_heap_t *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 35 | ASSERT (rp->flags & SVM_FLAGS_MHEAP); |
| 36 | u8 *rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 38 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 46 | static inline void * |
| 47 | svm_mem_alloc_aligned_at_offset (svm_region_t * rp, |
| 48 | uword size, uword align, uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 50 | clib_mem_heap_t *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 51 | ASSERT (rp->flags & SVM_FLAGS_MHEAP); |
| 52 | u8 *rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 54 | pthread_mutex_lock (&rp->mutex); |
| 55 | oldheap = clib_mem_set_heap (rp->data_heap); |
Dave Barach | 241e522 | 2016-10-13 10:53:26 -0400 | [diff] [blame] | 56 | rv = clib_mem_alloc_aligned_at_offset (size, align, offset, |
| 57 | 1 /* yes, call os_out_of_memory */ ); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 58 | clib_mem_set_heap (oldheap); |
| 59 | pthread_mutex_unlock (&rp->mutex); |
| 60 | return (rv); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 63 | static inline void |
| 64 | svm_mem_free (svm_region_t * rp, void *ptr) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 66 | clib_mem_heap_t *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 67 | ASSERT (rp->flags & SVM_FLAGS_MHEAP); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 68 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 69 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | |
| 75 | } |
| 76 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 77 | static inline void * |
| 78 | svm_push_pvt_heap (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 80 | clib_mem_heap_t *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 81 | oldheap = clib_mem_set_heap (rp->region_heap); |
| 82 | return ((void *) oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 85 | static inline void * |
| 86 | svm_push_data_heap (svm_region_t * rp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | { |
Damjan Marion | bfa75d6 | 2020-10-06 17:46:06 +0200 | [diff] [blame] | 88 | clib_mem_heap_t *oldheap; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 89 | oldheap = clib_mem_set_heap (rp->data_heap); |
| 90 | return ((void *) oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 93 | static inline void |
| 94 | svm_pop_heap (void *oldheap) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 96 | clib_mem_set_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | #endif /* __included_svm_h__ */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * fd.io coding-style-patch-verification: ON |
| 103 | * |
| 104 | * Local Variables: |
| 105 | * eval: (c-set-style "gnu") |
| 106 | * End: |
| 107 | */ |