What is meaning of the '_attribute_((aligned(4)));' in the first line?

后端 未结 3 1018
不思量自难忘°
不思量自难忘° 2021-01-25 05:03
char buf[BUF_LEN]_attribute_((aligned(4)));
ssize_t len, i = 0;
/* read BUF_LEN bytes\' worth of events */
len = read (fd, buf, BUF_LEN);
/* loop over every read event u         


        
3条回答
  •  离开以前
    2021-01-25 05:09

    The instruction tells the compiler to put the buffer on an address that's a multiple of four bytes.

    On some processors, this has no effect, on other processors it speeds up the memory access if you don't read a single byte at a time but rather 2, 4 or 8 (16, 32 and 64 bit) and on some processors it is even required for 2, 4 or 8 byte access (otherwise a bus error occurrs).

    In this case, this is indeed relevant since the buffer is later access as series of inotify_event structs, which contain members that are accessed as 16, 32 or 64 bit values.

提交回复
热议问题