1 /* 2 * Copyright 2018 Rockchip Electronics Co. LTD 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 * author: martin.cheng@rock-chips.com 17 * date: 20180704 18 */ 19 20 #ifndef INCLUDE_RT_BASE_RT_MEM_H_ 21 #define INCLUDE_RT_BASE_RT_MEM_H_ 22 23 #include <stdlib.h> 24 #include <stdint.h> 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #define rt_malloc(type) \ 31 reinterpret_cast<type *>( \ 32 rt_mem_malloc(sizeof(type), __FUNCTION__)) 33 34 #define rt_malloc_array(type, count) \ 35 reinterpret_cast<type *>( \ 36 rt_mem_malloc(sizeof(type) * (count), __FUNCTION__)) 37 38 #define rt_malloc_size(type, size) \ 39 reinterpret_cast<type *>( \ 40 rt_mem_malloc(size, __FUNCTION__)) 41 42 #define rt_calloc_size(type, size) \ 43 reinterpret_cast<type *>( \ 44 rt_mem_calloc(size, __FUNCTION__)) 45 46 #define rt_calloc(type, count) \ 47 reinterpret_cast<type *>( \ 48 rt_mem_calloc(sizeof(type) * (count), __FUNCTION__)) 49 50 #define rt_realloc(ptr, type, count) \ 51 reinterpret_cast<type *>( \ 52 rt_mem_realloc(ptr, sizeof(type) * (count), __FUNCTION__)) 53 54 #define rt_free(ptr) \ 55 rt_mem_free(ptr, __FUNCTION__) 56 57 #define rt_safe_free(ptr) \ 58 rt_mem_safe_free( \ 59 reinterpret_cast<void **>(&(ptr)), __FUNCTION__) 60 61 #ifndef rt_safe_delete 62 #define rt_safe_delete(p) { if (p) {delete(p); (p)=NULL;} } 63 #endif 64 65 #ifndef rt_safe_release 66 #define rt_safe_release(p) { if (p) {p->release(); p=NULL;} } 67 #endif 68 69 void *rt_mem_malloc(size_t size, const char *caller); 70 void *rt_mem_calloc(size_t size, const char *caller); 71 void *rt_mem_realloc(void *ptr, size_t size, const char *caller); 72 void rt_mem_free(void *ptr, const char *caller); 73 void rt_mem_safe_free(void **ptr, const char *caller); 74 void *rt_memset(void *ptr, int c, size_t n); 75 void *rt_memcpy(void *dst, const void *src, size_t n); 76 77 void rt_dump_callstack(size_t deep); 78 79 // utility functions for MemService. 80 void rt_mem_record_dump(); 81 void rt_mem_record_reset(); 82 void rt_mem_record_add_ref(); 83 void rt_mem_record_sub_ref(); 84 85 // utility functions for memory statistics 86 uint64_t rt_mem_get_total_size(); 87 uint32_t rt_mem_get_total_count(); 88 89 #ifdef __cplusplus 90 } 91 #endif 92 93 extern "C" { 94 void *av_mem_malloc(size_t size); 95 void *av_mem_memalign(size_t align, size_t size); 96 int av_mem_posix_memalign(void **ptr, size_t align, size_t size); 97 void *av_mem_realloc(void *ptr, size_t size); 98 void av_mem_free(void *ptr); 99 } 100 101 #endif // INCLUDE_RT_BASE_RT_MEM_H_ 102