1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-30 00:00:16 +01:00

dsh-empty

This commit is contained in:
Andy Green 2021-09-14 05:33:24 +01:00
parent 43bb8aead0
commit 65f16d737e
2 changed files with 59 additions and 29 deletions

View file

@ -71,6 +71,9 @@ enum {
LWS_VISIBLE LWS_EXTERN struct lws_dsh *
lws_dsh_create(lws_dll2_owner_t *owner, size_t buffer_size, int count_kinds);
LWS_VISIBLE LWS_EXTERN void
lws_dsh_empty(struct lws_dsh *dsh);
/**
* lws_dsh_destroy() - Destroy a DSH buffer
*

View file

@ -24,6 +24,16 @@
#include "private-lib-core.h"
#if defined(STANDALONE)
#undef lws_malloc
#define lws_malloc(a, b) malloc(a)
#undef lws_free
#define lws_free(a) free(a)
#undef lws_free_set_NULL
#define lws_free_set_NULL(a) { if (a) { free(a); a = NULL; }}
#endif
struct lws_dsh_search {
size_t required;
ssize_t natural_required;
@ -54,14 +64,44 @@ lws_dsh_align(size_t length)
return length;
}
void
lws_dsh_empty(struct lws_dsh *dsh)
{
lws_dsh_obj_t *obj;
size_t oha_len;
int n;
if (!dsh)
return;
oha_len = sizeof(lws_dsh_obj_head_t) * (unsigned int)dsh->count_kinds;
/* clear down the obj heads array */
memset(dsh->oha, 0, oha_len);
for (n = 0; n < dsh->count_kinds; n++) {
dsh->oha[n].kind = n;
dsh->oha[n].total_size = 0;
}
/* initially the whole buffer is on the free kind (0) list */
obj = (lws_dsh_obj_t *)dsh->buf;
memset(obj, 0, sizeof(*obj));
obj->asize = dsh->buffer_size - sizeof(*obj);
lws_dll2_add_head(&obj->list, &dsh->oha[0].owner);
dsh->locally_free = obj->asize;
dsh->locally_in_use = 0;
}
lws_dsh_t *
lws_dsh_create(lws_dll2_owner_t *owner, size_t buf_len, int _count_kinds)
{
int count_kinds = _count_kinds & 0xff;
lws_dsh_obj_t *obj;
lws_dsh_t *dsh;
size_t oha_len;
int n;
oha_len = sizeof(lws_dsh_obj_head_t) * (unsigned int)(++count_kinds);
@ -76,6 +116,7 @@ lws_dsh_create(lws_dll2_owner_t *owner, size_t buf_len, int _count_kinds)
/* set convenience pointers to the overallocated parts */
lws_dll2_clear(&dsh->list);
dsh->oha = (lws_dsh_obj_head_t *)&dsh[1];
dsh->buf = ((uint8_t *)dsh->oha) + oha_len;
dsh->count_kinds = count_kinds;
@ -84,26 +125,8 @@ lws_dsh_create(lws_dll2_owner_t *owner, size_t buf_len, int _count_kinds)
dsh->splitat = 0;
dsh->flags = (unsigned int)_count_kinds & 0xff000000u;
/* clear down the obj heads array */
lws_dsh_empty(dsh);
memset(dsh->oha, 0, oha_len);
for (n = 0; n < count_kinds; n++) {
dsh->oha[n].kind = n;
dsh->oha[n].total_size = 0;
}
/* initially the whole buffer is on the free kind (0) list */
obj = (lws_dsh_obj_t *)dsh->buf;
memset(obj, 0, sizeof(*obj));
obj->asize = buf_len - sizeof(*obj);
lws_dll2_add_head(&obj->list, &dsh->oha[0].owner);
dsh->locally_free = obj->asize;
dsh->locally_in_use = 0;
lws_dll2_clear(&dsh->list);
if (owner)
lws_dll2_add_head(&dsh->list, owner);
@ -176,6 +199,7 @@ lws_dsh_destroy(lws_dsh_t **pdsh)
dsh->being_destroyed = 1;
lws_dll2_remove(&dsh->list);
lws_dsh_empty(dsh);
/* everything else is in one heap allocation */
@ -243,8 +267,8 @@ _lws_dsh_alloc_tail(lws_dsh_t *dsh, int kind, const void *src1, size_t size1,
lws_dll2_foreach_safe(&dsh->oha[0].owner, &s, search_best_free);
if (!s.best) {
lwsl_notice("%s: no buffer has space for %lu\n",
__func__, (unsigned long)asize);
//lwsl_notice("%s: no buffer has space for %lu\n",
// __func__, (unsigned long)asize);
return 1;
}
@ -255,7 +279,7 @@ _lws_dsh_alloc_tail(lws_dsh_t *dsh, int kind, const void *src1, size_t size1,
lws_dsh_obj_t *rh;
size_t le;
// lwsl_notice("%s: coalescing\n", __func__);
// lwsl_notice("%s: coalescing\n", __func__);
/*
* logically remove the free list entry we're taking over the
@ -273,8 +297,10 @@ _lws_dsh_alloc_tail(lws_dsh_t *dsh, int kind, const void *src1, size_t size1,
s.dsh->oha[kind].total_size -= s.tail_obj->asize;
s.dsh->locally_in_use -= s.tail_obj->asize;
memcpy(nf, src1, size1);
nf += size1;
if (size1) {
memcpy(nf, src1, size1);
nf += size1;
}
if (size2) {
memcpy(nf, src2, size2);
nf += size2;
@ -321,7 +347,7 @@ _lws_dsh_alloc_tail(lws_dsh_t *dsh, int kind, const void *src1, size_t size1,
}
/* anything coming out of here must be aligned */
assert(!(((unsigned long)s.best) & (sizeof(int *) - 1)));
assert(!(((size_t)(intptr_t)s.best) & (sizeof(int *) - 1)));
if (s.best->asize < asize + (2 * sizeof(*s.best))) {
@ -410,7 +436,8 @@ _lws_dsh_alloc_tail(lws_dsh_t *dsh, int kind, const void *src1, size_t size1,
assert((uint8_t *)s.best + s.best->asize < e);
assert((uint8_t *)s.best + s.best->asize <= (uint8_t *)nf);
memcpy(&s.best[1], src1, size1);
if (size1)
memcpy(&s.best[1], src1, size1);
if (src2)
memcpy((uint8_t *)&s.best[1] + size1, src2, size2);
@ -498,7 +525,7 @@ lws_dsh_free(void **pobj)
lws_dsh_t *dsh = _o->dsh;
/* anything coming out of here must be aligned */
assert(!(((unsigned long)_o) & (sizeof(int *) - 1)));
assert(!(((size_t)(intptr_t)_o) & (sizeof(int *) - 1)));
/*
* Remove the object from its list and place on the free list of the