`
lobin
  • 瀏覽: 229082 次
  • 性別: Icon_minigender_1
  • 來自: 上海
社區版塊
存檔分類
最新評論

C: Linux C 編程 - 屬性__attribute__ ((attribute-list))

 
閱讀更多

屬性

語法

__attribute__ ((attribute-list))

?

寫道
An attribute specifier is of the form __attribute__ ((attribute-list)). An attribute list is a possibly empty comma-separated sequence of attributes, where each attribute is one of the following:

Empty. Empty attributes are ignored.
An attribute name (which may be an identifier such as unused, or a reserved word such as const).
An attribute name followed by a parenthesized list of parameters for the attribute. These parameters take one of the following forms:
An identifier. For example, mode attributes use this form.
An identifier followed by a comma and a non-empty comma-separated list of expressions. For example, format attributes use this form.
A possibly empty comma-separated list of expressions. For example, format_arg attributes use this form with the list being a single integer constant expression, and alias attributes use this form with the list being a single string constant.
An attribute specifier list is a sequence of one or more attribute specifiers, not separated by any other tokens.

You may optionally specify attribute names with ‘__’ preceding and following the name. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use the attribute name __noreturn__ instead of noreturn.

?

?

指定變量屬性

公共的變量屬性包括:

alias ("target")

?

#include <stdio.h>
#include <stdlib.h>

int __read_int(char *arg)
{
  return arg == NULL ? -1 : atoi(arg);
}

extern __typeof(__read_int) read_int __attribute__((alias ("__read_int")));

int main(int argc, char* argv[])
{
  return read_int(argc != 2 ? NULL : argv[1]);
}

?

aligned

aligned (alignment)

?

warn_if_not_aligned (alignment)

?

alloc_size (position)

alloc_size (position-1, position-2)

?

cleanup (cleanup_function)

?

common

nocommon

?

copy

copy (variable)

?

deprecated

deprecated (msg)

?

mode (mode)

?

nonstring

?

packed

?

section ("section-name")

?

tls_model ("tls_model")

?

unused

?

used

?

vector_size (bytes)

?

visibility ("visibility_type")

?

weak

?

noinit

?

指定類型屬性

公共的類型屬性包括:

aligned

aligned (alignment)

?

warn_if_not_aligned (alignment)

?

alloc_size (position)

alloc_size (position-1, position-2)

?

copy

copy (expression)

?

deprecated

deprecated (msg)

?

designated_init

?

may_alias

?

mode (mode)

?

packed

?

寫道
This attribute, attached to a struct, union, or C++ class type definition, specifies that each of its members (other than zero-width bit-fields) is placed to minimize the memory required. This is equivalent to specifying the packed attribute on each of the members.

?

在下面的例子中,根據fat 12/16文件格式定義,vbr結構占用512字節,也就是1個扇區。但是按照fat 12/16文件格式定義的各個字段,定義的結構體,如果沒有指定packed屬性(__attribute__((__packed__)))的話,其結構體大小為520字節,這顯然無法滿足fat 12/16文件格式vbr結構的解析。所以應該指定packed屬性(__attribute__((__packed__))),使得各個字段按照最小內存需要進行布局:

#define __fs_fat_vbr__                                       \
  u1 bs_jmp_boot[3];                   /* BS_JmpBoot */      \
  u1 bs_oem_name[8];                   /* BS_OEMName */      \
  u2 bpb_bytes_per_sector;             /* BPB_BytsPerSec */  \
  u1 bpb_sectors_per_cluster;          /* BPB_SecPerClus */  \
  u2 bpb_reserved_sector_count;        /* BPB_RsvdSecCnt */  \
  u1 bpb_fat_num;                      /* BPB_NumFATs */     \
  u2 bpb_root_entry_count;             /* BPB_RootEntCnt */  \
  u2 bpb_total_sector_16;              /* BPB_TotSec16 */    \
  u1 bpb_media;                        /* BPB_Media */       \
  u2 bpb_fat_size_16;                  /* BPB_FATSz16 */     \
  u2 bpb_sector_per_track;             /* BPB_SecPerTrk */   \
  u2 bpb_head_num;                     /* BPB_NumHeads */    \
  u4 bpb_hidden_sectors;               /* BPB_HiddSec */     \
  u4 bpb_total_sector_32;              /* BPB_TotSec32 */

// fields for FAT12/16
typedef struct 
{
  __fs_fat_vbr__

  u1 bs_drive_num;                       // BS_DrvNum
  u1 bs_reserved1;                       // BS_Reserved1
  u1 bs_boot_sig;                        // BS_BootSig
  u4 bs_volume_id;                       // BS_VolID
  u1 bs_volume_label[11];                // BS_VolLab
  u1 bs_file_system_type[8];             // BS_FilSysType
  u1 bs_boot_code[448];                  // BS_BootCode
  u2 bs_boot_sign;                       // BS_BootSign
} __attribute__((__packed__)) fs_fat_vbr_t;

如果指定了packed屬性(__attribute__((__packed__)))的話,結構體大小才為512字節,而不是520字節。

?

寫道
When attached to an enum definition, the packed attribute indicates that the smallest integral type should be used.

?

enum __attribute__((__packed__)) week
{
  MONDAY, // Monday
  TUESDAY, // Tuesday
  WEDNESDAY, // Wednesday
  THURSDAY, // Thursday
  FRIDAY, // Friday
  SATURDAY, // Saturday 
  SUNDAY // Sunday
};

在前面講枚舉類型時,在gcc下,枚舉類型占4字節。在定義枚舉類型時,如果指定了packed屬性(__attribute__((__packed__))),packed屬性指示枚舉類型使用的是最小整型類型。也就是char類型,這樣的話枚舉類型占用的是1字節。而沒有指定packed屬性,如前面的那樣,那么枚舉類型默認使用的是int整型類型, 這也就是為什么前面所說的枚舉類型占4字節。

?

?

scalar_storage_order ("endianness")

?

transparent_union

?

unused

?

vector_size (bytes)

?

visibility

?

指定函數屬性

函數存在函數定義和函數聲明,給函數指定屬性應該是在函數定義上還是函數聲明上?

?

公共的函數屬性包括:

access

access (access-mode, ref-index)

access (access-mode, ref-index, size-index)

?

alias ("target")

在函數上指定alias屬性,該函數語義上就是個函數定義,只是和普通的函數定義不一樣的是,該函數沒有實現體,因為它只是一個已經定義了的函數的別名。

?

#include <stdio.h>
#include <stdlib.h>

int __read_int(char *arg)
{
  return arg == NULL ? -1 : atoi(arg);
}

int __attribute__((alias ("__read_int"))) read_int(char *arg);

int main(int argc, char* argv[])
{
  return read_int(argc != 2 ? NULL : argv[1]);
}

?

?

aligned

aligned (alignment)

?

alloc_align (position)

?

alloc_size (position)

alloc_size (position-1, position-2)

?

always_inline

?

artificial

?

assume_aligned (alignment)

assume_aligned (alignment, offset)

?

cold

?

const

?

constructor

destructor

constructor (priority)

destructor (priority)

?

copy

copy (function)

?

deprecated

deprecated (msg)

?

error ("message")

warning ("message")

?

externally_visible

?

flatten

?

format (archetype, string-index, first-to-check)

?

format_arg (string-index)

?

gnu_inline

?

hot

?

ifunc ("resolver")

?

interrupt

interrupt_handler

?

leaf

?

malloc

?

no_icf

?

no_instrument_function

?

no_profile_instrument_function

?

no_reorder

?

no_sanitize ("sanitize_option")

?

no_sanitize_address

no_address_safety_analysis

?

no_sanitize_thread

?

no_sanitize_undefined

?

no_split_stack

?

no_stack_limit

?

noclone

?

noinline

?

noipa

?

nonnull

nonnull (arg-index, …)

?

noplt

?

noreturn

?

nothrow

?

optimize (level, …)

optimize (string, …)

?

patchable_function_entry

?

pure

?

returns_nonnull

?

returns_twice

?

section ("section-name")

?

sentinel

sentinel (position)

?

simd

simd("mask")

?

stack_protect

?

target (string, …)

?

symver ("[email protected]")

?

target_clones (options)

?

unused

?

used

?

visibility ("visibility_type")

?

warn_unused_result

?

weak

?

weakref

weakref ("target")

?

可以指定多個屬性

寫道
To specify multiple attributes, separate them by commas within the double parentheses: for example, ‘__attribute__ ((aligned (16), packed))’.

?

0
1
分享到:
評論
Global site tag (gtag.js) - Google Analytics 开心农场种蔬菜赚钱 暴涨股票推荐 1宁夏11选5推荐号码预测专家 湖北十一选五怎么玩 宁夏11选5平台注册 怎么看股票行情图 十一选五中奖规则表 山西快乐十分稳赚技巧 广东十一选五合买平台 江苏快三前后三期走势图 广西快乐双彩中奖查询 山东十一选五分布走势图 暴跌之后暴涨的股票 广东36选7最新开奖查询好彩3 陕西11选5走势分布图 河南今天11选5走势图 期货配资图片