Skip to content
Snippets Groups Projects
Commit acfe57a5 authored by Martine Lenders's avatar Martine Lenders
Browse files

gnrc_pktbuf_static: reduce checks to be in line with new alignment

parent 8a75d8d1
Branches
No related tags found
No related merge requests found
...@@ -108,8 +108,7 @@ gnrc_pktsnip_t *gnrc_pktbuf_mark(gnrc_pktsnip_t *pkt, size_t size, gnrc_nettype_ ...@@ -108,8 +108,7 @@ gnrc_pktsnip_t *gnrc_pktbuf_mark(gnrc_pktsnip_t *pkt, size_t size, gnrc_nettype_
{ {
gnrc_pktsnip_t *marked_snip; gnrc_pktsnip_t *marked_snip;
/* size required for chunk */ /* size required for chunk */
size_t required_new_size = (size < sizeof(_unused_t)) ? size_t required_new_size = _align(size);
_align(sizeof(_unused_t)) : _align(size);
void *new_data_marked; void *new_data_marked;
mutex_lock(&_mutex); mutex_lock(&_mutex);
...@@ -130,8 +129,7 @@ gnrc_pktsnip_t *gnrc_pktbuf_mark(gnrc_pktsnip_t *pkt, size_t size, gnrc_nettype_ ...@@ -130,8 +129,7 @@ gnrc_pktsnip_t *gnrc_pktbuf_mark(gnrc_pktsnip_t *pkt, size_t size, gnrc_nettype_
} }
/* marked data would not fit _unused_t marker => move data around to allow /* marked data would not fit _unused_t marker => move data around to allow
* for proper free */ * for proper free */
if ((pkt->size != size) && if ((pkt->size != size) && (size < required_new_size)) {
((size < required_new_size) || ((pkt->size - size) < sizeof(_unused_t)))) {
void *new_data_rest; void *new_data_rest;
new_data_marked = _pktbuf_alloc(size); new_data_marked = _pktbuf_alloc(size);
if (new_data_marked == NULL) { if (new_data_marked == NULL) {
...@@ -169,8 +167,7 @@ gnrc_pktsnip_t *gnrc_pktbuf_mark(gnrc_pktsnip_t *pkt, size_t size, gnrc_nettype_ ...@@ -169,8 +167,7 @@ gnrc_pktsnip_t *gnrc_pktbuf_mark(gnrc_pktsnip_t *pkt, size_t size, gnrc_nettype_
int gnrc_pktbuf_realloc_data(gnrc_pktsnip_t *pkt, size_t size) int gnrc_pktbuf_realloc_data(gnrc_pktsnip_t *pkt, size_t size)
{ {
size_t aligned_size = (size < sizeof(_unused_t)) ? size_t aligned_size = _align(size);
_align(sizeof(_unused_t)) : _align(size);
mutex_lock(&_mutex); mutex_lock(&_mutex);
assert(pkt != NULL); assert(pkt != NULL);
...@@ -189,8 +186,7 @@ int gnrc_pktbuf_realloc_data(gnrc_pktsnip_t *pkt, size_t size) ...@@ -189,8 +186,7 @@ int gnrc_pktbuf_realloc_data(gnrc_pktsnip_t *pkt, size_t size)
pkt->data = NULL; pkt->data = NULL;
} }
/* if new size is bigger than old size */ /* if new size is bigger than old size */
else if ((size > pkt->size) || /* new size does not fit */ else if (size > pkt->size) { /* new size does not fit */
((pkt->size - aligned_size) < sizeof(_unused_t))) { /* resulting hole would not fit marker */
void *new_data = _pktbuf_alloc(size); void *new_data = _pktbuf_alloc(size);
if (new_data == NULL) { if (new_data == NULL) {
DEBUG("pktbuf: error allocating new data section\n"); DEBUG("pktbuf: error allocating new data section\n");
...@@ -396,7 +392,7 @@ static void *_pktbuf_alloc(size_t size) ...@@ -396,7 +392,7 @@ static void *_pktbuf_alloc(size_t size)
{ {
_unused_t *prev = NULL, *ptr = _first_unused; _unused_t *prev = NULL, *ptr = _first_unused;
size = (size < sizeof(_unused_t)) ? _align(sizeof(_unused_t)) : _align(size); size = _align(size);
while (ptr && (size > ptr->size)) { while (ptr && (size > ptr->size)) {
prev = ptr; prev = ptr;
ptr = ptr->next; ptr = ptr->next;
...@@ -466,11 +462,11 @@ static void _pktbuf_free(void *data, size_t size) ...@@ -466,11 +462,11 @@ static void _pktbuf_free(void *data, size_t size)
ptr = ptr->next; ptr = ptr->next;
} }
new->next = ptr; new->next = ptr;
new->size = (size < sizeof(_unused_t)) ? _align(sizeof(_unused_t)) : _align(size); new->size = _align(size);
/* calculate number of bytes between new _unused_t chunk and end of packet /* calculate number of bytes between new _unused_t chunk and end of packet
* buffer */ * buffer */
bytes_at_end = ((&_pktbuf[0] + GNRC_PKTBUF_SIZE) - (((uint8_t *)new) + new->size)); bytes_at_end = ((&_pktbuf[0] + GNRC_PKTBUF_SIZE) - (((uint8_t *)new) + new->size));
if (bytes_at_end < _align(sizeof(_unused_t))) { if (bytes_at_end < sizeof(_unused_t)) {
/* new is very last segment and there is a little bit of memory left /* new is very last segment and there is a little bit of memory left
* that wouldn't fit _unused_t (cut of in _pktbuf_alloc()) => re-add it */ * that wouldn't fit _unused_t (cut of in _pktbuf_alloc()) => re-add it */
new->size += bytes_at_end; new->size += bytes_at_end;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment