KallistiOS  2.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
malloc.h
Go to the documentation of this file.
1 /* KallistiOS 2.0.0
2 
3  malloc.h
4  Copyright (C)2003 Dan Potter
5 
6 */
7 
8 /** \file malloc.h
9  \brief Standard C Malloc functionality
10 
11  This implements standard C heap allocation, deallocation, and stats.
12 
13  \author Dan Potter
14 */
15 
16 #ifndef __MALLOC_H
17 #define __MALLOC_H
18 
19 #include <sys/cdefs.h>
20 __BEGIN_DECLS
21 
22 #include <arch/types.h>
23 
24 /* Unlike previous versions, we totally decouple the implementation from
25  the declarations. */
26 
27 /** \brief ANSI C functions */
28 struct mallinfo {
29  /** \brief non-mmapped space allocated from system */
30  int arena;
31  /** \brief number of free chunks */
32  int ordblks;
33  /** \brief number of fastbin blocks */
34  int smblks;
35  /** \brief number of mmapped regions */
36  int hblks;
37  /** \brief space in mmapped regions */
38  int hblkhd;
39  /** \brief maximum total allocated space */
40  int usmblks;
41  /** \brief space available in freed fastbin blocks */
42  int fsmblks;
43  /** \brief total allocated space */
44  int uordblks;
45  /** \brief total free space */
46  int fordblks;
47  /** \brief top-most, releasable (via malloc_trim) space */
48  int keepcost;
49 };
50 
51 /** \brief allocate memory
52 
53  This allocates the specified size of bytes onto the heap. This memory is not
54  freed automatically if the returned pointer goes out of scope. Thus you must
55  call \ref free to reclaim the used space when finished.
56 
57  \param size is the size in bytes to allocate
58  \return a pointer to the newly allocated address or NULL on errors.
59  \see free
60  \note the memory chunk is uninitialized
61  \note NULL may also be returned if size is 0
62 */
63 void * malloc(size_t size);
64 
65 /** \brief allocate memory on the heap and initialize it to 0
66 
67  This allocates a chunk of memory of size * nmemb. In otherwords, an array
68  with nmemb elements of size or size[nmemb].
69 
70  \param nmemb is the amount of elements
71  \param size the size of each element
72  \return a pointer to the newly allocated address or NULL on errors.
73  \see free
74  \note the memory chunk is set to zero
75  \note NULL may be returned if nmemb or size is 0
76 */
77 void * calloc(size_t nmemb, size_t size);
78 
79 /** \brief releases memory that was previous allocated
80 
81  frees the memory space that had previously been allocated by malloc or
82  calloc.
83 
84  \param ptr is a pointer to the address of allocated ram
85  \note no action is taken if NULL is passed
86  \note calling free on the same ptr more than once should not be expected to
87  behave in a reproducable maner as it is unpredictable.
88 */
89 void free(void * ptr);
90 
91 /** \brief changes the size of previously allocated memory
92 
93  The size of ptr is changed to size. If data has already been placed in the
94  memory area at that location, it's preserved up to size. If size is larger
95  then the previously allocated memory, the new area will be unititialized.
96 
97  \param ptr the address pointer that's been previously returned by malloc/calloc
98  \param size the new size to give to the memory
99  \return a pointer to the new address
100  \note if ptr is NULL the call is basically a malloc(size)
101  \note if ptr is not NULL, and size is 0 the call is basically a free(ptr)
102 */
103 void * realloc(void * ptr, size_t size);
104 
105 /** \brief allocate memory aligned memory
106 
107  Memory of size is allocated with the address being a multiple of alignment
108 
109  \param alignment a multiple of two that the memory address will be aligned to
110  \param size the size of the memory to allocate
111  \return a pointer to the newly allocated address (aligned to alignment) or
112  NULL on errors
113 */
114 void * memalign(size_t alignment, size_t size);
115 
116 /** \brief allocates memory aligned to the system page size
117 
118  Memory is allocated of size and is aligned to the system page size.
119  This ends up basically being: memolign(PAGESIZE, size)
120 
121  \param size the size of the memory to allocate
122  \return a pointer to the newly allocated memory address
123  \see arch/arch.h
124 */
125 void * valloc(size_t size);
126 
127 /** \brief Sets tunable parameters for malloc related options.
128 */
129 struct mallinfo mallinfo();
130 
131 /* mallopt defines */
132 #define M_MXFAST 1
133 #define DEFAULT_MXFAST 64
134 
135 #define M_TRIM_THRESHOLD -1
136 #define DEFAULT_TRIM_THRESHOLD (256*1024)
137 
138 #define M_TOP_PAD -2
139 #define DEFAULT_TOP_PAD 0
140 
141 #define M_MMAP_THRESHOLD -3
142 #define DEFAULT_MMAP_THRESHOLD (256*1024)
143 
144 #define M_MMAP_MAX -4
145 #define DEFAULT_MMAP_MAX 65536
146 int mallopt(int, int);
147 
148 /** \brief Debug function
149 */
150 void malloc_stats();
151 
152 /** \brief KOS specfic calls
153 */
154 int malloc_irq_safe();
155 
156 /** \brief Only available with KM_DBG
157 */
158 int mem_check_block(void *p);
159 
160 /** \brief Only available with KM_DBG
161  */
162 int mem_check_all();
163 
164 __END_DECLS
165 
166 #endif /* __MALLOC_H */
167