Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created September 16, 2014 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/c5f354e05ef7422749fe to your computer and use it in GitHub Desktop.
Save patrickt/c5f354e05ef7422749fe to your computer and use it in GitHub Desktop.
#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