Skip to content

Instantly share code, notes, and snippets.

@kjseefried
Forked from anonymous/gist:5f97d8db71776b188820
Last active August 29, 2015 14:06
Show Gist options
  • Save kjseefried/8d0251b3b07e24d782e6 to your computer and use it in GitHub Desktop.
Save kjseefried/8d0251b3b07e24d782e6 to your computer and use it in GitHub Desktop.
better yet, just include a handful of function pointers into chan_t for each of the API methods that need more than the shared part of chan_t struct, initialize them on allocation and put type-specific code in these functions. It will make for a more compact, concise and localized code. (Edit 2) Here's what I mean with function pointers - https:…
typedef struct chan_t
{
pthread_mutex_t m_mu;
pthread_cond_t m_cond;
int closed;
void (* dispose)(struct chan_t * self);
...
} chan_t;
typedef struct unbuffered_t
{
chan_t base;
...
} unbuffered_t;
typedef struct buffered_t
{
chan_t base;
...
} buffered_t;
chan_t* chan_init(int capacity)
{
if (capacity)
{
buffered_t * ch = malloc(sizeof *ch);
ch->dispose = dispose_buffered;
...
return &ch->base;
}
else
{
Unbuffered_t * ch = malloc(sizeof *ch);
ch->dispose = dispose_unbuffered;
...
return &ch->base;
}
}
void dispose_unbuffered(chan_t * self)
{
unbuffered_t * ch = (chan_t*)self;
...
}
void dispose_buffered(chan_t * self)
{
buffered_t * ch = (chan_t*)self;
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment