Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge branch 'master' into pr-umm_malloc-style
  • Loading branch information
mhightower83 authored Jan 3, 2022
commit a6a60f5bfa9be03081b16add8ed2528de0960b74
45 changes: 42 additions & 3 deletions cores/esp8266/umm_malloc/umm_malloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,24 @@ static uint16_t umm_blocks(size_t size) {
* When a block removed from the free list, the space used by the free
* pointers is available for data. That's what the first calculation
* of size is doing.
*
* We don't check for the special case of (size == 0) here as this needs
* special handling in the caller depending on context. For example when we
* realloc() a block to size 0 it should simply be freed.
*
* We do NOT need to check for allocating more blocks than the heap can
* possibly hold - the allocator figures this out for us.
*
* There are only two cases left to consider:
*
* 1. (size <= body) Obviously this is just one block
* 2. (blocks > (2^15)) This should return ((2^15)) to force a
* failure when the allocator runs
*
* If the requested size is greater that 32677-2 blocks (max block index
* minus the overhead of the top and bottom bookkeeping blocks) then we
* will return an incorrectly truncated value when the result is cast to
* a uint16_t.
*/

if (size <= (sizeof(((umm_block *)0)->body))) {
Expand All @@ -283,12 +301,33 @@ static uint16_t umm_blocks(size_t size) {

/*
* If it's for more than that, then we need to figure out the number of
* additional whole blocks the size of an umm_block are required.
* additional whole blocks the size of an umm_block are required, so
* reduce the size request by the number of bytes in the body of the
* first block.
*/

size -= (sizeof(((umm_block *)0)->body));

/* NOTE WELL that we take advantage of the fact that INT16_MAX is the
* number of blocks that we can index in 15 bits :-)
*
* The below expression looks wierd, but it's right. Assuming body
* size of 4 bytes and a block size of 8 bytes:
*
* BYTES (BYTES-BODY) (BYTES-BODY-1)/BLOCKSIZE BLOCKS
* 1 n/a n/a 1
* 5 1 0 2
* 12 8 0 2
* 13 9 1 3
*/

size -= (1 + (sizeof(((umm_block *)0)->body)));
size_t blocks = (2 + ((size - 1) / sizeof(umm_block)));

if (blocks > (INT16_MAX)) {
blocks = INT16_MAX;
}

return 2 + size / (sizeof(umm_block));
return (uint16_t)blocks;
}

/* ------------------------------------------------------------------------ */
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.