• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __MPP_MEM_H__
17 #define __MPP_MEM_H__
18 
19 #include <stdlib.h>
20 
21 #include "rk_type.h"
22 #include "mpp_err.h"
23 
24 #define mpp_malloc_with_caller(type, count, caller)  \
25     (type*)mpp_osal_malloc(caller, sizeof(type) * (count))
26 
27 #define mpp_malloc(type, count)  \
28     (type*)mpp_osal_malloc(__FUNCTION__, sizeof(type) * (count))
29 
30 #define mpp_malloc_size(type, size)  \
31     (type*)mpp_osal_malloc(__FUNCTION__, size)
32 
33 #define mpp_calloc_size(type, size)  \
34     (type*)mpp_osal_calloc(__FUNCTION__, size)
35 
36 #define mpp_calloc(type, count)  \
37     (type*)mpp_osal_calloc(__FUNCTION__, sizeof(type) * (count))
38 
39 #define mpp_realloc(ptr, type, count) \
40     (type*)mpp_osal_realloc(__FUNCTION__, ptr, sizeof(type) * (count))
41 
42 #define mpp_free(ptr) \
43     mpp_osal_free(__FUNCTION__, ptr)
44 
45 #define MPP_FREE(ptr)   do { if (ptr) mpp_free(ptr); ptr = NULL; } while (0)
46 #define MPP_FCLOSE(fp)  do { if (fp)  fclose(fp);     fp = NULL; } while (0)
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 void *mpp_osal_malloc(const char *caller, size_t size);
53 void *mpp_osal_calloc(const char *caller, size_t size);
54 void *mpp_osal_realloc(const char *caller, void *ptr, size_t size);
55 void mpp_osal_free(const char *caller, void *ptr);
56 
57 void mpp_show_mem_status(void);
58 RK_U32 mpp_mem_total_now(void);
59 RK_U32 mpp_mem_total_max(void);
60 
61 /*
62  * mpp memory usage snapshot tool
63  *
64  * usage:
65  * call mpp_mem_get_snapshot on context init get one snapshot
66  * call mpp_mem_get_snapshot on context deinit get another snapshot
67  * call mpp_mem_diff_snapshot to show the difference between these two snapshot
68  * call mpp_mem_put_snapshot twice to release these two snapshot
69  */
70 typedef void* MppMemSnapshot;
71 
72 MPP_RET mpp_mem_get_snapshot(MppMemSnapshot *hnd);
73 MPP_RET mpp_mem_put_snapshot(MppMemSnapshot *hnd);
74 MPP_RET mpp_mem_squash_snapshot(MppMemSnapshot hnd0, MppMemSnapshot hnd1);
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif /* __MPP_MEM_H__ */