1 /**
2  * \file
3  *
4  * \brief Memory Bag allocator for 8-bit AVR, 32-bit AVR, SAM
5  *
6  * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 
47 #ifndef UTILS_MEMBAG_H
48 #define UTILS_MEMBAG_H
49 
50 #include <stddef.h>
51 
52 /**
53  * \defgroup membag_group Memory Bag Allocator
54  *
55  * The Membag allocator is a optimized, fragmentationless general purpose
56  * memory allocator utility module designed to replace the standard C library
57  * \c malloc() and \c free() functions in resource constrained environments.
58  *
59  *
60  * Internally, the Membag allocator uses several user defined "bags" of one or
61  * more fixed-size blocks to form a memory pool for use in a user application.
62  * When an allocation is requested, the Membag module will search all available
63  * bags and find the smallest unallocated block of sufficient size, and return
64  * this block to the calling function. The size of each bag and number of blocks
65  * in each bag is user configurable, via the \ref conf_membag.h header file
66  * added to the user project.
67  *
68  * The allocator also has basic statistics functionality to obtain the size of
69  * the entire memory pool, amount of free memory, and the size of the smallest
70  * and largest free memory block.
71  *
72  * The memory bag allocator always allocates memory as a block from a fixed size
73  * bag/pool. This helps reduce memory fragmentation compared to an generic
74  * allocator that gives exactly the bytes requested. While this gives a
75  * trade-off of the maximum number of concurrent allocations and the size of
76  * allocations that are allowable, the allocator prevents memory fragmentation
77  * from occuring in an embedded application.
78  *
79  * The allocation and deallocation of memory with the Membag module is
80  * non-deterministic, however the module functions all have a maximum run time
81  * that is dependent on the number of bags that have been configured.
82  *
83  * @{
84  */
85 
86 /**
87  * Macro used to create memory bags in conf_membag.h
88  *
89  * \note Multiple bags of the same size are allowed, if more than 32 bags of a
90  *       given size are required in an application.
91  *
92  * \param objsize  Size of each block in the bag
93  * \param nr_objs  Number of blocks in the bag, a value less than 32
94  */
95 #define MEMBAG(objsize, nr_objs)\
96 	{ .block_size = objsize, .num_blocks = nr_objs }
97 
98 /**
99  * Macro used to store the size of the membags in conf_membag.h
100  *
101  * \note Multiple bags of the same size are allowed, if more than 32 bags of a
102  *       given size are required in an application.
103  *
104  * \param objsize  Size of each block in the bag
105  * \param nr_objs  Number of blocks in the bag, a value less than 32
106  */
107 #define MEMBAG_SIZE(objsize, nr_objs)\
108 	(objsize * nr_objs)
109 
110 void membag_init(void);
111 
112 size_t membag_get_total(void);
113 size_t membag_get_total_free(void);
114 
115 size_t membag_get_smallest_free_block_size(void);
116 size_t membag_get_largest_free_block_size(void);
117 
118 void *membag_alloc(const size_t size);
119 void membag_free(const void *ptr);
120 
121 /** @} */
122 
123 #endif /* UTILS_MEMBAG_H */
124