2016-09-26 13:37:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Google, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-06-30 08:39:00 +00:00
|
|
|
#include "bt_common.h"
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "osi/allocator.h"
|
2016-09-26 13:37:39 +00:00
|
|
|
|
|
|
|
extern void *pvPortZalloc(size_t size);
|
|
|
|
extern void vPortFree(void *pv);
|
|
|
|
|
2018-09-17 08:44:38 +00:00
|
|
|
|
|
|
|
#if HEAP_MEMORY_DEBUG
|
2016-12-12 20:10:44 +00:00
|
|
|
|
2018-08-15 02:40:51 +00:00
|
|
|
#define OSI_MEM_DBG_INFO_MAX 1024*3
|
2016-12-12 20:10:44 +00:00
|
|
|
typedef struct {
|
|
|
|
void *p;
|
|
|
|
int size;
|
|
|
|
const char *func;
|
|
|
|
int line;
|
|
|
|
} osi_mem_dbg_info_t;
|
|
|
|
|
|
|
|
static uint32_t mem_dbg_count = 0;
|
|
|
|
static osi_mem_dbg_info_t mem_dbg_info[OSI_MEM_DBG_INFO_MAX];
|
2019-04-24 07:04:24 +00:00
|
|
|
static uint32_t mem_dbg_current_size = 0;
|
2018-09-29 08:04:50 +00:00
|
|
|
static uint32_t mem_dbg_max_size = 0;
|
2016-12-12 20:10:44 +00:00
|
|
|
|
2019-04-24 07:04:24 +00:00
|
|
|
#define OSI_MEM_DBG_MAX_SECTION_NUM 5
|
|
|
|
typedef struct {
|
|
|
|
bool used;
|
|
|
|
uint32_t max_size;
|
|
|
|
} osi_mem_dbg_max_size_section_t;
|
|
|
|
static osi_mem_dbg_max_size_section_t mem_dbg_max_size_section[OSI_MEM_DBG_MAX_SECTION_NUM];
|
|
|
|
|
2016-12-12 20:10:44 +00:00
|
|
|
void osi_mem_dbg_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
|
|
|
|
mem_dbg_info[i].p = NULL;
|
|
|
|
mem_dbg_info[i].size = 0;
|
|
|
|
mem_dbg_info[i].func = NULL;
|
|
|
|
mem_dbg_info[i].line = 0;
|
|
|
|
}
|
|
|
|
mem_dbg_count = 0;
|
2019-04-24 07:04:24 +00:00
|
|
|
mem_dbg_current_size = 0;
|
2018-09-29 08:04:50 +00:00
|
|
|
mem_dbg_max_size = 0;
|
2019-04-24 07:04:24 +00:00
|
|
|
|
|
|
|
for (i = 0; i < OSI_MEM_DBG_MAX_SECTION_NUM; i++){
|
|
|
|
mem_dbg_max_size_section[i].used = false;
|
|
|
|
mem_dbg_max_size_section[i].max_size = 0;
|
|
|
|
}
|
2016-12-12 20:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void osi_mem_dbg_record(void *p, int size, const char *func, int line)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!p || size == 0) {
|
2018-04-25 02:11:06 +00:00
|
|
|
OSI_TRACE_ERROR("%s invalid !!\n", __func__);
|
2016-12-12 20:10:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
|
|
|
|
if (mem_dbg_info[i].p == NULL) {
|
|
|
|
mem_dbg_info[i].p = p;
|
|
|
|
mem_dbg_info[i].size = size;
|
|
|
|
mem_dbg_info[i].func = func;
|
|
|
|
mem_dbg_info[i].line = line;
|
|
|
|
mem_dbg_count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= OSI_MEM_DBG_INFO_MAX) {
|
2018-04-25 02:11:06 +00:00
|
|
|
OSI_TRACE_ERROR("%s full %s %d !!\n", __func__, func, line);
|
2016-12-12 20:10:44 +00:00
|
|
|
}
|
2018-09-29 08:04:50 +00:00
|
|
|
|
2019-04-24 07:04:24 +00:00
|
|
|
mem_dbg_current_size += size;
|
|
|
|
if(mem_dbg_max_size < mem_dbg_current_size) {
|
|
|
|
mem_dbg_max_size = mem_dbg_current_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < OSI_MEM_DBG_MAX_SECTION_NUM; i++){
|
|
|
|
if (mem_dbg_max_size_section[i].used) {
|
|
|
|
if(mem_dbg_max_size_section[i].max_size < mem_dbg_current_size) {
|
|
|
|
mem_dbg_max_size_section[i].max_size = mem_dbg_current_size;
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 08:04:50 +00:00
|
|
|
}
|
2016-12-12 20:10:44 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 06:57:30 +00:00
|
|
|
void osi_mem_dbg_clean(void *p, const char *func, int line)
|
2016-12-12 20:10:44 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!p) {
|
2018-04-25 02:11:06 +00:00
|
|
|
OSI_TRACE_ERROR("%s invalid\n", __func__);
|
2016-12-12 20:10:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
|
|
|
|
if (mem_dbg_info[i].p == p) {
|
2019-04-24 07:04:24 +00:00
|
|
|
mem_dbg_current_size -= mem_dbg_info[i].size;
|
2016-12-12 20:10:44 +00:00
|
|
|
mem_dbg_info[i].p = NULL;
|
|
|
|
mem_dbg_info[i].size = 0;
|
|
|
|
mem_dbg_info[i].func = NULL;
|
|
|
|
mem_dbg_info[i].line = 0;
|
|
|
|
mem_dbg_count--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= OSI_MEM_DBG_INFO_MAX) {
|
2018-04-25 02:11:06 +00:00
|
|
|
OSI_TRACE_ERROR("%s full %s %d !!\n", __func__, func, line);
|
2016-12-12 20:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void osi_mem_dbg_show(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < OSI_MEM_DBG_INFO_MAX; i++) {
|
|
|
|
if (mem_dbg_info[i].p || mem_dbg_info[i].size != 0 ) {
|
2018-04-25 02:11:06 +00:00
|
|
|
OSI_TRACE_ERROR("--> p %p, s %d, f %s, l %d\n", mem_dbg_info[i].p, mem_dbg_info[i].size, mem_dbg_info[i].func, mem_dbg_info[i].line);
|
2016-12-12 20:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-25 02:11:06 +00:00
|
|
|
OSI_TRACE_ERROR("--> count %d\n", mem_dbg_count);
|
2019-04-24 07:04:24 +00:00
|
|
|
OSI_TRACE_ERROR("--> size %dB\n--> max size %dB\n", mem_dbg_current_size, mem_dbg_max_size);
|
2018-09-29 08:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t osi_mem_dbg_get_max_size(void)
|
|
|
|
{
|
|
|
|
return mem_dbg_max_size;
|
|
|
|
}
|
|
|
|
|
2019-04-24 07:04:24 +00:00
|
|
|
uint32_t osi_mem_dbg_get_current_size(void)
|
2018-09-29 08:04:50 +00:00
|
|
|
{
|
2019-04-24 07:04:24 +00:00
|
|
|
return mem_dbg_current_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void osi_men_dbg_set_section_start(uint8_t index)
|
|
|
|
{
|
|
|
|
if (index >= OSI_MEM_DBG_MAX_SECTION_NUM) {
|
|
|
|
OSI_TRACE_ERROR("Then range of index should be between 0 and %d, current index is %d.\n",
|
|
|
|
OSI_MEM_DBG_MAX_SECTION_NUM - 1, index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mem_dbg_max_size_section[index].used) {
|
|
|
|
OSI_TRACE_WARNING("This index(%d) has been started, restart it.\n", index);
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_dbg_max_size_section[index].used = true;
|
|
|
|
mem_dbg_max_size_section[index].max_size = mem_dbg_current_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void osi_men_dbg_set_section_end(uint8_t index)
|
|
|
|
{
|
|
|
|
if (index >= OSI_MEM_DBG_MAX_SECTION_NUM) {
|
|
|
|
OSI_TRACE_ERROR("Then range of index should be between 0 and %d, current index is %d.\n",
|
|
|
|
OSI_MEM_DBG_MAX_SECTION_NUM - 1, index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mem_dbg_max_size_section[index].used) {
|
|
|
|
OSI_TRACE_ERROR("This index(%d) has not been started.\n", index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem_dbg_max_size_section[index].used = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t osi_mem_dbg_get_max_size_section(uint8_t index)
|
|
|
|
{
|
|
|
|
if (index >= OSI_MEM_DBG_MAX_SECTION_NUM){
|
|
|
|
OSI_TRACE_ERROR("Then range of index should be between 0 and %d, current index is %d.\n",
|
|
|
|
OSI_MEM_DBG_MAX_SECTION_NUM - 1, index);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mem_dbg_max_size_section[index].max_size;
|
2016-12-12 20:10:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
char *osi_strdup(const char *str)
|
|
|
|
{
|
|
|
|
size_t size = strlen(str) + 1; // + 1 for the null terminator
|
2017-06-13 12:52:26 +00:00
|
|
|
char *new_string = (char *)osi_calloc(size);
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
if (!new_string) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-26 13:37:39 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
memcpy(new_string, str, size);
|
|
|
|
return new_string;
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:10:44 +00:00
|
|
|
void *osi_malloc_func(size_t size)
|
2016-11-24 18:10:15 +00:00
|
|
|
{
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_MEMORY_DEBUG
|
2016-12-13 06:57:30 +00:00
|
|
|
void *p;
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
|
2017-10-18 07:34:43 +00:00
|
|
|
p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
|
|
|
|
#else
|
2017-08-17 13:13:45 +00:00
|
|
|
p = malloc(size);
|
2018-09-17 08:44:38 +00:00
|
|
|
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
|
2016-12-13 06:57:30 +00:00
|
|
|
osi_mem_dbg_record(p, size, __func__, __LINE__);
|
|
|
|
return p;
|
2017-10-18 07:34:43 +00:00
|
|
|
#else
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
|
2017-10-18 07:34:43 +00:00
|
|
|
return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
|
2016-12-13 06:57:30 +00:00
|
|
|
#else
|
2017-08-17 13:13:45 +00:00
|
|
|
return malloc(size);
|
2018-09-17 08:44:38 +00:00
|
|
|
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
|
|
|
|
#endif /* #if HEAP_MEMORY_DEBUG */
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:10:44 +00:00
|
|
|
void *osi_calloc_func(size_t size)
|
2016-11-24 18:10:15 +00:00
|
|
|
{
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_MEMORY_DEBUG
|
2016-12-13 06:57:30 +00:00
|
|
|
void *p;
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
|
2017-10-18 07:34:43 +00:00
|
|
|
p = heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
|
|
|
|
#else
|
2016-12-13 06:57:30 +00:00
|
|
|
p = calloc(1, size);
|
2018-09-17 08:44:38 +00:00
|
|
|
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
|
2016-12-13 06:57:30 +00:00
|
|
|
osi_mem_dbg_record(p, size, __func__, __LINE__);
|
|
|
|
return p;
|
2017-10-18 07:34:43 +00:00
|
|
|
#else
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_ALLOCATION_FROM_SPIRAM_FIRST
|
2017-10-18 07:34:43 +00:00
|
|
|
return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
|
2016-12-13 06:57:30 +00:00
|
|
|
#else
|
2016-11-24 18:10:15 +00:00
|
|
|
return calloc(1, size);
|
2018-09-17 08:44:38 +00:00
|
|
|
#endif /* #if HEAP_ALLOCATION_FROM_SPIRAM_FIRST */
|
|
|
|
#endif /* #if HEAP_MEMORY_DEBUG */
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 20:10:44 +00:00
|
|
|
void osi_free_func(void *ptr)
|
2016-11-24 18:10:15 +00:00
|
|
|
{
|
2018-09-17 08:44:38 +00:00
|
|
|
#if HEAP_MEMORY_DEBUG
|
|
|
|
osi_mem_dbg_clean(ptr, __func__, __LINE__);
|
2016-12-13 06:57:30 +00:00
|
|
|
#endif
|
2016-11-24 18:10:15 +00:00
|
|
|
free(ptr);
|
2016-09-26 13:37:39 +00:00
|
|
|
}
|