• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: UPG alloc memory functions source file
15  */
16 
17 #include <stddef.h>
18 #include <stdint.h>
19 #include "common_def.h"
20 #include "upg_common.h"
21 #include "upg_porting.h"
22 
23 #include "upg_alloc.h"
24 
upg_is_flash_addr(const void * addr)25 STATIC bool upg_is_flash_addr(const void *addr)
26 {
27     uintptr_t start_addr = upg_get_flash_base_addr();
28     uintptr_t end_addr = start_addr + upg_get_flash_size();
29     if (start_addr <= (uintptr_t)(addr) && (uintptr_t)(addr) < end_addr) {
30         return true;
31     }
32     return false;
33 }
34 
upg_malloc(uint32_t size)35 void *upg_malloc(uint32_t size)
36 {
37     if (upg_get_func_list()->malloc != NULL) {
38         return upg_get_func_list()->malloc(size);
39     }
40     return NULL;
41 }
42 
upg_free(void * addr)43 void upg_free(void *addr)
44 {
45     if (addr == NULL || upg_is_flash_addr(addr)) { /* 使用总线方式直接读取时,flash地址不需要释放 */
46         return;
47     }
48     if (upg_get_func_list()->free != NULL) {
49         return upg_get_func_list()->free(addr);
50     }
51     return;
52 }
53