https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90941
Bug ID: 90941 Summary: [rfe] attribute to specify write-once static variable for early-initialized values Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: allison.karlitskaya at redhat dot com Target Milestone: --- The GObject type system contains support for private variables, which works like this: you create a MyObjectPrivate structure and tell GObject about its size, and GObject will allocate this extra space every time it creates an instance of MyObject. this space is always stored at a constant offset relative to the pointer to the object in question, but his offset is determined at runtime (during the one-time class initialization procedure). The code looks something like this (this part is emitted by macros): static int priv_offset; static void one_time_initialize_my_object_class() { ... priv_offset = calculate_offset(); ... } static inline MyObjectPrivate *get_priv_struct(MyObject *self) { return (MyObjectPrivate *) (((char *) self) + priv_offset); } and then the user uses it like this: void my_object_get_field (MyObject *self) { return get_priv_struct(self)->field; } It's often the case that the private structure is used very many times from within a single function. Because the compiler doesn't know that one_time_initialize_my_object() will never be called again (and that the one call to it happens before any object is allocated, and therefore definitely before any function operating on an object is called), it has to assume that the 'priv_offset' variable could change at any time. This leads to the variable being read and re-read (and the addition performed again). It would be really nice if there were an attribute to declare that a variable will be written to exactly once (before it is ever read from) and then never again. I've tried some tricks with the alias attribute and using externs with the wrong type, but none of these approaches are very clean. Thanks!