简体   繁体   English

此宏功能有什么作用?

[英]What does this MACRO function do?

This fucntion: 此功能:

FIFO_ELEM_AT(p_fifo, index) ((uint8_t*) ((uint8_t*) p_fifo->elem_array) + (p_fifo->elem_size) * (index))

and "p_fifo" was defined as: 而“ p_fifo”的定义为:

typedef struct
{
    void* elem_array;
    uint32_t elem_size;
    uint32_t array_len;
    uint32_t head;
    uint32_t tail;
    fifo_memcpy memcpy_fptr; /* must be a valid function or NULL */
  } fifo_t;

Overall it's trying to do something to a static fifo data structure. 总体而言,它正在尝试对静态fifo数据结构进行处理。 I can't really tell what you can possibly achieve by the calculation described in the function. 我真的无法告诉您通过函数中描述的计算可以实现的目标。 Please help! 请帮忙!

You should read the documentation for the code you are looking at. 您应该阅读正在查看的代码的文档。
Otherwise you are stuck with guessing. 否则,您将无法进行猜测。 You seem to ask for help with guessing. 您似乎在寻求猜测的帮助。
Here are my guesses: 这是我的猜测:

  • The mentioned struct type describes all the information for handling an instance of a special data construct. 提到的结构类型描述了用于处理特殊数据结构实例的所有信息。
  • Because of the name, it is probably a FIFO aka "ringbuffer", aka "queue" (as commented by MM) 由于名称的原因,它可能是FIFO,也称为“ ringbuffer”,也称为“队列”(由MM注释)
  • It needs an accompanying memory, to be used as the actual FIFO. 它需要一个附带的存储器,用作实际的FIFO。
  • The memory to use is referenced by a pointer to it, "elem_array". 要使用的内存由指向它的指针“ elem_array”引用。
  • "elem_size" is the size of one element in the FIFO “ elem_size”是FIFO中一个元素的大小
  • "head" references the next element to be returned from FIFO “ head”引用要从FIFO返回的下一个元素
  • "tail" references the last element added to the FIFO “ tail”引用添加到FIFO中的最后一个元素
  • or the other way around 或相反
  • "memcpy_fptr" references a potentially optimized function to move elements to and from the FIFO “ memcpy_fptr”引用了一个潜在的优化函数,用于将元素移入和移出FIFO
  • the quoted macro provides access to the element in the FIFO at a given index 用引号引起的宏可以访问给定索引处的FIFO中的元素
  • in the form of a pointer to uint8_t; 以指向uint8_t的指针的形式; instead of a pointer-to-type-of-element 而不是元素类型的指针
  • the macro gets the address to the FIFO, does some pointer arithmetic (including the size of elements and the given index) and thereby evaluates to the result: uint8_t-pointer to indexed element 宏获取到FIFO的地址,执行一些指针运算(包括元素的大小和给定的索引),并由此求出结果:uint8_t-指向已索引元素的指针

On top of my guesses, here are my opinion-based recommendations: 在我的猜测之上,这是我基于意见的建议:

  • when using the result (pointer to uint8_t), cast it to appropriate type 使用结果(指向uint8_t的指针)时,将其转换为适当的类型
  • when using the macro, make sure that you give an index to an existing element, at least an element which has been added, with a meaningful value 使用宏时,请确保为现有元素(至少是已添加的元素)提供有意义的值的索引
  • when using the macro, give an index to an element which has not been returned from the FIFO yet 使用宏时,为尚未从FIFO返回的元素提供索引
  • do not use the macro, it seems to go against the idea of a FIFO, 不要使用宏,这似乎违反了FIFO的想法,
    with the possible exception to get info on the content of the FIFO for debugging purposes 为了调试目的可能会获得有关FIFO内容的信息的异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM