blob: 34e7a8acc80100f84e21cb2fc4f079713557d95e [file] [log] [blame]
Alejandro Merycdc97172014-12-04 23:15:27 +01001#include "private-libwebsockets.h"
2
Andy Green9395eb62017-01-30 15:02:54 +08003#if defined(LWS_PLAT_OPTEE)
4
5#define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000
6
7void *__attribute__((weak))
8 TEE_Malloc(uint32_t size, uint32_t hint)
9{
10 return NULL;
11}
12void *__attribute__((weak))
13 TEE_Realloc(void *buffer, uint32_t newSize)
14{
15 return NULL;
16}
17void __attribute__((weak))
18 TEE_Free(void *buffer)
19{
20}
21
22void *lws_realloc(void *ptr, size_t size)
23{
24 return TEE_Realloc(ptr, size);
25}
26
27void *lws_malloc(size_t size)
28{
29 return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
30}
31
32void lws_free(void *p)
33{
34 TEE_Free(p);
35}
36
37void *lws_zalloc(size_t size)
38{
39 void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
40 if (ptr)
41 memset(ptr, 0, size);
42 return ptr;
43}
44
45void lws_set_allocator(void *(*cb)(void *ptr, size_t size))
46{
47 (void)cb;
48}
49#else
50
Alejandro Merycdc97172014-12-04 23:15:27 +010051static void *_realloc(void *ptr, size_t size)
52{
53 if (size)
Andy Green9395eb62017-01-30 15:02:54 +080054#if defined(LWS_PLAT_OPTEE)
55 return (void *)TEE_Realloc(ptr, size);
56#else
Andy Green7acf76c2016-07-23 14:18:25 +080057 return (void *)realloc(ptr, size);
Andy Green9395eb62017-01-30 15:02:54 +080058#endif
Alejandro Merycdc97172014-12-04 23:15:27 +010059 else if (ptr)
60 free(ptr);
61 return NULL;
62}
63
64void *(*_lws_realloc)(void *ptr, size_t size) = _realloc;
65
66void *lws_realloc(void *ptr, size_t size)
67{
68 return _lws_realloc(ptr, size);
69}
70
71void *lws_zalloc(size_t size)
72{
73 void *ptr = _lws_realloc(NULL, size);
74 if (ptr)
75 memset(ptr, 0, size);
76 return ptr;
77}
78
79void lws_set_allocator(void *(*cb)(void *ptr, size_t size))
80{
81 _lws_realloc = cb;
82}
Andy Green9395eb62017-01-30 15:02:54 +080083#endif