mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
Subject: [PATCH] Add custom allocator support using the realloc() interface
Signed-off-by: Alejandro Mery <amery@geeks.cl>
This commit is contained in:
parent
6d91d5f8a1
commit
cdc9717be1
4 changed files with 49 additions and 0 deletions
|
@ -277,6 +277,7 @@ set(SOURCES
|
|||
lib/parsers.c
|
||||
lib/context.c
|
||||
lib/sha-1.c
|
||||
lib/alloc.c
|
||||
)
|
||||
|
||||
if (NOT LWS_WITHOUT_CLIENT)
|
||||
|
|
30
lib/alloc.c
Normal file
30
lib/alloc.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "private-libwebsockets.h"
|
||||
|
||||
static void *_realloc(void *ptr, size_t size)
|
||||
{
|
||||
if (size)
|
||||
return realloc(ptr, size);
|
||||
else if (ptr)
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *(*_lws_realloc)(void *ptr, size_t size) = _realloc;
|
||||
|
||||
void *lws_realloc(void *ptr, size_t size)
|
||||
{
|
||||
return _lws_realloc(ptr, size);
|
||||
}
|
||||
|
||||
void *lws_zalloc(size_t size)
|
||||
{
|
||||
void *ptr = _lws_realloc(NULL, size);
|
||||
if (ptr)
|
||||
memset(ptr, 0, size);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void lws_set_allocator(void *(*cb)(void *ptr, size_t size))
|
||||
{
|
||||
_lws_realloc = cb;
|
||||
}
|
|
@ -1337,6 +1337,12 @@ libwebsocket_read(struct libwebsocket_context *context,
|
|||
LWS_VISIBLE LWS_EXTERN struct libwebsocket_extension *libwebsocket_get_internal_extensions();
|
||||
#endif
|
||||
|
||||
/*
|
||||
* custom allocator support
|
||||
*/
|
||||
LWS_VISIBLE LWS_EXTERN void
|
||||
lws_set_allocator(void *(*realloc)(void *ptr, size_t size));
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1157,6 +1157,18 @@ lws_ssl_capable_write_no_ssl(struct libwebsocket *wsi, unsigned char *buf, int l
|
|||
#define lws_handshake_server(_a, _b, _c, _d) (0)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* custom allocator
|
||||
*/
|
||||
LWS_EXTERN void*
|
||||
lws_realloc(void *ptr, size_t size);
|
||||
|
||||
LWS_EXTERN void*
|
||||
lws_zalloc(size_t size);
|
||||
|
||||
#define lws_malloc(S) lws_realloc(NULL, S)
|
||||
#define lws_free(P) lws_realloc(P, 0)
|
||||
|
||||
/*
|
||||
* lws_plat_
|
||||
*/
|
||||
|
|
Loading…
Add table
Reference in a new issue