Skip to content

Commit

Permalink
Merge branch 'fix/rename-include-folder-to-private' into 'idf'
Browse files Browse the repository at this point in the history
feat(tlsf): Update the architecture to keep only public headers in the include folder

See merge request espressif/tlsf!14
  • Loading branch information
SoucheSouche committed Sep 16, 2024
2 parents 64170ec + bc61bdf commit ba64d19
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 142 deletions.
7 changes: 6 additions & 1 deletion include/tlsf.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
#ifndef INCLUDED_tlsf
#define INCLUDED_tlsf

#include <assert.h>
#include <stddef.h>
#include <stdbool.h>
#include "tlsf_common.h"

#if defined(__cplusplus)
extern "C" {
#endif

/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
/* pool_t: a block of memory that TLSF can manage. */
typedef void* tlsf_t;
typedef void* pool_t;

/* Create/destroy a memory pool. */
tlsf_t tlsf_create(void* mem, size_t max_bytes);
tlsf_t tlsf_create_with_pool(void* mem, size_t pool_bytes, size_t max_bytes);
Expand Down
133 changes: 0 additions & 133 deletions include/tlsf_common.h

This file was deleted.

1 change: 0 additions & 1 deletion tlsf.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <limits.h>
#include <stdio.h>
#include "tlsf.h"
#include "tlsf_common.h"
#include "tlsf_block_functions.h"
#include "tlsf_control_functions.h"

Expand Down
69 changes: 68 additions & 1 deletion include/tlsf_block_functions.h → tlsf_block_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,79 @@
*/

#pragma once
#include "tlsf_common.h"

#if defined(__cplusplus)
extern "C" {
#endif

/*
** Constants definition for poisoning.
** These defines are used as 3rd argument of tlsf_poison_fill_region() for readability purposes.
*/
#define POISONING_AFTER_FREE true
#define POISONING_AFTER_MALLOC !POISONING_AFTER_FREE

/* A type used for casting when doing pointer arithmetic. */
typedef ptrdiff_t tlsfptr_t;

/*
** Cast and min/max macros.
*/
#if !defined (tlsf_cast)
#define tlsf_cast(t, exp) ((t) (exp))
#endif
#if !defined (tlsf_min)
#define tlsf_min(a, b) ((a) < (b) ? (a) : (b))
#endif
#if !defined (tlsf_max)
#define tlsf_max(a, b) ((a) > (b) ? (a) : (b))
#endif

/*
** Set assert macro, if it has not been provided by the user.
*/
#if !defined (tlsf_assert)
#define tlsf_assert assert
#endif

typedef struct block_header_t
{
/* Points to the previous physical block. */
struct block_header_t* prev_phys_block;

/* The size of this block, excluding the block header. */
size_t size;

/* Next and previous free blocks. */
struct block_header_t* next_free;
struct block_header_t* prev_free;
} block_header_t;

/* User data starts directly after the size field in a used block. */
#define block_start_offset (offsetof(block_header_t, size) + sizeof(size_t))

/*
** A free block must be large enough to store its header minus the size of
** the prev_phys_block field, and no larger than the number of addressable
** bits for FL_INDEX.
*/
#define block_size_min (sizeof(block_header_t) - sizeof(block_header_t*))

/*
** Since block sizes are always at least a multiple of 4, the two least
** significant bits of the size field are used to store the block status:
** - bit 0: whether block is busy or free
** - bit 1: whether previous block is busy or free
*/
#define block_header_free_bit (1UL << 0)
#define block_header_prev_free_bit (1UL << 1)

/*
** The size of the block header exposed to used blocks is the size field.
** The prev_phys_block field is stored *inside* the previous free block.
*/
#define block_header_overhead (sizeof(size_t))

/*
** block_header_t member functions.
*/
Expand Down
52 changes: 46 additions & 6 deletions include/tlsf_control_functions.h → tlsf_control_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

#pragma once
#include "tlsf_common.h"
#include "tlsf_block_functions.h"

#if defined(__cplusplus)
Expand All @@ -15,6 +14,48 @@ extern "C" {
#define tlsf_decl static inline __attribute__((always_inline))
#endif

enum tlsf_config
{
/* All allocation sizes and addresses are aligned to 4 bytes. */
ALIGN_SIZE_LOG2 = 2,
ALIGN_SIZE = (1 << ALIGN_SIZE_LOG2),
};

/* The TLSF control structure. */
typedef struct control_t
{
/* Empty lists point at this block to indicate they are free. */
block_header_t block_null;

/* Local parameter for the pool. Given the maximum
* value of each field, all the following parameters
* can fit on 4 bytes when using bitfields
*/
unsigned int fl_index_count : 5; // 5 cumulated bits
unsigned int fl_index_shift : 3; // 8 cumulated bits
unsigned int fl_index_max : 6; // 14 cumulated bits
unsigned int sl_index_count : 6; // 20 cumulated bits

/* log2 of number of linear subdivisions of block sizes. Larger
** values require more memory in the control structure. Values of
** 4 or 5 are typical.
*/
unsigned int sl_index_count_log2 : 3; // 23 cumulated bits
unsigned int small_block_size : 8; // 31 cumulated bits

/* size of the metadata ( size of control block,
* sl_bitmap and blocks )
*/
size_t size;

/* Bitmaps for free lists. */
unsigned int fl_bitmap;
unsigned int *sl_bitmap;

/* Head of free lists. */
block_header_t** blocks;
} control_t;

/*
** Architecture-specific bit manipulation routines.
**
Expand Down Expand Up @@ -227,29 +268,28 @@ tlsf_decl size_t tlsf_block_size_min(void)
return block_size_min;
}

tlsf_decl size_t tlsf_block_size_max(tlsf_t tlsf)
tlsf_decl size_t tlsf_block_size_max(control_t *control)
{
if (tlsf == NULL)
if (control == NULL)
{
return 0;
}
control_t* control = tlsf_cast(control_t*, tlsf);
return tlsf_cast(size_t, 1) << control->fl_index_max;
}

/*
** Adjust an allocation size to be aligned to word size, and no smaller
** than internal minimum.
*/
tlsf_decl size_t adjust_request_size(tlsf_t tlsf, size_t size, size_t align)
tlsf_decl size_t adjust_request_size(control_t *control, size_t size, size_t align)
{
size_t adjust = 0;
if (size)
{
const size_t aligned = align_up(size, align);

/* aligned sized must not exceed block_size_max or we'll go out of bounds on sl_bitmap */
if (aligned < tlsf_block_size_max(tlsf))
if (aligned < tlsf_block_size_max(control))
{
adjust = tlsf_max(aligned, block_size_min);
}
Expand Down

0 comments on commit ba64d19

Please sign in to comment.