📄 rtl_malloc.h
字号:
/* * Two Levels Segregate Fit memory allocator (TLSF) * Version 1.1 * * Written by Miguel Masmano Tello <mmasmano@disca.upv.es> * Copyright (C) Dec, 2002 OCERA Consortium * Release under the terms of the GNU General Public License Version 2 * */#ifndef _THREE_LEVELS_SEGREGATE_FIT_MALLOC_H_#define _THREE_LEVELS_SEGREGATE_FIT_MALLOC_H_/*------------------------------------------------------------------------*//******************************//* CONFIGURATION PARAMETERS *//******************************/// The following parameters allows to tune TLSF/* TLSF_PREF defines the prefix used by all TLSF functions */#define WITH_RTL_PREFIX/* * MAX_FL_INDEX defines the maximum first index which * will be used by TLSF. The maximum first index is * calculated in the init_memory_pool (size) * * if (log2 (size) <= MAX_FL_INDEX) then * max_fl_index := log2 (size); * else * max_fl_index := MAX_FL_INDEX; * end if; * */#define MAX_FL_INDEX 24 // TLSF default MAX_FL_INDEX is 16 MBytes/*------------------------------------------------------------------------*/#if defined(__KERNEL__) && !defined(__RTL__)#error "This modules can only be used in RTLinux or user space"#endif#include <linux/types.h>#define __u8 unsigned char#define __u16 unsigned short#define __u32 unsigned int#define __s8 char#define __s16 short#define __s32 int#ifdef __RTL__/* RTLinux module */#include <rtl.h>#define PRINT_MSG rtl_printf#define PRINT_DBG_C(message) rtl_printf(message)#define PRINT_DBG_D(message) rtl_printf("%i", message);//#define PRINT_DBG_F(message) rtl_printf("%f", message);#define PRINT_DBG_H(message) rtl_printf("%x", (unsigned int) message);#else/* User space */#include <stdlib.h>#include <stdio.h>#define PRINT_MSG printf#define PRINT_DBG_C(message) printf(message)#define PRINT_DBG_D(message) printf("%i", message);#define PRINT_DBG_F(message) printf("%f", message);#define PRINT_DBG_H(message) printf("%x", (unsigned int) message);#endifextern char *main_buffer; // This buffer is associated with // a block of memory by the user/* * associate buffer allows to indicate to TLSF that one specific * buffer must be used by default, this allow to the user to use * malloc, free, calloc and realloc functions */#define associate_buffer(ptr) main_buffer = (char *) ptr;/* * max_sl_log2_index defines the maximum second index which will be * used by TLSF. * * max_sl_log2_index allows to the user to tune the maximum internal * fragmentation, but a high max_sl_log2_index value will cause big * TLSF structure. * * max_sl_log2_index max. internal fragmentation (approximately) * ----------------- ------------------------------------------- * 1 25 % * 2 12.5 % * 3 6.25 % * 4 3.125 % * 5 1.563 % */// max_size is in Kbytesint init_memory_pool (int max_size, int max_sl_log2_index, char *block_ptr);void destroy_memory_pool (char *block_ptr);/* see man malloc */#ifdef WITH_RTL_PREFIX#define rtl_malloc(size) rtl_malloc_ex (size, main_buffer)void *rtl_malloc_ex (size_t size, char *block_ptr);#else#define malloc(size) malloc_ex (size, main_buffer)void *malloc_ex (size_t size, char *block_ptr);#endif/* see man realloc */#ifdef WITH_RTL_PREFIX#define rtl_realloc(p, new_len) rtl_realloc_ex (p, new_len, main_buffer) void *rtl_realloc_ex (void *p, size_t new_len, char *block_ptr);#else#define realloc(p, new_len) realloc_ex (p, new_len, main_buffer)void *realloc_ex (void *p, size_t new_len, char *block_ptr);#endif/* see man calloc */#ifdef WITH_RTL_PREFIX#define rtl_calloc(nelem, elem_size) \rtl_calloc_ex (nelem, elem_size, main_buffer)void *rtl_calloc_ex (size_t nelem, size_t elem_size, char *block_ptr);#else#define calloc(nelem, elem_size) \calloc_ex (nelem, elem_size, main_buffer)void *calloc_ex (size_t nelem, size_t elem_size, char *block_ptr);#endif/* * see man free * * free () is only guaranteed to work if ptr is the address * of a block allocated by rt_malloc() (and not yet freed). */#ifdef WITH_RTL_PREFIX#define rtl_free(ptr) rtl_free_ex (ptr, main_buffer)void rtl_free_ex (void *ptr, char *block_ptr);#else#define free(ptr) free_ex (ptr, main_buffer)void free_ex (void *ptr, char *block_ptr);#endifvoid free_blocks_status (char *block_ptr);void structure_status (char *block_ptr);#endif // #ifndef _THREE_LEVELS_SEGREGATE_FIT_MALLOC_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -