Mistake on this page?
Report an issue in GitHub or email us
emac_TestMemoryManager.h
1 /*
2  * Copyright (c) 2018, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef EMAC_TEST_MEMORY_MANAGER_H
19 #define EMAC_TEST_MEMORY_MANAGER_H
20 
21 #include <list>
22 
23 #include "EMACMemoryManager.h"
24 
25 #define MEM_CHECK 0x01
26 #define MEM_NO_ALIGN 0x02
27 
28 typedef struct emac_memory {
29  struct emac_memory *next;
30  void *buffer; /**< Pointer to allocated buffer */
31  unsigned int orig_len; /**< Original buffer length (set_len() does not change) */
32  unsigned int len; /**< Buffer length */
33  void *ptr; /**< Aligned pointer */
34  bool first;
36 
38 public:
39 
40  /**
41  * Creates emac test memory manager
42  */
44 
45  /*
46  * Gets static instance
47  */
48  static EmacTestMemoryManager &get_instance();
49 
50  /**
51  * Allocates memory buffer from the heap
52  *
53  * Memory buffer allocated from heap is always contiguous and can be arbitrary size.
54  *
55  * @param size Size of the memory to allocate in bytes
56  * @param align Memory alignment requirement in bytes
57  * @return Allocated memory buffer, or NULL in case of error
58  */
59  virtual emac_mem_buf_t *alloc_heap(uint32_t size, uint32_t align);
60 
61  /**
62  * Allocates memory buffer from the heap
63  *
64  * Memory buffer allocated from heap is always contiguous and can be arbitrary size.
65  *
66  * @param size Size of the memory to allocate in bytes
67  * @param align Memory alignment requirement in bytes
68  * @param opt Options
69  * @return Allocated memory buffer, or NULL in case of error
70  */
71  emac_mem_buf_t *alloc_heap(uint32_t size, uint32_t align, uint8_t opt);
72 
73  /**
74  * Allocates memory buffer chain from a pool
75  *
76  * Memory allocated from pool is contiguous if size is equal or less than
77  * (aligned) allocation unit, otherwise may be chained. Will typically come from
78  * fixed-size packet pool memory.
79  *
80  * @param size Total size of the memory to allocate in bytes
81  * @param align Memory alignment requirement for each buffer in bytes
82  * @return Allocated memory buffer chain, or NULL in case of error
83  */
84  virtual emac_mem_buf_t *alloc_pool(uint32_t size, uint32_t align);
85 
86  /**
87  * Allocates memory buffer chain from a pool
88  *
89  * Memory allocated from pool is contiguous if size is equal or less than
90  * (aligned) allocation unit, otherwise may be chained. Will typically come from
91  * fixed-size packet pool memory.
92  *
93  * @param size Total size of the memory to allocate in bytes
94  * @param align Memory alignment requirement for each buffer in bytes
95  * @param opt Options
96  * @return Allocated memory buffer chain, or NULL in case of error
97  */
98  emac_mem_buf_t *alloc_pool(uint32_t size, uint32_t align, uint8_t opt);
99 
100  /**
101  * Get memory buffer pool allocation unit
102  *
103  * Returns the maximum size of contiguous memory that can be allocated from a pool.
104  *
105  * @param align Memory alignment requirement in bytes
106  * @return Contiguous memory size
107  */
108  virtual uint32_t get_pool_alloc_unit(uint32_t align) const;
109 
110  /**
111  * Free memory buffer chain
112  *
113  * If memory buffer is chained must point to the start of the chain. Frees all buffers
114  * from the chained list.
115  *
116  * @param buf Memory buffer chain to be freed.
117  */
118  virtual void free(emac_mem_buf_t *buf);
119 
120  /**
121  * Return total length of a memory buffer chain
122  *
123  * Returns a total length of this buffer and any following buffers in the chain.
124  *
125  * @param buf Memory buffer chain
126  * @return Total length in bytes
127  */
128  virtual uint32_t get_total_len(const emac_mem_buf_t *buf) const;
129 
130  /**
131  * Copy a memory buffer chain
132  *
133  * Copies data from one buffer chain to another. Copy operation does not adjust the lengths
134  * of the copied-to memory buffer chain, so chain total lengths must be the same.
135  *
136  * @param to_buf Memory buffer chain to copy to
137  * @param from_buf Memory buffer chain to copy from
138  */
139  virtual void copy(emac_mem_buf_t *to_buf, const emac_mem_buf_t *from_buf);
140 
141  /**
142  * Concatenate two memory buffer chains
143  *
144  * Concatenates buffer chain to end of the other buffer chain. Concatenated-to buffer total length
145  * is adjusted accordingly. cat_buf must point to the start of a the chain. After concatenation
146  * to_buf's chain now owns those buffers, and they will be freed when the to_buf chain is freed.
147  *
148  * @param to_buf Memory buffer chain to concatenate to
149  * @param cat_buf Memory buffer chain to concatenate
150  */
151  virtual void cat(emac_mem_buf_t *to_buf, emac_mem_buf_t *cat_buf);
152 
153  /**
154  * Returns the next buffer
155  *
156  * Returns the next buffer from the memory buffer chain.
157  *
158  * @param buf Memory buffer
159  * @return The next memory buffer, or NULL if last
160  */
161  virtual emac_mem_buf_t *get_next(const emac_mem_buf_t *buf) const;
162 
163  /**
164  * Return pointer to the payload of the buffer
165  *
166  * @param buf Memory buffer
167  * @return Pointer to the payload
168  */
169  virtual void *get_ptr(const emac_mem_buf_t *buf) const;
170 
171  /**
172  * Return payload size of the buffer
173  *
174  * @param buf Memory buffer
175  * @return Size in bytes
176  */
177  virtual uint32_t get_len(const emac_mem_buf_t *buf) const;
178 
179  /**
180  * Sets the payload size of the buffer
181  *
182  * The allocated payload size will not change. It is not permitted
183  * to change the length of a buffer that is not the first (or only) in a chain.
184  *
185  * @param buf Memory buffer
186  * @param len Payload size, must be less or equal allocated size
187  */
188  virtual void set_len(emac_mem_buf_t *buf, uint32_t len);
189 
190  /**
191  * Sets memory buffer pool allocation unit
192  *
193  * Sets the maximum size of contiguous memory that can be allocated from a pool.
194  *
195  * @param alloc_unit Contiguous memory size
196  */
197  virtual void set_alloc_unit(uint32_t alloc_unit);
198 
199  /**
200  * Sets whether memory is available
201  *
202  * Can be used to disable memory allocation request from emac.
203  *
204  * @param memory True if memory is available
205  */
206  void set_memory_available(bool memory);
207 
208  /**
209  * Gets memory statistics
210  *
211  * Gets memory usage statistics
212  *
213  * @param buffers Number of buffers that are reserved
214  * @param memory Reserved memory in bytes
215  */
216  void get_memory_statistics(int *buffers, int *memory);
217 
218 private:
219  void validate_list() const;
220  template <typename TYPE> void check_value(TYPE value, const char *fmt, ...) const;
221  bool validate_ptr(const emac_mem_buf_t *buf) const;
222  void check_align(uint32_t align) const;
223  mutable rtos::Mutex m_mem_mutex;
224  std::list<emac_memory_t *> m_mem_buffers;
225  unsigned int m_alloc_unit;
226  bool m_memory_available;
227 };
228 
229 #endif /* EMAC_TEST_MEMORY_MANAGER_H */
unsigned int orig_len
Original buffer length (set_len() does not change)
unsigned int len
Buffer length.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:68
void * buffer
Pointer to allocated buffer.
void * ptr
Aligned pointer.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.