| 1 |
/* pointers based on relative offsets, for shared memory |
| 2 |
* Copyright (C) 2012 Red Hat Inc. |
| 3 |
* |
| 4 |
* This file is part of systemtap, and is free software. You can |
| 5 |
* redistribute it and/or modify it under the terms of the GNU General |
| 6 |
* Public License (GPL); either version 2, or (at your option) any |
| 7 |
* later version. |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifndef _OFFPTR_H |
| 11 |
#define _OFFPTR_H |
| 12 |
|
| 13 |
|
| 14 |
/* An offset pointer refers to memory without using an absolute address. This |
| 15 |
* is useful for shared memory between processes, and perhaps also for cases |
| 16 |
* where memory may move, as with realloc. */ |
| 17 |
|
| 18 |
/* Implementation NB: By nature, NULL is never ever a relative pointer, always |
| 19 |
* absolute. If it's treated as a plain offset like any other pointer, then it |
| 20 |
* will definitely be wrong when the base is changed. Thus, NULL must be |
| 21 |
* treated as a special case. */ |
| 22 |
|
| 23 |
/* Here we have a few different implementations for testing and comparison: |
| 24 |
* |
| 25 |
* OFFPTR_IMPL_GLOBAL: Offsets are stored relative to a global pointer, the |
| 26 |
* shared-memory base. This means it will only work for pointers within shm. |
| 27 |
* |
| 28 |
* OFFPTR_IMPL_SELF: Offsets are stored relative to the offptr_t itself. |
| 29 |
* This has a little more flexibility, but copying offptr_t values requires |
| 30 |
* more pointer arithmetic. |
| 31 |
* |
| 32 |
* OFFPTR_IMPL_POINTERS: Plain pointers, not suitable for use where the |
| 33 |
* relative functionality is actually needed! (e.g. anything multiprocess) |
| 34 |
* |
| 35 |
* The default for now is OFFPTR_IMPL_GLOBAL. |
| 36 |
*/ |
| 37 |
#if !defined(OFFPTR_IMPL_GLOBAL) \ |
| 38 |
&& !defined(OFFPTR_IMPL_SELF) \ |
| 39 |
&& !defined(OFFPTR_IMPL_POINTERS) |
| 40 |
#define OFFPTR_IMPL_GLOBAL 1 |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(OFFPTR_IMPL_GLOBAL) |
| 44 |
/* OFFPTR_IMPL_GLOBAL: In this mode, the offset is stored relative to the |
| 45 |
* shared-memory base pointer (see runtime/dyninst/shm.c). This has the |
| 46 |
* advantage of very easy offptr_t copies. The disadvantage is that it only |
| 47 |
* works for pointers that are part of shared memory. It may also be costly to |
| 48 |
* dereference the global pointer all the time, but that can be measured. |
| 49 |
* |
| 50 |
* For NULL, we can get away with the special-case of offset 0, so long as we |
| 51 |
* accept that the very base of shared memory is not a valid offptr_t target. |
| 52 |
* Given that, offset 0 should be simpler for code gen, and insulates against |
| 53 |
* bugs slightly since a calloced offptr_t will already represent NULL. |
| 54 |
*/ |
| 55 |
|
| 56 |
typedef struct { |
| 57 |
/* The offset is always relative to the global shared-memory base. |
| 58 |
* NULL is special-cased as offset==0. */ |
| 59 |
ptrdiff_t offset; |
| 60 |
} offptr_t; |
| 61 |
|
| 62 |
static void* _stp_shm_base; /* from runtime/dyninst/shm.c */ |
| 63 |
|
| 64 |
static inline void * |
| 65 |
offptr_get(offptr_t* op) |
| 66 |
{ |
| 67 |
return op->offset ? (_stp_shm_base + op->offset) : NULL; |
| 68 |
} |
| 69 |
|
| 70 |
static inline void |
| 71 |
offptr_set(offptr_t* op, void* ptr) |
| 72 |
{ |
| 73 |
op->offset = ptr ? (ptr - _stp_shm_base) : 0; |
| 74 |
} |
| 75 |
|
| 76 |
#elif defined(OFFPTR_IMPL_SELF) |
| 77 |
/* OFFPTR_IMPL_SELF: In this mode, the offset is stored relative to the |
| 78 |
* offptr_t itself. The advantage of this is better abstraction, as different |
| 79 |
* offptr_t could refer to different memory blocks (as long as they're |
| 80 |
* self-contained). The disadvantage is that it requires more math to copy |
| 81 |
* offptr_t values around, as with linked-list updates. |
| 82 |
* |
| 83 |
* For NULL, we could let offset 0 be special, but linked lists often want to |
| 84 |
* link back to themselves, which would be a legitimate offset 0. Instead, |
| 85 |
* we'll let offset 1 represent NULL, which should never happen naturally as |
| 86 |
* it's in the middle of the offptr_t itself. |
| 87 |
*/ |
| 88 |
|
| 89 |
typedef struct { |
| 90 |
/* The offset is always relative to the offptr_t itself. |
| 91 |
* NULL is special-cased as offset==1. */ |
| 92 |
ptrdiff_t offset; |
| 93 |
} offptr_t; |
| 94 |
|
| 95 |
static inline void * |
| 96 |
offptr_get(offptr_t* op) |
| 97 |
{ |
| 98 |
return (op->offset == 1) ? NULL : (op->offset + (void*)op); |
| 99 |
} |
| 100 |
|
| 101 |
static inline void |
| 102 |
offptr_set(offptr_t* op, void* ptr) |
| 103 |
{ |
| 104 |
op->offset = (ptr == NULL) ? 1 : (ptr - (void*)op); |
| 105 |
} |
| 106 |
|
| 107 |
#elif defined(OFFPTR_IMPL_POINTERS) |
| 108 |
/* OFFPTR_IMPL_POINTERS: In this mode, offptr_t is basically a plain pointer |
| 109 |
* again. Despite the level of abstraction, it should compile down to the same |
| 110 |
* as using direct pointers would. |
| 111 |
* |
| 112 |
* Since this is always absolute, the NULL caveat doesn't apply. |
| 113 |
*/ |
| 114 |
typedef struct { void* pointer; } offptr_t; |
| 115 |
static inline void * offptr_get(offptr_t* op) { return op->pointer; } |
| 116 |
static inline void offptr_set(offptr_t* op, void* ptr) { op->pointer = ptr; } |
| 117 |
|
| 118 |
#else |
| 119 |
#error "No offptr_t implementation?!" |
| 120 |
#endif |
| 121 |
|
| 122 |
|
| 123 |
/* Since offptr_t is untyped, this template-like macro lets you define |
| 124 |
* accessors with the appropriate pointer types enforced. */ |
| 125 |
#define DEFINE_OFFPTR_GETSET(prefix, T1, T2, member) \ |
| 126 |
static inline T2* prefix##_##member(T1* ptr) { \ |
| 127 |
return (T2*) offptr_get(&ptr->member); \ |
| 128 |
} \ |
| 129 |
static inline void prefix##_set_##member(T1* ptr, T2* val) { \ |
| 130 |
offptr_set(&ptr->member, val); \ |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
#endif /* _OFFPTR_H */ |