1 /*! 2 * @file amp_memmgt.h 3 * 4 * Copyright (C) 2015-2021 Alibaba Group Holding Limited 5 */ 6 7 #ifndef _AMP_MEMMGT_H_ 8 #define _AMP_MEMMGT_H_ 9 10 #include "stdio.h" 11 #include "stdlib.h" 12 #include "stdint.h" 13 14 //#define MEMMGT_MEM_SIZE_CHECK 15 //#define MEMMGT_MEM_DBG_STATIC 16 //#define MEMMGT_MEM_TRACE 17 #define MEMMGT_TOTAL_PT_NUM 2 18 19 #define MEMMGT_OFFSET 4 20 21 typedef void * (*amp_memmgt_malloc_fn)(unsigned int size); 22 typedef void * (*amp_memmgt_realloc_fn)(void *ptr, unsigned int size); 23 typedef void (*amp_memmgt_free_fn)(void *ptr); 24 25 typedef struct { 26 amp_memmgt_malloc_fn malloc_fn; 27 amp_memmgt_realloc_fn realloc_fn; 28 amp_memmgt_free_fn free_fn; 29 30 int pt_num; 31 int trace_pt; 32 unsigned int mem_limit[MEMMGT_TOTAL_PT_NUM]; 33 } amp_memmgt_config_t; 34 35 extern int amp_memmgt_init(amp_memmgt_config_t *config); 36 37 ////////////////////////////////////////////////////////////////////////////// 38 39 #ifdef __i386__ 40 #define GET_LR(tmp_lr) tmp_lr = 0 41 #else 42 #ifdef __GNUC__ 43 #define GET_LR(tmp_lr) __asm__ __volatile__("mov %0, lr \n":"=r"(tmp_lr):) 44 #else 45 #define GET_LR(tmp_lr) __asm("MOV tmp_lr, lr\n") 46 #endif 47 #endif 48 49 extern void *amp_memmgt_malloc(unsigned int size, unsigned int lr, int ptno); 50 51 #define AMP_MEMMGT_MALLOC(ptr, size, ptno) \ 52 do { \ 53 unsigned int tmp_lr = 0; \ 54 GET_LR(tmp_lr); \ 55 ptr = amp_memmgt_malloc(size, tmp_lr, ptno); \ 56 } while(0) 57 58 ///////////////////////////////////////////////////////////////////////////////////////// 59 extern void *amp_memmgt_realloc(void *ptr, unsigned int size, unsigned int lr, int ptno); 60 #define AMP_MEMMGT_REALLOC(ptr_new, ptr, size, ptno) \ 61 do { \ 62 unsigned int tmp_lr = 0; \ 63 GET_LR(tmp_lr); \ 64 ptr_new = amp_memmgt_realloc(ptr, size, tmp_lr, ptno); \ 65 } while(0) 66 67 68 ///////////////////////////////////////////////////////////////////////////// 69 extern void amp_memmgt_free(void *ptr, unsigned int lr, int ptno); 70 71 #define AMP_MEMMGT_FREE(ptr, ptno) \ 72 do { \ 73 unsigned int tmp_lr = 0; \ 74 GET_LR(tmp_lr); \ 75 amp_memmgt_free(ptr, tmp_lr, ptno); \ 76 } while(0) 77 78 #endif 79