]> git.sur5r.net Git - freertos/blob - Demo/uIP_Demo_Rowley_ARM7/uip/memb.c
Renamed the CORTEX_M4_ATSAM4S_AVR_Studio directory to the correct CORTEX_M4_ATSAM4S_A...
[freertos] / Demo / uIP_Demo_Rowley_ARM7 / uip / memb.c
1 /**\r
2  * \addtogroup exampleapps\r
3  * @{\r
4  */\r
5 \r
6 /**\r
7  * \file\r
8  * Memory block allocation routines.\r
9  * \author Adam Dunkels <adam@sics.se>\r
10  *\r
11  * The memory block allocation routines provide a simple yet powerful\r
12  * set of functions for managing a set of memory blocks of fixed\r
13  * size. A set of memory blocks is statically declared with the\r
14  * MEMB() macro. Memory blocks are allocated from the declared\r
15  * memory by the memb_alloc() function, and are deallocated with the\r
16  * memb_free() function.\r
17  *\r
18  * \note Because of namespace clashes only one MEMB() can be\r
19  * declared per C module, and the name scope of a MEMB() memory\r
20  * block is local to each C module.\r
21  *\r
22  * The following example shows how to declare and use a memory block\r
23  * called "cmem" which has 8 chunks of memory with each memory chunk\r
24  * being 20 bytes large.\r
25  *\r
26  \code\r
27  MEMB(cmem, 20, 8);\r
28 \r
29  int main(int argc, char *argv[]) {\r
30     char *ptr;\r
31     \r
32     memb_init(&cmem);\r
33 \r
34     ptr = memb_alloc(&cmem);\r
35 \r
36     if(ptr != NULL) {\r
37        do_something(ptr);\r
38     } else {\r
39        printf("Could not allocate memory.\n");\r
40     }\r
41 \r
42     if(memb_free(ptr) == 0) {\r
43        printf("Deallocation succeeded.\n");\r
44     }\r
45  }\r
46  \endcode\r
47  * \r
48  */\r
49 \r
50 #include <string.h>\r
51 \r
52 #include "memb.h"\r
53 \r
54 /*------------------------------------------------------------------------------*/\r
55 /**\r
56  * Initialize a memory block that was declared with MEMB().\r
57  *\r
58  * \param m A memory block previosly declared with MEMB().\r
59  */\r
60 /*------------------------------------------------------------------------------*/\r
61 void\r
62 memb_init(struct memb_blocks *m)\r
63 {\r
64   memset(m->mem, (m->size + 1) * m->num, 0);\r
65 }\r
66 /*------------------------------------------------------------------------------*/\r
67 /**\r
68  * Allocate a memory block from a block of memory declared with MEMB().\r
69  *\r
70  * \param m A memory block previosly declared with MEMB().\r
71  */\r
72 /*------------------------------------------------------------------------------*/\r
73 char *\r
74 memb_alloc(struct memb_blocks *m)\r
75 {\r
76   int i;\r
77   char *ptr;\r
78 \r
79   ptr = m->mem;\r
80   for(i = 0; i < m->num; ++i) {\r
81     if(*ptr == 0) {\r
82       /* If this block was unused, we increase the reference count to\r
83          indicate that it now is used and return a pointer to the\r
84          first byte following the reference counter. */\r
85       ++*ptr;\r
86       return ptr + 1;\r
87     }\r
88     ptr += m->size + 1;\r
89   }\r
90 \r
91   /* No free block was found, so we return NULL to indicate failure to\r
92      allocate block. */\r
93   return NULL;\r
94 }\r
95 /*------------------------------------------------------------------------------*/\r
96 /**\r
97  * Deallocate a memory block from a memory block previously declared\r
98  * with MEMB().\r
99  *\r
100  * \param m m A memory block previosly declared with MEMB().\r
101  *\r
102  * \param ptr A pointer to the memory block that is to be deallocated.\r
103  *\r
104  * \return The new reference count for the memory block (should be 0\r
105  * if successfully deallocated) or -1 if the pointer "ptr" did not\r
106  * point to a legal memory block.\r
107  */\r
108 /*------------------------------------------------------------------------------*/\r
109 char\r
110 memb_free(struct memb_blocks *m, char *ptr)\r
111 {\r
112   int i;\r
113   char *ptr2;\r
114 \r
115   /* Walk through the list of blocks and try to find the block to\r
116      which the pointer "ptr" points to. */\r
117   ptr2 = m->mem;\r
118   for(i = 0; i < m->num; ++i) {\r
119     \r
120     if(ptr2 == ptr - 1) {\r
121       /* We've found to block to which "ptr" points so we decrease the\r
122          reference count and return the new value of it. */      \r
123       return --*ptr2;\r
124     }\r
125     ptr2 += m->size + 1;\r
126   }\r
127   return -1;\r
128 }\r
129 /*------------------------------------------------------------------------------*/\r
130 /**\r
131  * Increase the reference count for a memory chunk.\r
132  *\r
133  * \note No sanity checks are currently made.\r
134  *\r
135  * \param m m A memory block previosly declared with MEMB().\r
136  *\r
137  * \param ptr A pointer to the memory chunk for which the reference\r
138  * count should be increased.\r
139  *\r
140  * \return The new reference count.\r
141  */\r
142 /*------------------------------------------------------------------------------*/\r
143 char\r
144 memb_ref(struct memb_blocks *m, char *ptr)\r
145 {\r
146   return ++*(ptr - 1);\r
147 }\r
148 /*------------------------------------------------------------------------------*/\r
149 \r
150 \r
151 \r
152 \r