Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

rtl_malloc.h

Go to the documentation of this file.
00001 /*
00002  * Two Levels Segregate Fit memory allocator (TLSF)
00003  * Version 1.1
00004  *
00005  * Written by Miguel Masmano Tello <mmasmano@disca.upv.es>
00006  * Copyright (C) Dec, 2002 OCERA Consortium
00007  * Release under the terms of the GNU General Public License Version 2
00008  *
00009  */
00010 
00011 #ifndef _THREE_LEVELS_SEGREGATE_FIT_MALLOC_H_
00012 #define _THREE_LEVELS_SEGREGATE_FIT_MALLOC_H_
00013 
00014 
00015 /*------------------------------------------------------------------------*/
00016 /******************************/
00017 /*  CONFIGURATION PARAMETERS  */
00018 /******************************/
00019 
00020 // The following parameters allows to tune TLSF
00021 
00022 /* TLSF_PREF defines the prefix 
00023    used by all TLSF functions */
00024 #define WITH_RTL_PREFIX
00025 
00026 /* 
00027  * MAX_FL_INDEX defines the maximum first index which
00028  * will be used by TLSF. The maximum first index is
00029  * calculated in the init_memory_pool (size)
00030  * 
00031  * if (log2 (size) <= MAX_FL_INDEX) then
00032  *   max_fl_index := log2 (size);
00033  * else
00034  *   max_fl_index := MAX_FL_INDEX;
00035  * end if;
00036  *
00037  */
00038 
00039 #define MAX_FL_INDEX 24 // TLSF default MAX_FL_INDEX is 16 MBytes
00040 
00041 /*------------------------------------------------------------------------*/
00042 
00043 #if defined(__KERNEL__) && !defined(__RTL__)
00044 #error "This modules can only be used in RTLinux or user space"
00045 #endif
00046 
00047 #include <linux/types.h>
00048 
00049 #define __u8 unsigned char
00050 #define __u16 unsigned short
00051 #define __u32 unsigned int
00052 #define __s8 char
00053 #define __s16 short
00054 #define __s32 int
00055 
00056 #ifdef __RTL__
00057 
00058 /* RTLinux module */
00059 
00060 #include <rtl.h>
00061 
00062 #define PRINT_MSG rtl_printf
00063 #define PRINT_DBG_C(message) rtl_printf(message)
00064 #define PRINT_DBG_D(message) rtl_printf("%i", message);
00065 //#define PRINT_DBG_F(message) rtl_printf("%f", message);
00066 #define PRINT_DBG_H(message) rtl_printf("%x", (unsigned int) message);
00067 
00068 #else
00069 
00070 /* User space */
00071 
00072 #include <stdlib.h>
00073 #include <stdio.h>
00074 #define PRINT_MSG printf
00075 #define PRINT_DBG_C(message) printf(message)
00076 #define PRINT_DBG_D(message) printf("%i", message);
00077 #define PRINT_DBG_F(message) printf("%f", message);
00078 #define PRINT_DBG_H(message) printf("%x", (unsigned int) message);
00079 
00080 #endif
00081 
00082 extern char *main_buffer; // This buffer is associated with 
00083                           // a block of memory by the user
00084 
00085 /*
00086  * associate  buffer allows  to indicate  to TLSF  that  one specific
00087  * buffer  must be  used by  default, this  allow to  the user  to use
00088  * malloc, free, calloc and realloc functions
00089  */
00090 
00091 #define associate_buffer(ptr) main_buffer = (char *) ptr;
00092 
00093 /*
00094  * max_sl_log2_index defines  the maximum  second index which  will be
00095  * used by TLSF.
00096  *
00097  * max_sl_log2_index allows  to the user to tune  the maximum internal
00098  * fragmentation, but  a high  max_sl_log2_index value will  cause big
00099  * TLSF structure.
00100  *
00101  * max_sl_log2_index  max. internal fragmentation (approximately)
00102  * -----------------  -------------------------------------------
00103  *     1                             25 %
00104  *     2                           12.5 %
00105  *     3                           6.25 %
00106  *     4                          3.125 %
00107  *     5                          1.563 %
00108  */
00109 
00110 // max_size is in Kbytes
00111 int init_memory_pool (int max_size, 
00112                       int max_sl_log2_index, char *block_ptr);
00113 
00114 void destroy_memory_pool (char *block_ptr);
00115 
00116 /* see man malloc */
00117 #ifdef WITH_RTL_PREFIX
00118 #define rtl_malloc(size) rtl_malloc_ex (size, main_buffer)
00119 void *rtl_malloc_ex (size_t size, char *block_ptr);
00120 #else
00121 #define malloc(size) malloc_ex (size, main_buffer)
00122 void *malloc_ex (size_t size, char *block_ptr);
00123 #endif
00124 
00125 /* see man realloc */
00126 #ifdef WITH_RTL_PREFIX
00127 #define rtl_realloc(p, new_len) rtl_realloc_ex (p, new_len, main_buffer) 
00128 void *rtl_realloc_ex (void *p, size_t new_len, char *block_ptr);
00129 #else
00130 #define realloc(p, new_len) realloc_ex (p, new_len, main_buffer)
00131 void *realloc_ex (void *p, size_t new_len, char *block_ptr);
00132 #endif
00133 
00134 /* see man calloc */
00135 #ifdef WITH_RTL_PREFIX
00136 #define rtl_calloc(nelem, elem_size) \
00137 rtl_calloc_ex (nelem, elem_size, main_buffer)
00138 void *rtl_calloc_ex (size_t nelem, size_t elem_size, char *block_ptr);
00139 #else
00140 #define calloc(nelem, elem_size) \
00141 calloc_ex (nelem, elem_size, main_buffer)
00142 void *calloc_ex (size_t nelem, size_t elem_size, char *block_ptr);
00143 #endif
00144 
00145 /*
00146  * see man free
00147  *
00148  * free () is only guaranteed  to work if ptr is the address
00149  * of a block allocated by rt_malloc() (and not yet freed).
00150  */
00151 
00152 #ifdef WITH_RTL_PREFIX
00153 #define rtl_free(ptr) rtl_free_ex (ptr, main_buffer)
00154 void rtl_free_ex (void *ptr, char *block_ptr);
00155 #else
00156 #define free(ptr) free_ex (ptr, main_buffer)
00157 void free_ex (void *ptr, char *block_ptr);
00158 #endif
00159 
00160 void free_blocks_status (char *block_ptr);
00161 void structure_status (char *block_ptr);
00162 
00163 #endif // #ifndef _THREE_LEVELS_SEGREGATE_FIT_MALLOC_H_

Generated on Wed Jan 14 12:58:57 2004 for RTL-lwIP-0.4 by doxygen 1.3.4