1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdint.h>
8
9 #include <aos/kernel.h>
10
11 #if AOS_COMP_CLI
12 #include "aos/cli.h"
13 #endif
14
15 /**
16 * The use case allocates the same memory in three different ways.
17 */
18
19 /* module name used by ulog */
20 #define MODULE_NAME "aos_mem_example"
21
22 /* four of these types of structures are allocated in the use case */
23 struct message {
24 uint8_t type;
25 uint16_t length;
26 uint8_t data[10];
27 };
28
mem_alloc(void)29 void mem_alloc(void)
30 {
31 struct message *messages_ptr = NULL;
32
33 /* way-1 */
34 /* allocate four message structures using aos_malloc */
35 messages_ptr = (struct message *)aos_malloc(sizeof(struct message) * 4);
36 /**
37 * after allocating memory, you need to determine whether the return value is NULL.
38 * NULL means the allocation failed.
39 */
40 if (messages_ptr == NULL) {
41 printf("[%s]aos_malloc error\n", MODULE_NAME);
42 return;
43 }
44
45 printf("[%s]aos_malloc success!\n", MODULE_NAME);
46
47 /* all newly allocated memory areas are set to 0 */
48 memset(messages_ptr, 0, sizeof(struct message) * 4);
49
50 /* free memory when memory is used up */
51 aos_free(messages_ptr);
52 messages_ptr = NULL;
53
54
55 /* way-2 */
56 /* allocate four message structures using aos_zalloc. This function clears memory automatically. */
57 messages_ptr = (struct message *)aos_zalloc(sizeof(struct message) * 4);
58 /**
59 * after allocating memory, you need to determine whether the return value is NULL.
60 * NULL means the allocation failed.
61 */
62 if (messages_ptr == NULL) {
63 printf("[%s]aos_zalloc error\n", MODULE_NAME);
64 return;
65 }
66
67 printf("[%s]aos_zalloc success!\n", MODULE_NAME);
68
69 /* free memory when memory is used up */
70 aos_free(messages_ptr);
71 messages_ptr = NULL;
72
73
74 /* way-3 */
75 /* allocate four message structures using aos_calloc. This function clears memory automatically.*/
76 messages_ptr = (struct message *)aos_calloc(4, sizeof(struct message));
77 /**
78 * after allocating memory, you need to determine whether the return value is NULL.
79 * NULL means the allocation failed.
80 */
81 if (messages_ptr == NULL) {
82 printf("[%s]aos_calloc error\n", MODULE_NAME);
83 return;
84 }
85
86 printf("[%s]aos_calloc success!\n", MODULE_NAME);
87
88 /* free memory when memory is used up */
89 aos_free(messages_ptr);
90 messages_ptr = NULL;
91 }
92
93 /**
94 * This use case demonstrates that the dynamically allocated memory is out of use
95 * and aos_realloc is called to expand the memory.
96 */
97
mem_realloc(void)98 void mem_realloc(void)
99 {
100 struct message *old_ptr = NULL, *new_ptr = NULL;
101
102 /* allocate four message structures using aos_malloc */
103 old_ptr = (struct message *)aos_malloc(sizeof(struct message) * 4);
104 /**
105 * after allocating memory, you need to determine whether the return value is NULL.
106 * NULL means the allocation failed.
107 */
108 if (old_ptr == NULL) {
109 printf("[%s]aos_malloc error\n", MODULE_NAME);
110 return;
111 }
112 /* all newly allocated memory areas are set to 0 */
113 memset(old_ptr, 0, sizeof(struct message) * 4);
114
115 /**
116 * in the process of use, it was found that 4 structures were not enough,
117 * so we need to expand to 6
118 */
119 new_ptr = aos_realloc(old_ptr, sizeof(struct message) * 6);
120 if (new_ptr == NULL) {
121 /* The memory pointed to by old_ptr is not automatically freed when aos_realloc fails */
122 aos_free(old_ptr);
123 old_ptr= NULL;
124 printf("[%s]aos_realloc task1 error\n", MODULE_NAME);
125 return;
126 }
127
128 printf("[%s]aos_realloc success!\n", MODULE_NAME);
129 /**
130 * When aos_realloc executes successfully, the contents of old_ptr are automatically copied
131 * to new_ptr, and old_ptr is freed, after which the user can use new_ptr's new memory.
132 */
133
134
135 /* free memory when memory is used up */
136 aos_free(new_ptr);
137 new_ptr = NULL;
138 }
139
aos_mem_example(int argc,char ** argv)140 static void aos_mem_example(int argc, char **argv)
141 {
142 mem_alloc();
143
144 mem_realloc();
145 }
146
147 #if AOS_COMP_CLI
148 /* reg args: fun, cmd, description*/
149 ALIOS_CLI_CMD_REGISTER(aos_mem_example, mem_example, aos mem example)
150 #endif
151
152