Motr  M0
memory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #pragma once
23 
24 #ifndef __MOTR_LIB_MEMORY_H__
25 #define __MOTR_LIB_MEMORY_H__
26 
27 #include "lib/types.h"
28 #include "lib/assert.h" /* M0_CASSERT */
29 #include "lib/finject.h" /* M0_FI_ENABLED */
30 
45 void *m0_alloc(size_t size);
46 
52 M0_INTERNAL void *m0_alloc_nz(size_t size);
53 
61 void m0_free(void *data);
62 
74 M0_INTERNAL void m0_memory_pagein(void *addr, size_t size);
75 
77 #define m0_free0(pptr) \
78  do { \
79  typeof(pptr) __pptr = (pptr); \
80  m0_free(*__pptr); \
81  *__pptr = NULL; \
82  } while (0)
83 
84 #define M0_ALLOC_ARR(arr, nr) ((arr) = M0_FI_ENABLED(#arr "-fail") ? NULL : \
85  m0_alloc((nr) * sizeof ((arr)[0])))
86 #define M0_ALLOC_PTR(ptr) M0_ALLOC_ARR(ptr, 1)
87 
88 #define M0_ALLOC_ARR_ALIGNED(arr, nr, shift) \
89  ((arr) = m0_alloc_aligned((nr) * sizeof ((arr)[0]), (shift)))
90 
96 M0_INTERNAL void *m0_alloc_aligned(size_t size, unsigned shift);
97 
104 M0_INTERNAL void m0_free_aligned(void *data, size_t size, unsigned shift);
105 
107 static inline bool m0_addr_is_aligned(const void *addr, unsigned shift)
108 {
109  M0_CASSERT(sizeof(unsigned long) >= sizeof(void *));
110  return ((((unsigned long)addr >> shift) << shift) ==
111  (unsigned long)addr);
112 }
113 
124 M0_INTERNAL void *m0_alloc_wired(size_t size, unsigned shift);
125 
129 M0_INTERNAL void m0_free_wired(void *data, size_t size, unsigned shift);
130 
134 M0_INTERNAL size_t m0_allocated(void);
135 
140 M0_INTERNAL size_t m0_allocated_total(void);
141 
146 M0_INTERNAL size_t m0_freed_total(void);
147 
152 M0_INTERNAL int m0_pagesize_get(void);
153 
158 M0_INTERNAL int m0_pageshift_get(void);
159 
170 M0_INTERNAL bool m0_is_poisoned(const void *p);
171 
176 M0_INTERNAL int m0_dont_dump(void *p, size_t size);
177 
178 
180 #endif /* __MOTR_LIB_MEMORY_H__ */
181 
182 /*
183  * Local variables:
184  * c-indentation-style: "K&R"
185  * c-basic-offset: 8
186  * tab-width: 8
187  * fill-column: 80
188  * scroll-step: 1
189  * End:
190  */
static struct m0_addb2_philter p
Definition: consumer.c:40
static bool m0_addr_is_aligned(const void *addr, unsigned shift)
Definition: memory.h:107
M0_INTERNAL size_t m0_allocated_total(void)
Definition: memory.c:221
#define M0_CASSERT(cond)
struct m0_bufvec data
Definition: di.c:40
M0_INTERNAL void * m0_alloc_wired(size_t size, unsigned shift)
Definition: memory.c:202
M0_INTERNAL void m0_free_aligned(void *data, size_t size, unsigned shift)
Definition: memory.c:192
M0_INTERNAL int m0_pageshift_get(void)
Definition: memory.c:238
M0_INTERNAL size_t m0_allocated(void)
Definition: memory.c:215
M0_INTERNAL void m0_free_wired(void *data, size_t size, unsigned shift)
Definition: memory.c:207
M0_INTERNAL int m0_pagesize_get(void)
Definition: memory.c:233
M0_INTERNAL void * m0_alloc_nz(size_t size)
Definition: memory.c:115
M0_INTERNAL int m0_dont_dump(void *p, size_t size)
Definition: memory.c:243
void * m0_alloc(size_t size)
Definition: memory.c:126
Definition: xcode.h:73
M0_INTERNAL bool m0_is_poisoned(const void *ptr)
Definition: memory.c:79
M0_INTERNAL size_t m0_freed_total(void)
Definition: memory.c:227
m0_bcount_t size
Definition: di.c:39
M0_INTERNAL void m0_memory_pagein(void *addr, size_t size)
Definition: memory.c:163
M0_INTERNAL void * m0_alloc_aligned(size_t size, unsigned shift)
Definition: memory.c:168
void m0_free(void *data)
Definition: memory.c:146