bluesky idea: __attribute__ (netorder)
Stephen Williams
steve@icarus.com
Sun Sep 6 18:57:00 GMT 1998
> Even bluer sky...
>
> __attribute__((property(foo)))
bcurrie@tssc.co.nz said:
> *THAT* is probably even better. I like the expandability of if,
> though I'm not sure what other properties will be wanted. However,
> someone will probably think of some.
Say I have a memory region that is accessible to the software, but also
accessible to a DMA controller that can move it to my faster program
memory.
typedef void*image_mem_ptr __attribute__((property(image_mem)));
void*image_data __attribute__((property(image_mem)));
extern void dma_copy_from(void*dest, image_mem_ptr src);
Or maybe I have a piece of hardware that requires a buffer that must
not be cached
typedef void*dev_buf_ptr __attribute__((property(non_cached)));
extern dev_buf_ptr alloc_buffer(size_t);
extern dev_buf_ptr device_register;
device_register = alloc_buffer(1024);
The Linux kernel has different kinds of addresses, i.e. virtual, physical,
and bus addresses:
typedef void*void_phys __attribute__((property(physical)));
typedef void*void_bus __attribute__((property(bus)));
extern void_phys virt_to_phys(void*);
extern void_bus virt_to_bus(void*);
extern void* bus_to_virt(void_bus);
extern void* phys_to_bus(void_phys);
struct device_dma_registers {
void_bus addr;
__u32 count;
};
Embedded and operating system programmers can get loads of mileage out
if this kind of thing. We deal with special kinds of data all the time.
We don't necessarily need the compiler to treat the memory any differently,
other then to notice and warn about property mismatches. The properties
can be type checking is done.
The endian property can be handled by this technique by clever use of inline
and typedefs, to wit:
typedef unsigned long net_ulong __attribute__((property(netorder));
#ifdef __sparc__
inline net_ulong htonl(unsigned long x) { return x; }
#endif
#ifdef __i386__
inline net_ulong htonl(unsigned long x) { return swapbytes(x); }
#endif
The property names are not interpreted at all by the compiler. They are
just strings (or a list of strings a la property(netorder,bus)) that
the compiler checks for mismatches. The rvalue needs at least the properties
of the lvalue to prevent a warning.
--
Steve Williams "The woods are lovely, dark and deep.
steve@icarus.com But I have promises to keep,
steve@picturel.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."
More information about the Gcc
mailing list