2020-04-13 18:10:59 +03:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-08-15 10:49:52 +01:00
|
|
|
#include "private-lib-core.h"
|
2014-12-04 23:15:27 +01:00
|
|
|
|
2019-03-16 08:10:47 +08:00
|
|
|
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
|
|
|
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
/* the heap is processwide */
|
|
|
|
static size_t allocated;
|
|
|
|
#endif
|
|
|
|
|
2017-01-30 15:02:54 +08:00
|
|
|
#if defined(LWS_PLAT_OPTEE)
|
|
|
|
|
|
|
|
#define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000
|
2019-01-15 06:59:48 +08:00
|
|
|
#if defined (LWS_WITH_NETWORK)
|
|
|
|
|
|
|
|
/* normal TA apis */
|
2017-01-30 15:02:54 +08:00
|
|
|
|
|
|
|
void *__attribute__((weak))
|
|
|
|
TEE_Malloc(uint32_t size, uint32_t hint)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void *__attribute__((weak))
|
|
|
|
TEE_Realloc(void *buffer, uint32_t newSize)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
void __attribute__((weak))
|
|
|
|
TEE_Free(void *buffer)
|
|
|
|
{
|
|
|
|
}
|
2019-01-15 06:59:48 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* in-OP-TEE core apis */
|
|
|
|
|
|
|
|
void *
|
|
|
|
TEE_Malloc(uint32_t size, uint32_t hint)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
void *
|
|
|
|
TEE_Realloc(void *buffer, uint32_t newSize)
|
|
|
|
{
|
|
|
|
return realloc(buffer, newSize);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
TEE_Free(void *buffer)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2017-01-30 15:02:54 +08:00
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void *lws_realloc(void *ptr, size_t size, const char *reason)
|
2017-01-30 15:02:54 +08:00
|
|
|
{
|
|
|
|
return TEE_Realloc(ptr, size);
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void *lws_malloc(size_t size, const char *reason)
|
2017-01-30 15:02:54 +08:00
|
|
|
{
|
|
|
|
return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lws_free(void *p)
|
|
|
|
{
|
|
|
|
TEE_Free(p);
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void *lws_zalloc(size_t size, const char *reason)
|
2017-01-30 15:02:54 +08:00
|
|
|
{
|
|
|
|
void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
|
|
|
|
if (ptr)
|
|
|
|
memset(ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
|
2017-01-30 15:02:54 +08:00
|
|
|
{
|
|
|
|
(void)cb;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2019-03-16 08:10:47 +08:00
|
|
|
static void *
|
|
|
|
_realloc(void *ptr, size_t size, const char *reason)
|
2014-12-04 23:15:27 +01:00
|
|
|
{
|
2019-03-16 08:10:47 +08:00
|
|
|
void *v;
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
if (size) {
|
2019-08-18 10:35:43 +01:00
|
|
|
#if defined(LWS_PLAT_FREERTOS)
|
2020-07-02 15:38:53 +01:00
|
|
|
lwsl_debug("%s: size %lu: %s (free heap %d)\n", __func__,
|
2019-06-05 05:04:17 +01:00
|
|
|
#if defined(LWS_AMAZON_RTOS)
|
|
|
|
(unsigned long)size, reason, (unsigned int)xPortGetFreeHeapSize() - (int)size);
|
|
|
|
#else
|
2017-11-26 09:22:42 +08:00
|
|
|
(unsigned long)size, reason, (unsigned int)esp_get_free_heap_size() - (int)size);
|
2019-06-05 05:04:17 +01:00
|
|
|
#endif
|
2017-10-13 10:33:02 +08:00
|
|
|
#else
|
2017-10-28 07:42:44 +08:00
|
|
|
lwsl_debug("%s: size %lu: %s\n", __func__,
|
|
|
|
(unsigned long)size, reason);
|
2017-10-13 10:33:02 +08:00
|
|
|
#endif
|
2019-03-16 08:10:47 +08:00
|
|
|
|
|
|
|
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
|
|
|
|
if (ptr)
|
|
|
|
allocated -= malloc_usable_size(ptr);
|
|
|
|
#endif
|
|
|
|
|
2017-01-30 15:02:54 +08:00
|
|
|
#if defined(LWS_PLAT_OPTEE)
|
2019-03-16 08:10:47 +08:00
|
|
|
v = (void *)TEE_Realloc(ptr, size);
|
2017-01-30 15:02:54 +08:00
|
|
|
#else
|
2019-03-16 08:10:47 +08:00
|
|
|
v = (void *)realloc(ptr, size);
|
2017-01-30 15:02:54 +08:00
|
|
|
#endif
|
2019-03-16 08:10:47 +08:00
|
|
|
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
|
|
|
|
allocated += malloc_usable_size(v);
|
|
|
|
#endif
|
|
|
|
return v;
|
2017-10-04 07:10:39 +08:00
|
|
|
}
|
2019-03-16 08:10:47 +08:00
|
|
|
if (ptr) {
|
|
|
|
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
|
|
|
|
allocated -= malloc_usable_size(ptr);
|
|
|
|
#endif
|
2014-12-04 23:15:27 +01:00
|
|
|
free(ptr);
|
2019-03-16 08:10:47 +08:00
|
|
|
}
|
2017-10-04 07:10:39 +08:00
|
|
|
|
2014-12-04 23:15:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
|
2014-12-04 23:15:27 +01:00
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void *lws_realloc(void *ptr, size_t size, const char *reason)
|
2014-12-04 23:15:27 +01:00
|
|
|
{
|
2017-10-04 07:10:39 +08:00
|
|
|
return _lws_realloc(ptr, size, reason);
|
2014-12-04 23:15:27 +01:00
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void *lws_zalloc(size_t size, const char *reason)
|
2014-12-04 23:15:27 +01:00
|
|
|
{
|
2017-10-04 07:10:39 +08:00
|
|
|
void *ptr = _lws_realloc(NULL, size, reason);
|
2019-03-16 08:10:47 +08:00
|
|
|
|
2014-12-04 23:15:27 +01:00
|
|
|
if (ptr)
|
|
|
|
memset(ptr, 0, size);
|
2019-03-16 08:10:47 +08:00
|
|
|
|
2014-12-04 23:15:27 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2017-10-04 07:10:39 +08:00
|
|
|
void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
|
2014-12-04 23:15:27 +01:00
|
|
|
{
|
|
|
|
_lws_realloc = cb;
|
|
|
|
}
|
2019-03-16 08:10:47 +08:00
|
|
|
|
|
|
|
size_t lws_get_allocated_heap(void)
|
|
|
|
{
|
|
|
|
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
|
|
|
|
return allocated;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-30 15:02:54 +08:00
|
|
|
#endif
|