On 20/05/2010 3:06 p.m., Ian Lance Taylor wrote:
I don't see how you can possibly avoid ALU operations given your
design goals (i.e., permitting the pointers to exist in memory areas
other than the low 4GB). You are just asking for a way to make the
compiler generate them automatically.
Heck, even if you don't like C++, you can do it yourself with macros.
Here's the relative-pointer idea expressed as macros, too:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef uint32_t relptr;
struct s {
relptr p;
int elem;
};
#define GET_RELPTR(p) \
((void*) ((p) + (uintptr_t) &(p)))
#define SET_RELPTR(p, q) \
(p) = (relptr) ((char*) (q) - (char*) &(p))
int main(void) {
struct s arr[100];
arr[0].elem=143;
arr[99].elem=42;
SET_RELPTR(arr[0].p, &arr[99]);
SET_RELPTR(arr[99].p, &arr[0]);
printf("%d\n", ((struct s *) GET_RELPTR(arr[0].p))->elem);
printf("%d\n", ((struct s *) GET_RELPTR(arr[99].p))->elem);
return 0;
}
A relptr can point to anywhere else within the same object the pointer
is stored in, so long as the object is smaller than 4GB.
Cheers,
Nicholas Sherlock