mirror of
https://github.com/espressif/tlsf.git
synced 2025-12-10 17:43:22 +00:00
Use "uintptr_t" for 64-bit targets
On 64-bit targets, casting from pointer to unsigned can lose data. Instead, cast to uintptr_t. This causes no code change on 32-bit platforms, since the types have the same width, but preserves all bits on systems where pointers are 64 bits.
This commit is contained in:
7
tlsf.c
7
tlsf.c
@@ -7,6 +7,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include "tlsf.h"
|
#include "tlsf.h"
|
||||||
#include "tlsf_common.h"
|
#include "tlsf_common.h"
|
||||||
#include "tlsf_block_functions.h"
|
#include "tlsf_block_functions.h"
|
||||||
@@ -1036,11 +1037,11 @@ void* tlsf_malloc_addr(tlsf_t tlsf, size_t size, void *address)
|
|||||||
control_t* control = tlsf_cast(control_t*, tlsf);
|
control_t* control = tlsf_cast(control_t*, tlsf);
|
||||||
|
|
||||||
/* adjust the address to be ALIGN_SIZE bytes aligned. */
|
/* adjust the address to be ALIGN_SIZE bytes aligned. */
|
||||||
const unsigned int addr_adjusted = align_down(tlsf_cast(unsigned int, address), ALIGN_SIZE);
|
const uintptr_t addr_adjusted = align_down(tlsf_cast(uintptr_t, address), ALIGN_SIZE);
|
||||||
|
|
||||||
/* adjust the size to be ALIGN_SIZE bytes aligned. Add to the size the difference
|
/* adjust the size to be ALIGN_SIZE bytes aligned. Add to the size the difference
|
||||||
* between the requested address and the address_adjusted. */
|
* between the requested address and the address_adjusted. */
|
||||||
size_t size_adjusted = align_up(size + (tlsf_cast(unsigned int, address) - addr_adjusted), ALIGN_SIZE);
|
size_t size_adjusted = align_up(size + (tlsf_cast(uintptr_t, address) - addr_adjusted), ALIGN_SIZE);
|
||||||
|
|
||||||
/* find the free block that starts before the address in the pool and is big enough
|
/* find the free block that starts before the address in the pool and is big enough
|
||||||
* to support the size of allocation at the given address */
|
* to support the size of allocation at the given address */
|
||||||
@@ -1080,7 +1081,7 @@ void* tlsf_malloc_addr(tlsf_t tlsf, size_t size, void *address)
|
|||||||
|
|
||||||
/* trim any leading space or add the leading space to the overall requested size
|
/* trim any leading space or add the leading space to the overall requested size
|
||||||
* if the leading space is not big enough to store a block of minimum size */
|
* if the leading space is not big enough to store a block of minimum size */
|
||||||
const size_t space_before_addr_adjusted = addr_adjusted - tlsf_cast(unsigned int, block_to_ptr(block));
|
const size_t space_before_addr_adjusted = addr_adjusted - tlsf_cast(uintptr_t, block_to_ptr(block));
|
||||||
block_header_t *return_block = block;
|
block_header_t *return_block = block;
|
||||||
if (space_before_addr_adjusted >= block_size_min) {
|
if (space_before_addr_adjusted >= block_size_min) {
|
||||||
return_block = block_trim_free_leading(control, block, space_before_addr_adjusted);
|
return_block = block_trim_free_leading(control, block, space_before_addr_adjusted);
|
||||||
|
|||||||
Reference in New Issue
Block a user