diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2009-07-08 11:07:56 +1000 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2009-07-08 13:42:04 +1000 |
commit | 0e2be045663045dc087f21df95de91f824322915 (patch) | |
tree | 6b56edf7be0d4f69bd02748eb2c26bf6c5f88c9f /src/XIint.h | |
parent | c81a383f11c87976454200805b4b5416a414b4c5 (diff) |
Add next_block() call to advance pointers over memory.
Simple call to get the pointer to the next field when operating on a wire
protocol struct or on a single-memory-block Xlib structure.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'src/XIint.h')
-rw-r--r-- | src/XIint.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/XIint.h b/src/XIint.h index 63e33f1..400c920 100644 --- a/src/XIint.h +++ b/src/XIint.h @@ -29,4 +29,41 @@ typedef struct _XInputData XExtensionVersion *vers; } XInputData; + +/** + * Returns the next valid memory block of the given size within the block + * previously allocated. + * Use letting pointers inside a struct point to bytes after the same + * struct, e.g. during protocol parsing etc. + * + * Caller is responsible for allocating enough memory. + * + * Example: + * void *ptr; + * struct foo { + * int num_a; + * int num_b; + * int *a; + * int *b; + * } bar; + * + * ptr = malloc(large_enough); + * bar = next_block(&ptr, sizeof(struct foo)); + * bar->num_a = 10; + * bar->num_b = 20; + * bar->a = next_block(&ptr, bar->num_a); + * bar->b = next_block(&ptr, bar->num_b); + */ +static inline void* +next_block(void **ptr, int size) { + void *ret = *ptr; + + if (!*ptr) + return NULL; + + *(unsigned char**)ptr += size; + + return ret; +} + #endif |