Created
September 16, 2014 21:19
-
-
Save patrickt/c5f354e05ef7422749fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct helium_connection_s { | |
int retain_count; | |
int foo; | |
}; | |
typedef struct helium_connection_s *helium_connection_t; | |
struct helium_device_s { | |
int retain_count; | |
float blah; | |
}; | |
typedef struct helium_device_s *helium_device_t; | |
union helium_object_u { | |
helium_connection_t as_connection; | |
helium_device_t as_device; | |
} __attribute__((transparent_union)); | |
typedef union helium_object_u helium_object_t; | |
void helium_retain(helium_object_t obj) | |
{ | |
int *retain_count_pointer = (int*)obj.as_connection; | |
(*retain_count_pointer)++; | |
} | |
void helium_release(helium_object_t obj) | |
{ | |
int *retain_count_pointer = (int*)obj.as_connection; | |
(*retain_count_pointer)--; | |
} | |
int main (int argc, char const *argv[]) | |
{ | |
helium_connection_t conn = calloc(sizeof(struct helium_connection_s), 1); | |
helium_device_t dev = calloc(sizeof(struct helium_device_s), 1); | |
// look ma, no casts | |
printf("rcounts: %d, %d\n", conn->retain_count, dev->retain_count); | |
helium_retain(conn); | |
helium_retain(dev); | |
printf("rcounts: %d, %d\n", conn->retain_count, dev->retain_count); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment