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
24 #if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
25 #define SYSINIT_MSYS_1_MEMBLOCK_SIZE \
26 OS_ALIGN(MYNEWT_VAL(MSYS_1_BLOCK_SIZE), OS_ALIGNMENT)
27 #define SYSINIT_MSYS_1_MEMPOOL_SIZE \
28 OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_1_BLOCK_COUNT), \
29 SYSINIT_MSYS_1_MEMBLOCK_SIZE)
30 static os_membuf_t os_msys_init_1_data[SYSINIT_MSYS_1_MEMPOOL_SIZE];
31 static struct os_mbuf_pool os_msys_init_1_mbuf_pool;
32 static struct os_mempool os_msys_init_1_mempool;
33 #endif
34
35 #if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
36 #define SYSINIT_MSYS_2_MEMBLOCK_SIZE \
37 OS_ALIGN(MYNEWT_VAL(MSYS_2_BLOCK_SIZE), OS_ALIGNMENT)
38 #define SYSINIT_MSYS_2_MEMPOOL_SIZE \
39 OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_2_BLOCK_COUNT), \
40 SYSINIT_MSYS_2_MEMBLOCK_SIZE)
41 static os_membuf_t os_msys_init_2_data[SYSINIT_MSYS_2_MEMPOOL_SIZE];
42 static struct os_mbuf_pool os_msys_init_2_mbuf_pool;
43 static struct os_mempool os_msys_init_2_mempool;
44 #endif
45
46 static void
os_msys_init_once(void * data,struct os_mempool * mempool,struct os_mbuf_pool * mbuf_pool,int block_count,int block_size,char * name)47 os_msys_init_once(void *data, struct os_mempool *mempool,
48 struct os_mbuf_pool *mbuf_pool,
49 int block_count, int block_size, char *name)
50 {
51 int rc;
52
53 rc = mem_init_mbuf_pool(data, mempool, mbuf_pool, block_count, block_size,
54 name);
55 assert(rc == 0);
56
57 rc = os_msys_register(mbuf_pool);
58 assert(rc == 0);
59 }
60
61 void
os_msys_init(void)62 os_msys_init(void)
63 {
64 os_msys_reset();
65
66 (void)os_msys_init_once;
67 #if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
68 os_msys_init_once(os_msys_init_1_data,
69 &os_msys_init_1_mempool,
70 &os_msys_init_1_mbuf_pool,
71 MYNEWT_VAL(MSYS_1_BLOCK_COUNT),
72 SYSINIT_MSYS_1_MEMBLOCK_SIZE,
73 "msys_1");
74 #endif
75
76 #if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
77 os_msys_init_once(os_msys_init_2_data,
78 &os_msys_init_2_mempool,
79 &os_msys_init_2_mbuf_pool,
80 MYNEWT_VAL(MSYS_2_BLOCK_COUNT),
81 SYSINIT_MSYS_2_MEMBLOCK_SIZE,
82 "msys_2");
83 #endif
84 }
85