Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers emac_TestMemoryManager.h Source File

emac_TestMemoryManager.h

00001 /*
00002  * Copyright (c) 2018, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #if MBED_CONF_APP_TEST_WIFI || MBED_CONF_APP_TEST_ETHERNET
00019 
00020 #ifndef EMAC_TEST_MEMORY_MANAGER_H
00021 #define EMAC_TEST_MEMORY_MANAGER_H
00022 
00023 #include <list>
00024 
00025 #include "EMACMemoryManager.h"
00026 
00027 #define MEM_CHECK       0x01
00028 #define MEM_NO_ALIGN    0x02
00029 
00030 typedef struct emac_memory {
00031     struct emac_memory *next;
00032     void *buffer;              /**< Pointer to allocated buffer */
00033     unsigned int orig_len;     /**< Original buffer length (set_len() does not change) */
00034     unsigned int len;          /**< Buffer length */
00035     void *ptr;                 /**< Aligned pointer */
00036     bool first;
00037 } emac_memory_t;
00038 
00039 class EmacTestMemoryManager : public EMACMemoryManager {
00040 public:
00041 
00042     /**
00043      * Creates emac test memory manager
00044      */
00045     EmacTestMemoryManager();
00046 
00047     /*
00048      * Gets static instance
00049      */
00050     static EmacTestMemoryManager &get_instance();
00051 
00052     /**
00053      * Allocates memory buffer from the heap
00054      *
00055      * Memory buffer allocated from heap is always contiguous and can be arbitrary size.
00056      *
00057      * @param size     Size of the memory to allocate in bytes
00058      * @param align    Memory alignment requirement in bytes
00059      * @return         Allocated memory buffer, or NULL in case of error
00060      */
00061     virtual emac_mem_buf_t *alloc_heap(uint32_t size, uint32_t align);
00062 
00063     /**
00064      * Allocates memory buffer from the heap
00065      *
00066      * Memory buffer allocated from heap is always contiguous and can be arbitrary size.
00067      *
00068      * @param size        Size of the memory to allocate in bytes
00069      * @param align       Memory alignment requirement in bytes
00070      * @param opt         Options
00071      * @return            Allocated memory buffer, or NULL in case of error
00072      */
00073     emac_mem_buf_t *alloc_heap(uint32_t size, uint32_t align, uint8_t opt);
00074 
00075     /**
00076      * Allocates memory buffer chain from a pool
00077      *
00078      * Memory allocated from pool is contiguous if size is equal or less than
00079      * (aligned) allocation unit, otherwise may be chained. Will typically come from
00080      * fixed-size packet pool memory.
00081      *
00082      * @param  size    Total size of the memory to allocate in bytes
00083      * @param  align   Memory alignment requirement for each buffer in bytes
00084      * @return         Allocated memory buffer chain, or NULL in case of error
00085      */
00086     virtual emac_mem_buf_t *alloc_pool(uint32_t size, uint32_t align);
00087 
00088     /**
00089      * Allocates memory buffer chain from a pool
00090      *
00091      * Memory allocated from pool is contiguous if size is equal or less than
00092      * (aligned) allocation unit, otherwise may be chained. Will typically come from
00093      * fixed-size packet pool memory.
00094      *
00095      * @param size        Total size of the memory to allocate in bytes
00096      * @param align       Memory alignment requirement for each buffer in bytes
00097      * @param opt         Options
00098      * @return            Allocated memory buffer chain, or NULL in case of error
00099      */
00100     emac_mem_buf_t *alloc_pool(uint32_t size, uint32_t align, uint8_t opt);
00101 
00102     /**
00103      * Get memory buffer pool allocation unit
00104      *
00105      * Returns the maximum size of contiguous memory that can be allocated from a pool.
00106      *
00107      * @param align    Memory alignment requirement in bytes
00108      * @return         Contiguous memory size
00109      */
00110     virtual uint32_t get_pool_alloc_unit(uint32_t align) const;
00111 
00112     /**
00113      * Free memory buffer chain
00114      *
00115      * If memory buffer is chained must point to the start of the chain. Frees all buffers
00116      * from the chained list.
00117      *
00118      * @param mem      Memory buffer chain to be freed.
00119      */
00120     virtual void free(emac_mem_buf_t *buf);
00121 
00122     /**
00123      * Return total length of a memory buffer chain
00124      *
00125      * Returns a total length of this buffer and any following buffers in the chain.
00126      *
00127      * @param mem      Memory buffer chain
00128      * @return         Total length in bytes
00129      */
00130     virtual uint32_t get_total_len(const emac_mem_buf_t *buf) const;
00131 
00132     /**
00133      * Copy a memory buffer chain
00134      *
00135      * Copies data from one buffer chain to another. Copy operation does not adjust the lengths
00136      * of the copied-to memory buffer chain, so chain total lengths must be the same.
00137      *
00138      * @param to_buf    Memory buffer chain to copy to
00139      * @param from_buf  Memory buffer chain to copy from
00140      */
00141     virtual void copy(emac_mem_buf_t *to_buf, const emac_mem_buf_t *from_buf);
00142 
00143     /**
00144      * Concatenate two memory buffer chains
00145      *
00146      * Concatenates buffer chain to end of the other buffer chain. Concatenated-to buffer total length
00147      * is adjusted accordingly. cat_buf must point to the start of a the chain. After concatenation
00148      * to_buf's chain now owns those buffers, and they will be freed when the to_buf chain is freed.
00149      *
00150      * @param to_buf   Memory buffer chain to concatenate to
00151      * @param cat_buf  Memory buffer chain to concatenate
00152      */
00153     virtual void cat(emac_mem_buf_t *to_buf, emac_mem_buf_t *cat_buf);
00154 
00155     /**
00156      * Returns the next buffer
00157      *
00158      * Returns the next buffer from the memory buffer chain.
00159      *
00160      * @param mem      Memory buffer
00161      * @return         The next memory buffer, or NULL if last
00162      */
00163     virtual emac_mem_buf_t *get_next(const emac_mem_buf_t *buf) const;
00164 
00165     /**
00166      * Return pointer to the payload of the buffer
00167      *
00168      * @param mem      Memory buffer
00169      * @return         Pointer to the payload
00170      */
00171     virtual void *get_ptr(const emac_mem_buf_t *buf) const;
00172 
00173     /**
00174      * Return payload size of the buffer
00175      *
00176      * @param mem      Memory buffer
00177      * @return         Size in bytes
00178      */
00179     virtual uint32_t get_len(const emac_mem_buf_t *buf) const;
00180 
00181     /**
00182      * Sets the payload size of the buffer
00183      *
00184      * The allocated payload size will not change. It is not permitted
00185      * to change the length of a buffer that is not the first (or only) in a chain.
00186      *
00187      * @param mem      Memory buffer
00188      * @param len      Payload size, must be less or equal allocated size
00189      */
00190     virtual void set_len(emac_mem_buf_t *buf, uint32_t len);
00191 
00192     /**
00193      * Sets memory buffer pool allocation unit
00194      *
00195      * Sets the maximum size of contiguous memory that can be allocated from a pool.
00196      *
00197      * @param alloc_unit   Contiguous memory size
00198      */
00199     virtual void set_alloc_unit(uint32_t alloc_unit);
00200 
00201     /**
00202      * Sets whether memory is available
00203      *
00204      * Can be used to disable memory allocation request from emac.
00205      *
00206      * @param memory    True if memory is available
00207      */
00208     void set_memory_available(bool memory);
00209 
00210     /**
00211      * Gets memory statistics
00212      *
00213      * Gets memory usage statistics
00214      *
00215      * @param buffers   Number of buffers that are reserved
00216      * @param memory    Reserved memory in bytes
00217      */
00218     void get_memory_statistics(int *buffers, int *memory);
00219 
00220 private:
00221     void validate_list() const;
00222     template <typename TYPE> void check_value(TYPE value, const char *fmt, ...) const;
00223     bool validate_ptr(const emac_mem_buf_t *buf) const;
00224     void check_align(uint32_t align) const;
00225     mutable rtos::Mutex m_mem_mutex;
00226     std::list<emac_memory_t *> m_mem_buffers;
00227     unsigned int m_alloc_unit;
00228     bool m_memory_available;
00229 };
00230 
00231 #endif /* EMAC_TEST_MEMORY_MANAGER_H */
00232 #endif