1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <assert.h>
21 #include "os/os.h"
22 #include "mem/mem.h"
23 #include "wm_mem.h"
24
25 #if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
26 #define SYSINIT_MSYS_1_MEMBLOCK_SIZE \
27 OS_ALIGN(MYNEWT_VAL(MSYS_1_BLOCK_SIZE), OS_ALIGNMENT)
28 #define SYSINIT_MSYS_1_MEMPOOL_SIZE \
29 OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_1_BLOCK_COUNT), \
30 SYSINIT_MSYS_1_MEMBLOCK_SIZE)
31 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
32 static os_membuf_t *os_msys_init_1_data = NULL;
33 #else
34 static os_membuf_t os_msys_init_1_data[SYSINIT_MSYS_1_MEMPOOL_SIZE];
35 #endif
36
37 static struct os_mbuf_pool os_msys_init_1_mbuf_pool;
38 static struct os_mempool os_msys_init_1_mempool;
39 #endif
40
41 #if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
42 #define SYSINIT_MSYS_2_MEMBLOCK_SIZE \
43 OS_ALIGN(MYNEWT_VAL(MSYS_2_BLOCK_SIZE), OS_ALIGNMENT)
44 #define SYSINIT_MSYS_2_MEMPOOL_SIZE \
45 OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_2_BLOCK_COUNT), \
46 SYSINIT_MSYS_2_MEMBLOCK_SIZE)
47
48 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
49 static os_membuf_t *os_msys_init_2_data = NULL;
50 #else
51 static os_membuf_t os_msys_init_2_data[SYSINIT_MSYS_2_MEMPOOL_SIZE];
52 #endif
53
54 static struct os_mbuf_pool os_msys_init_2_mbuf_pool;
55 static struct os_mempool os_msys_init_2_mempool;
56 #endif
57
os_msys_init_once(void * data,struct os_mempool * mempool,struct os_mbuf_pool * mbuf_pool,int block_count,int block_size,char * name)58 static void os_msys_init_once(void *data, struct os_mempool *mempool,
59 struct os_mbuf_pool *mbuf_pool,
60 int block_count, int block_size, char *name)
61 {
62 int rc;
63 rc = mem_init_mbuf_pool(data, mempool, mbuf_pool, block_count, block_size,
64 name);
65 assert(rc == 0);
66 rc = os_msys_register(mbuf_pool);
67 assert(rc == 0);
68 }
69
os_msys_init(void)70 void os_msys_init(void)
71 {
72 os_msys_reset();
73 (void)os_msys_init_once;
74 #if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
75 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
76 os_msys_init_1_data = (os_membuf_t *)tls_mem_alloc(sizeof(os_membuf_t) *
77 SYSINIT_MSYS_1_MEMPOOL_SIZE);
78 assert(os_msys_init_1_data != NULL);
79 #endif
80 os_msys_init_once(os_msys_init_1_data,
81 &os_msys_init_1_mempool,
82 &os_msys_init_1_mbuf_pool,
83 MYNEWT_VAL(MSYS_1_BLOCK_COUNT),
84 SYSINIT_MSYS_1_MEMBLOCK_SIZE,
85 "msys_1");
86 #endif
87 #if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
88 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
89 os_msys_init_2_data = (os_membuf_t *)tls_mem_alloc(sizeof(os_membuf_t) *
90 SYSINIT_MSYS_2_MEMPOOL_SIZE);
91 assert(os_msys_init_2_data != NULL);
92 #endif
93 os_msys_init_once(os_msys_init_2_data,
94 &os_msys_init_2_mempool,
95 &os_msys_init_2_mbuf_pool,
96 MYNEWT_VAL(MSYS_2_BLOCK_COUNT),
97 SYSINIT_MSYS_2_MEMBLOCK_SIZE,
98 "msys_2");
99 #endif
100 }
os_msys_deinit(void)101 void os_msys_deinit(void)
102 {
103 #if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
104 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
105
106 if (os_msys_init_1_data) {
107 tls_mem_free(os_msys_init_1_data);
108 os_msys_init_1_data = NULL;
109 }
110
111 #endif
112 #endif
113 #if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
114 #if MYNEWT_VAL(SYS_MEM_DYNAMIC)
115
116 if (os_msys_init_2_data) {
117 tls_mem_free(os_msys_init_2_data);
118 os_msys_init_2_data = NULL;
119 }
120
121 #endif
122 #endif
123 }