kopia lustrzana https://github.com/espressif/esp-idf
164 wiersze
5.4 KiB
C
164 wiersze
5.4 KiB
C
/*
|
|
** Two Level Segregated Fit memory allocator, version 3.1.
|
|
** Written by Matthew Conte
|
|
** http://tlsf.baisoku.org
|
|
**
|
|
** Based on the original documentation by Miguel Masmano:
|
|
** http://www.gii.upv.es/tlsf/main/docs
|
|
**
|
|
** This implementation was written to the specification
|
|
** of the document, therefore no GPL restrictions apply.
|
|
**
|
|
** Copyright (c) 2006-2016, Matthew Conte
|
|
** All rights reserved.
|
|
**
|
|
** Redistribution and use in source and binary forms, with or without
|
|
** modification, are permitted provided that the following conditions are met:
|
|
** * Redistributions of source code must retain the above copyright
|
|
** notice, this list of conditions and the following disclaimer.
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
** documentation and/or other materials provided with the distribution.
|
|
** * Neither the name of the copyright holder nor the
|
|
** names of its contributors may be used to endorse or promote products
|
|
** derived from this software without specific prior written permission.
|
|
**
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
** DISCLAIMED. IN NO EVENT SHALL MATTHEW CONTE BE LIABLE FOR ANY
|
|
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#pragma once
|
|
#include <assert.h>
|
|
#include <limits.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stddef.h>
|
|
#include "heap_tlsf_config.h"
|
|
|
|
#if defined(__cplusplus)
|
|
extern "C" {
|
|
#endif
|
|
|
|
/*
|
|
** Cast and min/max macros.
|
|
*/
|
|
#define tlsf_cast(t, exp) ((t) (exp))
|
|
#define tlsf_min(a, b) ((a) < (b) ? (a) : (b))
|
|
#define tlsf_max(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
/* A type used for casting when doing pointer arithmetic. */
|
|
typedef ptrdiff_t tlsfptr_t;
|
|
|
|
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;
|
|
|
|
/* 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;
|
|
|
|
#include "heap_tlsf_block_functions.h"
|
|
|
|
/* 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);
|
|
pool_t tlsf_get_pool(tlsf_t tlsf);
|
|
|
|
/* Add/remove memory pools. */
|
|
pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
|
|
void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
|
|
|
|
/* malloc/memalign/realloc/free replacements. */
|
|
void* tlsf_malloc(tlsf_t tlsf, size_t size);
|
|
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size);
|
|
void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t offset);
|
|
void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
|
|
void tlsf_free(tlsf_t tlsf, void* ptr);
|
|
|
|
/* Returns internal block size, not original request size */
|
|
size_t tlsf_block_size(void* ptr);
|
|
|
|
/* Overheads/limits of internal structures. */
|
|
size_t tlsf_size(tlsf_t tlsf);
|
|
size_t tlsf_align_size(void);
|
|
size_t tlsf_block_size_min(void);
|
|
size_t tlsf_block_size_max(tlsf_t tlsf);
|
|
size_t tlsf_pool_overhead(void);
|
|
size_t tlsf_alloc_overhead(void);
|
|
|
|
/**
|
|
* @brief Return the allocable size based on the size passed
|
|
* as parameter
|
|
*
|
|
* @param tlsf Pointer to the tlsf structure
|
|
* @param size The allocation size
|
|
* @return size_t The updated allocation size
|
|
*/
|
|
size_t tlsf_fit_size(tlsf_t tlsf, size_t size);
|
|
|
|
/* Debugging. */
|
|
typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
|
|
void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
|
|
/* Returns nonzero if any internal consistency check fails. */
|
|
int tlsf_check(tlsf_t tlsf);
|
|
int tlsf_check_pool(pool_t pool);
|
|
|
|
#if defined(__cplusplus)
|
|
};
|
|
#endif
|