Expand comments on padding used by nano_malloc
This patch adds further comments to nano-mallocr.c, to more comprehensively explain how padding works in the malloc_chunk structure. It was originally discussed in the following thread: https://sourceware.org/ml/newlib/2017/msg00031.html 2017-01-13 Joe Seymour <joe.s@somniumtech.com> newlib/ * libc/stdlib/nano-mallocr.c (malloc_chunk, get_chunk_from_ptr) (nano_malloc): Add comments.
This commit is contained in:
parent
677ffdc247
commit
c0ac2ea2b3
|
@ -123,19 +123,24 @@ typedef size_t malloc_size_t;
|
||||||
|
|
||||||
typedef struct malloc_chunk
|
typedef struct malloc_chunk
|
||||||
{
|
{
|
||||||
/* ------------------
|
/* --------------------------------------
|
||||||
* chunk->| size (4 bytes) |
|
* chunk->| size |
|
||||||
* ------------------
|
* --------------------------------------
|
||||||
* | Padding for |
|
* | Padding for alignment |
|
||||||
* | alignment |
|
* | This includes padding inserted by |
|
||||||
* | holding neg |
|
* | the compiler (to align fields) and |
|
||||||
* | offset to size |
|
* | explicit padding inserted by this |
|
||||||
* ------------------
|
* | implementation. If any explicit |
|
||||||
* mem_ptr->| point to next |
|
* | padding is being used then the |
|
||||||
* | free when freed|
|
* | sizeof (size) bytes at |
|
||||||
* | or data load |
|
* | mem_ptr - CHUNK_OFFSET must be |
|
||||||
* | when allocated |
|
* | initialized with the negative |
|
||||||
* ------------------
|
* | offset to size. |
|
||||||
|
* --------------------------------------
|
||||||
|
* mem_ptr->| When allocated: data |
|
||||||
|
* | When freed: pointer to next free |
|
||||||
|
* | chunk |
|
||||||
|
* --------------------------------------
|
||||||
*/
|
*/
|
||||||
/* size of the allocated payload area, including size before
|
/* size of the allocated payload area, including size before
|
||||||
CHUNK_OFFSET */
|
CHUNK_OFFSET */
|
||||||
|
@ -187,8 +192,13 @@ extern void * nano_pvalloc(RARG size_t s);
|
||||||
|
|
||||||
static inline chunk * get_chunk_from_ptr(void * ptr)
|
static inline chunk * get_chunk_from_ptr(void * ptr)
|
||||||
{
|
{
|
||||||
|
/* Assume that there is no explicit padding in the
|
||||||
|
chunk, and that the chunk starts at ptr - CHUNK_OFFSET. */
|
||||||
chunk * c = (chunk *)((char *)ptr - CHUNK_OFFSET);
|
chunk * c = (chunk *)((char *)ptr - CHUNK_OFFSET);
|
||||||
/* Skip the padding area */
|
|
||||||
|
/* c->size being negative indicates that there is explicit padding in
|
||||||
|
the chunk. In which case, c->size is currently the negative offset to
|
||||||
|
the true size. */
|
||||||
if (c->size < 0) c = (chunk *)((char *)c + c->size);
|
if (c->size < 0) c = (chunk *)((char *)c + c->size);
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -314,6 +324,19 @@ void * nano_malloc(RARG malloc_size_t s)
|
||||||
|
|
||||||
if (offset)
|
if (offset)
|
||||||
{
|
{
|
||||||
|
/* Initialize sizeof (malloc_chunk.size) bytes at
|
||||||
|
align_ptr - CHUNK_OFFSET with negative offset to the
|
||||||
|
size field (at the start of the chunk).
|
||||||
|
|
||||||
|
The negative offset to size from align_ptr - CHUNK_OFFSET is
|
||||||
|
the size of any remaining padding minus CHUNK_OFFSET. This is
|
||||||
|
equivalent to the total size of the padding, because the size of
|
||||||
|
any remaining padding is the total size of the padding minus
|
||||||
|
CHUNK_OFFSET.
|
||||||
|
|
||||||
|
Note that the size of the padding must be at least CHUNK_OFFSET.
|
||||||
|
|
||||||
|
The rest of the padding is not initialized. */
|
||||||
*(long *)((char *)r + offset) = -offset;
|
*(long *)((char *)r + offset) = -offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue