1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
10 |
|
|
**************************************************************************/ |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/**************************************************************************/ |
15 |
|
|
/** */ |
16 |
|
|
/** USBX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** USBX main stack */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
/* Include necessary system files. */ |
25 |
|
|
|
26 |
|
|
#define UX_SOURCE_CODE |
27 |
|
|
|
28 |
|
|
#include "ux_api.h" |
29 |
|
|
|
30 |
|
|
|
31 |
|
|
/**************************************************************************/ |
32 |
|
|
/* */ |
33 |
|
|
/* FUNCTION RELEASE */ |
34 |
|
|
/* */ |
35 |
|
|
/* _ux_utility_memory_byte_pool_create PORTABLE C */ |
36 |
|
|
/* 6.3.0 */ |
37 |
|
|
/* AUTHOR */ |
38 |
|
|
/* */ |
39 |
|
|
/* Yajun Xia, Microsoft Corporation */ |
40 |
|
|
/* */ |
41 |
|
|
/* DESCRIPTION */ |
42 |
|
|
/* */ |
43 |
|
|
/* This function creates a pool of memory bytes in the specified */ |
44 |
|
|
/* memory area. */ |
45 |
|
|
/* */ |
46 |
|
|
/* INPUT */ |
47 |
|
|
/* */ |
48 |
|
|
/* pool_ptr Pointer to pool control block */ |
49 |
|
|
/* pool_start Address of beginning of pool area */ |
50 |
|
|
/* pool_size Number of bytes in the byte pool */ |
51 |
|
|
/* */ |
52 |
|
|
/* OUTPUT */ |
53 |
|
|
/* */ |
54 |
|
|
/* UX_SUCCESS Successful completion status */ |
55 |
|
|
/* */ |
56 |
|
|
/* CALLS */ |
57 |
|
|
/* */ |
58 |
|
|
/* None */ |
59 |
|
|
/* */ |
60 |
|
|
/* CALLED BY */ |
61 |
|
|
/* */ |
62 |
|
|
/* USBX Components */ |
63 |
|
|
/* */ |
64 |
|
|
/**************************************************************************/ |
65 |
|
416 |
UINT _ux_utility_memory_byte_pool_create(UX_MEMORY_BYTE_POOL *pool_ptr, VOID *pool_start, ULONG pool_size) |
66 |
|
|
{ |
67 |
|
|
|
68 |
|
|
UCHAR *block_ptr; |
69 |
|
|
UCHAR **block_indirect_ptr; |
70 |
|
|
UCHAR *temp_ptr; |
71 |
|
|
ALIGN_TYPE *free_ptr; |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
/* Initialize the byte pool control block to all zeros. */ |
75 |
|
416 |
_ux_utility_memory_set((UCHAR *)pool_ptr, 0, sizeof(UX_MEMORY_BYTE_POOL)); /* Use case of memset is verified. */ |
76 |
|
|
|
77 |
|
|
/* Round the pool size down to something that is evenly divisible by |
78 |
|
|
an ULONG. */ |
79 |
|
416 |
pool_size = (pool_size/(sizeof(ALIGN_TYPE))) * (sizeof(ALIGN_TYPE)); |
80 |
|
|
|
81 |
|
|
/* Save the start and size of the pool. */ |
82 |
|
416 |
pool_ptr -> ux_byte_pool_start = UX_VOID_TO_UCHAR_POINTER_CONVERT(pool_start); |
83 |
|
416 |
pool_ptr -> ux_byte_pool_size = pool_size; |
84 |
|
416 |
pool_ptr -> ux_byte_pool_search = UX_VOID_TO_UCHAR_POINTER_CONVERT(pool_start); |
85 |
|
|
|
86 |
|
|
/* Initially, the pool will have two blocks. One large block at the |
87 |
|
|
beginning that is available and a small allocated block at the end |
88 |
|
|
of the pool that is there just for the algorithm. Be sure to count |
89 |
|
|
the available block's header in the available bytes count. */ |
90 |
|
416 |
pool_ptr -> ux_byte_pool_available = pool_size - ((sizeof(VOID *)) + (sizeof(ALIGN_TYPE))); |
91 |
|
416 |
pool_ptr -> ux_byte_pool_fragments = ((UINT) 2); |
92 |
|
|
|
93 |
|
|
/* Each block contains a "next" pointer that points to the next block in the pool followed by a ALIGN_TYPE |
94 |
|
|
field that contains either the constant UX_BYTE_BLOCK_FREE (if the block is free) or a pointer to the |
95 |
|
|
owning pool (if the block is allocated). */ |
96 |
|
|
|
97 |
|
|
/* Calculate the end of the pool's memory area. */ |
98 |
|
416 |
block_ptr = UX_VOID_TO_UCHAR_POINTER_CONVERT(pool_start); |
99 |
|
416 |
block_ptr = UX_UCHAR_POINTER_ADD(block_ptr, pool_size); |
100 |
|
|
|
101 |
|
|
/* Backup the end of the pool pointer and build the pre-allocated block. */ |
102 |
|
416 |
block_ptr = UX_UCHAR_POINTER_SUB(block_ptr, (sizeof(ALIGN_TYPE))); |
103 |
|
|
|
104 |
|
|
/* Cast the pool pointer into a ULONG. */ |
105 |
|
416 |
temp_ptr = UX_BYTE_POOL_TO_UCHAR_POINTER_CONVERT(pool_ptr); |
106 |
|
416 |
block_indirect_ptr = UX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT(block_ptr); |
107 |
|
416 |
*block_indirect_ptr = temp_ptr; |
108 |
|
|
|
109 |
|
416 |
block_ptr = UX_UCHAR_POINTER_SUB(block_ptr, (sizeof(UCHAR *))); |
110 |
|
416 |
block_indirect_ptr = UX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT(block_ptr); |
111 |
|
416 |
*block_indirect_ptr = UX_VOID_TO_UCHAR_POINTER_CONVERT(pool_start); |
112 |
|
|
|
113 |
|
|
/* Now setup the large available block in the pool. */ |
114 |
|
416 |
temp_ptr = UX_VOID_TO_UCHAR_POINTER_CONVERT(pool_start); |
115 |
|
416 |
block_indirect_ptr = UX_UCHAR_TO_INDIRECT_UCHAR_POINTER_CONVERT(temp_ptr); |
116 |
|
416 |
*block_indirect_ptr = block_ptr; |
117 |
|
416 |
block_ptr = UX_VOID_TO_UCHAR_POINTER_CONVERT(pool_start); |
118 |
|
416 |
block_ptr = UX_UCHAR_POINTER_ADD(block_ptr, (sizeof(UCHAR *))); |
119 |
|
416 |
free_ptr = UX_UCHAR_TO_ALIGN_TYPE_POINTER_CONVERT(block_ptr); |
120 |
|
416 |
*free_ptr = UX_BYTE_BLOCK_FREE; |
121 |
|
|
|
122 |
|
|
/* Return UX_SUCCESS. */ |
123 |
|
416 |
return(UX_SUCCESS); |
124 |
|
|
} |