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) (type *)mpp_osal_malloc(caller, sizeof(type) * (count)) 25 26 #define mpp_malloc(type, count) (type *)mpp_osal_malloc(__FUNCTION__, sizeof(type) * (count)) 27 28 #define mpp_malloc_size(type, size) (type *)mpp_osal_malloc(__FUNCTION__, size) 29 30 #define mpp_calloc_size(type, size) (type *)mpp_osal_calloc(__FUNCTION__, size) 31 32 #define mpp_calloc(type, count) (type *)mpp_osal_calloc(__FUNCTION__, sizeof(type) * (count)) 33 34 #define mpp_realloc(ptr, type, count) (type *)mpp_osal_realloc(__FUNCTION__, ptr, sizeof(type) * (count)) 35 36 #define mpp_free(ptr) mpp_osal_free(__FUNCTION__, ptr) 37 38 #define MPP_FREE(ptr) \ 39 do { \ 40 if (ptr) \ 41 mpp_free(ptr); \ 42 ptr = NULL; \ 43 } while (0) 44 #define MPP_FCLOSE(fp) \ 45 do { \ 46 if (fp) \ 47 fclose(fp); \ 48 fp = NULL; \ 49 } while (0) 50 51 #ifdef __cplusplus 52 extern "C" { 53 #endif 54 55 void *mpp_osal_malloc(const char *caller, size_t size); 56 void *mpp_osal_calloc(const char *caller, size_t size); 57 void *mpp_osal_realloc(const char *caller, void *ptr, size_t size); 58 void mpp_osal_free(const char *caller, void *ptr); 59 60 void mpp_show_mem_status(void); 61 unsigned int mpp_mem_total_now(void); 62 unsigned int mpp_mem_total_max(void); 63 64 /* 65 * mpp memory usage snapshot tool 66 * 67 * usage: 68 * call mpp_mem_get_snapshot on context init get one snapshot 69 * call mpp_mem_get_snapshot on context deinit get another snapshot 70 * call mpp_mem_diff_snapshot to show the difference between these two snapshot 71 * call mpp_mem_put_snapshot twice to release these two snapshot 72 */ 73 typedef void *MppMemSnapshot; 74 75 MPP_RET mpp_mem_get_snapshot(MppMemSnapshot *hnd); 76 MPP_RET mpp_mem_put_snapshot(MppMemSnapshot *hnd); 77 MPP_RET mpp_mem_squash_snapshot(MppMemSnapshot hnd0, MppMemSnapshot hnd1); 78 79 #ifdef __cplusplus 80 } 81 #endif 82 83 #endif /* __MPP_MEM_H__ */