Alejandro Mery | cdc9717 | 2014-12-04 23:15:27 +0100 | [diff] [blame] | 1 | #include "private-libwebsockets.h" |
| 2 | |
Andy Green | 9395eb6 | 2017-01-30 15:02:54 +0800 | [diff] [blame] | 3 | #if defined(LWS_PLAT_OPTEE) |
| 4 | |
| 5 | #define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000 |
| 6 | |
| 7 | void *__attribute__((weak)) |
| 8 | TEE_Malloc(uint32_t size, uint32_t hint) |
| 9 | { |
| 10 | return NULL; |
| 11 | } |
| 12 | void *__attribute__((weak)) |
| 13 | TEE_Realloc(void *buffer, uint32_t newSize) |
| 14 | { |
| 15 | return NULL; |
| 16 | } |
| 17 | void __attribute__((weak)) |
| 18 | TEE_Free(void *buffer) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | void *lws_realloc(void *ptr, size_t size) |
| 23 | { |
| 24 | return TEE_Realloc(ptr, size); |
| 25 | } |
| 26 | |
| 27 | void *lws_malloc(size_t size) |
| 28 | { |
| 29 | return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO); |
| 30 | } |
| 31 | |
| 32 | void lws_free(void *p) |
| 33 | { |
| 34 | TEE_Free(p); |
| 35 | } |
| 36 | |
| 37 | void *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 | |
| 45 | void lws_set_allocator(void *(*cb)(void *ptr, size_t size)) |
| 46 | { |
| 47 | (void)cb; |
| 48 | } |
| 49 | #else |
| 50 | |
Alejandro Mery | cdc9717 | 2014-12-04 23:15:27 +0100 | [diff] [blame] | 51 | static void *_realloc(void *ptr, size_t size) |
| 52 | { |
| 53 | if (size) |
Andy Green | 9395eb6 | 2017-01-30 15:02:54 +0800 | [diff] [blame] | 54 | #if defined(LWS_PLAT_OPTEE) |
| 55 | return (void *)TEE_Realloc(ptr, size); |
| 56 | #else |
Andy Green | 7acf76c | 2016-07-23 14:18:25 +0800 | [diff] [blame] | 57 | return (void *)realloc(ptr, size); |
Andy Green | 9395eb6 | 2017-01-30 15:02:54 +0800 | [diff] [blame] | 58 | #endif |
Alejandro Mery | cdc9717 | 2014-12-04 23:15:27 +0100 | [diff] [blame] | 59 | else if (ptr) |
| 60 | free(ptr); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | void *(*_lws_realloc)(void *ptr, size_t size) = _realloc; |
| 65 | |
| 66 | void *lws_realloc(void *ptr, size_t size) |
| 67 | { |
| 68 | return _lws_realloc(ptr, size); |
| 69 | } |
| 70 | |
| 71 | void *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 | |
| 79 | void lws_set_allocator(void *(*cb)(void *ptr, size_t size)) |
| 80 | { |
| 81 | _lws_realloc = cb; |
| 82 | } |
Andy Green | 9395eb6 | 2017-01-30 15:02:54 +0800 | [diff] [blame] | 83 | #endif |