creating a C function with a given size in the text segment

后端 未结 4 1687
梦谈多话
梦谈多话 2021-02-19 11:48

I\'m programming an embedded powerpc 32 system with a 32 kbyte 8-way set associative L2 instruction cache. To avoid cache thrashing we align functions in a way such that the tex

4条回答
  •  悲哀的现实
    2021-02-19 12:29

    If you are willing to expend some effort, you can use

    __attribute__((section(".text.hotpath.a")))
    

    to place the function into a separate section, and then in a custom linker script explicitly place the functions.

    This gives you a bit more fine-grained control than simply asking for the functions to be aligned, but requires more hand-holding.

    Example, assuming that you want to lock 4KiB into cache:

    SECTIONS {
        .text.hotpath.one BLOCK(0x1000) {
            *(.text.hotpath.a)
            *(.text.hotpath.b)
        }
    }
    ASSERT(SIZEOF(.text.hotpath.one) <= 0x1000, "Hot Path functions do not fit into 4KiB")
    

    This will make sure the hot path functions a and b are next to each other and both fit into the same block of 4 KiB that is aligned on a 4 KiB boundary, so you can simply lock that page into the cache; if the code doesn't fit, you get an error.

    You can even use

    NOCROSSREFS(.text.hotpath.one .text)
    

    to forbid hot path functions calling other functions.

提交回复
热议问题