• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 #ifndef __OAL_MM_H__
19 #define __OAL_MM_H__
20 
21 /* ****************************************************************************
22   1 其他头文件包含
23 **************************************************************************** */
24 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
25 #include <linux/slab.h>
26 #include <linux/hardirq.h>
27 #include <linux/vmalloc.h>
28 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
29 #include <string.h>
30 #endif
31 #include "hi_types_base.h"
32 #include "hi_stdlib.h"
33 
34 #ifdef __cplusplus
35 #if __cplusplus
36 extern "C" {
37 #endif
38 #endif
39 
40 /* ****************************************************************************
41   2 宏定义
42 **************************************************************************** */
43 #define OAL_GFP_KERNEL GFP_KERNEL
44 #define OAL_GFP_ATOMIC GFP_ATOMIC
45 
46 /* ****************************************************************************
47   10 函数声明
48 **************************************************************************** */
49 /* ****************************************************************************
50  功能描述  : 申请核心态的内存空间,并填充0。对于Linux操作系统而言,
51              需要考虑中断上下文和内核上下文的不同情况(GFP_KERNEL和GFP_ATOMIC)。
52  输入参数  : ul_size: alloc mem size
53  返 回 值  : alloc mem addr
54 **************************************************************************** */
oal_memalloc(hi_u32 ul_size)55 static inline hi_void *oal_memalloc(hi_u32 ul_size)
56 {
57 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
58     hi_s32 l_flags = GFP_KERNEL;
59     hi_void *puc_mem_space = HI_NULL;
60 
61     /* 不睡眠或在中断程序中标志置为GFP_ATOMIC */
62     if (in_interrupt() || irqs_disabled()) {
63         l_flags = GFP_ATOMIC;
64     }
65 
66     puc_mem_space = kmalloc(ul_size, l_flags);
67     if (puc_mem_space == HI_NULL) {
68         return HI_NULL;
69     }
70 
71     return puc_mem_space;
72 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
73     return malloc(ul_size);
74 #endif
75 }
76 
oal_kzalloc(hi_u32 ul_size,hi_s32 l_flags)77 static inline hi_void *oal_kzalloc(hi_u32 ul_size, hi_s32 l_flags)
78 {
79 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
80     return kzalloc(ul_size, l_flags);
81 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
82     (void)(l_flags);
83     return calloc(1, ul_size);
84 #endif
85 }
86 
oal_vmalloc(hi_u32 ul_size)87 static inline hi_void *oal_vmalloc(hi_u32 ul_size)
88 {
89 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
90     return vmalloc(ul_size);
91 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
92     return malloc(ul_size);
93 #endif
94 }
95 
96 /* ****************************************************************************
97  功能描述  : 释放核心态的内存空间。
98 **************************************************************************** */
99 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
oal_free(const hi_void * p_buf)100 static inline hi_void oal_free(const hi_void *p_buf)
101 {
102     kfree(p_buf);
103 }
104 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
oal_free(hi_void * p_buf)105 static inline hi_void oal_free(hi_void *p_buf)
106 {
107     free(p_buf);
108 }
109 #endif
110 
oal_vfree(hi_void * p_buf)111 static inline hi_void oal_vfree(hi_void *p_buf)
112 {
113 #if (_PRE_OS_VERSION_LINUX == _PRE_OS_VERSION)
114     vfree(p_buf);
115 #elif (_PRE_OS_VERSION_LITEOS == _PRE_OS_VERSION)
116     free(p_buf);
117 #endif
118 }
119 
120 #ifdef __cplusplus
121 #if __cplusplus
122 }
123 #endif
124 #endif
125 
126 #endif /* end of oal_mm.h */
127